Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Amplify, how to check if user is logged in?

Tags:

I've been using the aws-amplify library with ionic and was wondering how I would check if a user is logged in? I'm coming from a firebase background so this is quite different. This is so that I can grant access to certain pages based on the user's log in status. In my auth provider I import Amplify {Auth}. I can see that it's possible to get several pieces of data but I'm not sure what to use. There's currentUserPoolUser, getCurrentUser(), getSyncedUser(), currentAuthenticatedUser, currentSession, getCurrentUser(), userSession, currentUserCredentials, currentCredentials and currentUserInfo. I can't seem to find any documentation on any of this either. Everything I've read and watched covers up until the user signs in... Is this all supposed to be done on the client? Thanks.

like image 981
NeXtMaN_786 Avatar asked Feb 11 '18 15:02

NeXtMaN_786


People also ask

What does Auth currentAuthenticatedUser do?

currentAuthenticatedUser() to get the current authenticated user object. This method can be used to check if a user is logged in when the page is loaded. It will throw an error if there is no user logged in. This method should be called after the Auth module is configured or the user is logged in.

Where does amplify store credentials?

Access and Logging in via Amplify CLI These credentials would be stored under a profile name (usually default ).

How do I add users to AWS amplify?

Sign in to the AWS Management Console and open AWS Amplify. In the navigation pane, choose Amplify Studio settings. On the Amplify Studio settings page, in the Access control settings section, choose Add team members. For Email, enter the email address of the team member to invite.

Can I use amplify without CLI?

js. Q: Can I use the Amplify libraries even if I do not use the CLI? Yes. The libraries can be used to access backend resources that were created without the Amplify CLI.


2 Answers

I'm using the ionViewCanEnter() function in every page to allow/deny access. The return value of this function determines if the page can be loaded or not (and it is executed before running the costructor). Inside this function you have to implement you logic.

In my case, using Amplify, I'm doing this:

async function ionViewCanEnter() {
    try {
        await Auth.currentAuthenticatedUser();
        return true;
    } catch {
        return false;
    }
}

Since amplify currentAuthenticatedUser() return a promise I use async await to wait for the response to know if the user is logged in or not.

like image 147
AleCat83 Avatar answered Sep 24 '22 06:09

AleCat83


Hey I think for now you can only use Auth.currentUserInfo(); to detect whether logged in or not. It will return undefined if you are not logged in or an object if you are.

like image 33
powerful23 Avatar answered Sep 21 '22 06:09

powerful23