Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Auth.currentAuthenticatedUser() and Auth.currentSession()?

Prior to every call made to the backend, I used Auth.currentAuthenticatedUser() to obtain idToken.jwtToken and pass it in the header of my request to the backend server for data.

Is there a difference between using Auth.currentSession() instead of Auth.currentAuthenticatedUser() for my use-case? Does Auth.currentAuthenticatedUser() refresh the token once it has expired, similar to Auth.currentSession()?

like image 344
user5735224 Avatar asked Apr 18 '19 05:04

user5735224


People also ask

What is the difference between user authentication and user authorization?

The user authentication is visible at user end. The user authorization is not visible at the user end. The user authentication is identified with username, password, face recognition, retina scan, fingerprints, etc. The user authorization is carried out through the access rights to resources by using roles that have been pre-defined.

What is an example of an authentication requirement?

Example: Employees in a company are required to authenticate through the network before accessing their company email. Example: After an employee successfully authenticates, the system determines what information the employees are allowed to access. Writing code in comment?

What is the difference between OAuth and OpenID Connect (OIDC)?

Generally, transmit information through an Access Token. The OpenID Connect (OIDC) protocol is an authentication protocol that is generally in charge of user authentication process. The OAuth 2.0 protocol governs the overall system of user authorization process. 2FA/MFA (Two-Factor Authentication / Multi-Factor Authentication)

What is the authentication and authorization area unit?

Both Authentication and Authorization area units are utilized in respect of knowledge security that permits the safety of an automatic data system. Each area unit terribly crucial topics usually related to the online as key items of its service infrastructure.


1 Answers

The documentation for amplify auth is still very poor, so I looked into the source code for @aws-amplify/auth and amazon-cognito-identity-js packages and these are the findings:

  • currentAuthenticatedUser will try to retrieve authenticated user info from localstorage (unless your storage options is configured otherwise). If it doesn't exist in storage, then it will make api calls to retrieve user info which involves automatically refreshing the user session in the process.
  • currentSession will not check the local storage and always invoke the API which also involves automatically refreshing the user session if expired.

So to answer your question directly, the Auth.currentAuthenticatedUser() method doesn't always give you a valid token. If your storage contains an expired token, it will just return that. This would require you to call user.getSession() on the returned user object to request for a new session/token manually. I recommend that you use Auth.currentSession() since this handles the token refresh automatically and always returns a valid token.

like image 152
hwkd Avatar answered Oct 21 '22 23:10

hwkd