0

I am trying to add AWS Cognito with Google and Facebook login. Somehow the current plugins are available not working with my current setup and I am not able to get it working for a few days now. After adding multiple bug https://github.com/agnostech/flutter_amplify/issues/4 and https://github.com/furaiev/amazon-cognito-identity-dart-2/issues/42 with no help so far. So I thought to make a get/post request to lambda with the access token (As I am login with Facebook and google but not able add a user to Cognito) and that would add the user.

AWS.config.region = 'us-XXXXXXX-1';
// Add the Google access token to the Cognito credentials login map.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-XXXX-1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    RoleArn: 'arn:aws:iam::XXXXXXXX:role/Cognito_XXXXXXXXXUnauth_Role',
    Logins: {
        'accounts.google.com': authResult['id_token']
    }
});

I am following this AWS Cognito + google signup but is there a better way to do this on the current date? Also each time user logins will it try to add the user to Cognito? Considering we have only one button for sign it or sign up.

Thanks in advance.

Subhendu Kundu
  • 3,618
  • 6
  • 26
  • 57

1 Answers1

3

I just read this guide to have google auth with cognito. https://docs.aws.amazon.com/cognito/latest/developerguide/google.html

It is quite simple, you have to login with google using regular way, as soon as you receive access token from google you pass it to cognito.

logins.put("accounts.google.com", token); credentialsProvider.setLogins(logins);

Package way,

There is a plugin for this, Look at the one of the use cases in the guide here. https://pub.dev/packages/amazon_cognito_identity_dart_2

Quoting the doc

Use case 19. Using this library with Cognito's federated sign in on mobile devices. Use flutter_webview (https://pub.dev/packages/webview_flutter) to navigate to Cognito's authorize URL. Use flutter_webview's navigationDelegate to catch the redirect to myapp://?code=<AUTH_CODE>. Make a POST request to Cognito's token URL to get your tokens. Create the session and user with the tokens

prasadsunny1
  • 775
  • 6
  • 15
  • So completely agree, on the using webview, I was avoiding to code natively(main reason would be I don't know about it, but I can try). Now package way, well I did try amazon_cognito_identity_dart_2, flutter_amplify, flutter_cognito_plugin (both gave the same error) probably that's why I thought it's an issue with setting up amplify. Coming from ReactNative background where I did publish a plugin for google login using webview https://github.com/subhendukundu/react-native-simple-google-login so I guess I can manage that. Then again the idea was to do it flutter way. :) Thanks – Subhendu Kundu Mar 31 '20 at 21:59
  • Hey im having a problem with google sign in using the package you recommended. Here is the code https://pastebin.com/M6JgcTV9 but basically when im pressing the button it is not showing anything and im trying to do google sign in using the package – LearnFlutter Jun 06 '22 at 09:09