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.
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.
Access and Logging in via Amplify CLI These credentials would be stored under a profile name (usually default ).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With