Using Facebook Login, why does Amplify (using Auth.currentAuthenticatedUser()) not work immediately after login, but if I refresh the app (Command + R in the iOS simulator), the user is recognized?
See original GitHub issue** Which Category is your question related to? ** Amplify Facebook Login Integration
** What AWS Services are you utilizing? ** Amplify, Auth
** Provide additional details e.g. code snippets **
code is now working - here’s the snippet
import {
CognitoUser,
CognitoIdToken,
CognitoAccessToken,
CognitoRefreshToken,
CognitoUserSession,
CognitoUserPool } from 'amazon-cognito-identity-js';
//import { withOAuth } from 'aws-amplify-react-native'
const userPool = new CognitoUserPool({
UserPoolId: config.aws_user_pools_id,
ClientId: config.aws_user_pools_web_client_id,
});
import { AuthSession } from 'expo'
// Open URL in a browser
openURL = async (url) => {
try {
let result = await AuthSession.startAsync({ authUrl: url })
this.getTokenbyCode(result.params.code)
} catch(openURLError) {
console.log(`openURLError: ${openURLError}`)
}
}
getTokenbyCode = async (code) => {
const details = {
grant_type: 'authorization_code',
code,
client_id: userPool.clientId,
redirect_uri: AuthSession.getRedirectUrl()
}
const formBody = Object.keys(details)
.map(
key => `${encodeURIComponent(key)}=${encodeURIComponent(details[key])}`
)
.join("&");
try {
let res = await fetch(
'https://DOMAIN.auth.us-west-2.amazoncognito.com/oauth2/token',
{
method: "POST",
headers: {
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
}
)
let tokenRequestJson = await res.json();
const IdToken = new CognitoIdToken({ IdToken: tokenRequestJson.id_token });
const AccessToken = new CognitoAccessToken({ AccessToken: tokenRequestJson.access_token });
const RefreshToken = new CognitoRefreshToken({ RefreshToken: tokenRequestJson.refresh_token
})
try {
let userSession = new CognitoUserSession({ IdToken, AccessToken, RefreshToken });
const userData = {
Username: IdToken.payload['cognito:username'],
Pool: userPool
};
let authUser = Auth.createCognitoUser(userData.Username); // THIS NOW WORKS
let authSession = await Auth.userSession(authUser)
let authCurrentUser = Auth.currentAuthenticatedUser()
} catch (userError) {
//HANDLE USERERROR
}
} catch (fetchError) {
//HANDLE FETCHERROR
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
AWS Amplify currentAuthenticatedUser is null after FB ...
I have been able to implement successfully the Auth.signIn() method, and I can consistently retrieve the user using Auth.
Read more >Facebook login not working iOS 10
I am having an issue with an app and logging in with Facebook. My app uses this method: FBSDKLoginManager logInWithReadPermissions:<#(NSArray *)#> ...
Read more >Extending IBM Business Process Manager to the Mobile ...
2.5.1 Worklight Studio and command-line interface for IBM Worklight Developers. ... 7.4.3 Configuring the Worklight mobile app to authenticate using LTPA.
Read more >Facebook Login Using AWS Amplify and Amazon Cognito
With Facebook Login integration, instead of creating a new account via Cognito User Pools, the same user can simply choose the Facebook Login...
Read more >AWS amplify add auth: how to add a redirect signin URI ...
Accepted answer. I don't know what problem you are having, but you can use amplify update auth. If you try: amplify add auth....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cmaronchick I’ve been really struggling with oauth amplify for expo as well. If it’s not too much to ask can you please help me with your working code? I can’t seem to get it work with Fb login or Google login 😦
Thanks. I figured this out, and you should update your documentation.
this line is incorrect: let authUser = Auth.createCognitoUser(userData); It should read this: let authUser = Auth.createCognitoUser(cognitoUser.getUsername());
Regarding your documentation, it is not at all straightforward. I tried it out and got an error saying that response.token is undefined.
Since my solution is working (I’m using Expo FWIW), I’m going to stick with the hack, unless you’d like to update your documentation to make it easier.