I'm exploring Amplify for a React Native project and wondering how I would go about persisting a user session if I'm not using the HOC's?
My code is more or less identical to the documentation:
import { Auth } from 'aws-amplify';
Auth.signUp({
username,
password,
attributes: {
email, // optional
phone_number, // optional - E.164 number convention
// other custom attributes
},
validationData: [] //optional
})
.then(data => console.log(data))
.catch(err => console.log(err));
Upon submission, I see the user is created, but nothing is being persisted anywhere. Is there something I'm missing?
When I use withAuthenticator
everything works as I would expect, I see entries added to my AsyncStorage and when I reload the app the session persists, however, I have some requirements for additional customization to the screen flow and behaviors which don't quite fit when I go this route.
@ckorhonen you can use Auth.currentAuthenticatedUser()
to check whether the user is logged in or not and then determine switch the app state to signed in. The library will store user info into AsyncStorage and keep it until Auth.signOut()
called.
@powerful23 This does not seem to be working.
Auth.signUp(username, password, email)
.then(data => {
console.log(data);
const session = Auth.currentAuthenticatedUser()
.then(data => {
console.log(data)
});
})
.catch(err => {
console.log(err)
});
In the console I see:
Object {user: CognitoUser, userConfirmed: false, userSub: "7662cdf4-68e0-45cc-86e2-1fa32125c06e"}
Get item: key is federatedUser with options undefined
If I inspect AsyncStorage using showAsyncStorageContentInDev()
then I see the following - nothing related to my user session:
I notice if I inspect my CognitoUser, user.storage
refers to MemoryStorage - is there something I need to do in order to have it use AsyncStorage?
@ckorhonen There is no authenticated user with just signing up. You need to go back to sign in after a user signed up.
@powerful23 I also have problems with persisting auth. After I call 'signIn' on my following code, i get the 'not authenticated' error back - but the User is logged in and able to make requests and gets correct responses from aws. After reload the page (without cookie delete), the user is logged out (my 'isLoggedIn' calls Auth.currentAuthenticatedUser() - and rejects too - not authenticated).
In [email protected] it works (with Auth.currentSession() ) flawlessly.
constructor() {
Amplify.configure({
Auth: {
identityPoolId: process.env.REACT_APP_AWS_IDENTITY_POOL_ID,
region: process.env.REACT_APP_AWS_REGION,
userPoolId: process.env.REACT_APP_AWS_USER_POOL_ID,
userPoolWebClientId: process.env.REACT_APP_AWS_APP_CLIENT_ID,
mandatorySignIn: true,
cookieStorage: {
domain: process.env.REACT_APP_AWS_DOMAIN,
path: '/',
expires: 365,
secure: true
}
}
});
}
signIn(email: string, password: string): Promise<AuthEntity> {
Auth.signIn(email, password)
.then(user => {
Auth.currentAuthenticatedUser().then((session) => {
console.log(session);
}).catch((error) => {
console.log(error);
});
console.log(user);
}).catch(error => {
console.log(error);
});
}
@Larsrdev can you try Auth.currentUserPoolUser()
to check if the current user is stored in your cookie?
No there isn't a user stored in my cookies. Do I have to configure something else to store the session?
currently tried on @0.4.0
Edit for clearer answer:
This code:
signIn(email: string, password: string): Promise<AuthEntity> {
Auth.signIn(email, password)
.then(user => {
console.log(user);
Auth.currentUserPoolUser().then((session) => {
console.log(session);
}).catch((error) => {
console.log(error);
});
console.log(user);
}).catch(error => {
console.log(error);
});
}
returns following log:
CognitoUser聽{username: "8ea0c53c5eced70667990bbXXXXXXXXXXXX",
pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession,聽鈥Session: nullauthenticationFlowType: "USER_SRP_AUTH"client: Client聽{endpoint: "https://cognito-idp.eu-central-1.amazonaws.com/", userAgent: "aws-amplify/0.1.x js"}pool: CognitoUserPool聽{userPoolId: "eu-central-1_ECXXXXXXX", clientId: "7r748tb8XXXXXXXXXXXXXX", client: Client, advancedSecurityDataCollectionFlag: true, storage: CookieStorage}signInUserSession: CognitoUserSession聽{idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 1}storage: CookieStorage聽{domain: "dev.sXXXXX.com", path: "/", expires: 365, secure: true}username: "8ea0c53c5eceXXXXXXXXXXXXXXXXXXX"__proto__: Object
CognitoService.ts:36
**No current user in userPool**
@Larsrdev
I am also running into the same issue. I have noticed that if I remove the cookieStorage configuration I am able to successfully get a user back from the Auth.currentUserPoolUser().
Yea man you are right. Tried this too after I read this Issue: https://github.com/aws/aws-amplify/issues/841. I works.
@tnortman Hi Man, you save me a lot of time. I am stucking here for a long time and have to go back to use the aws-cognito-js && aws-sdk library in my angular project. The aws-amplify document said and what it showed are so different. It was very confusing. no current user, no sign status persisting,
Remove the COOKIE SETTING in the Amplify.configure(), all good to go.
Thanks~
I am running into this issue without having Cookie Storage in my config file.
The config looks like this:
const awsmobile = {
aws_project_region: ****,
aws_cognito_region: ****,
aws_user_pools_id: ****,
aws_user_pools_web_client_id: ****
};
export default config;
My code looks like this:
const signedInUser = await Auth
.signIn(username, password);
const authUser = await Auth.currentAuthenticatedUser(); // throws error not authenticated
I had this error simply because I had some additional logic inside onAuthUIStateChange( ... ) before setting the user state.
Most helpful comment
I am running into this issue without having Cookie Storage in my config file.
The config looks like this:
My code looks like this: