react-native-aws-cognito-js icon indicating copy to clipboard operation
react-native-aws-cognito-js copied to clipboard

facebook token, now whats next

Open vvavepacket opened this issue 7 years ago • 19 comments

Let's say we want users to sign in into our app. There are 2 ways:

1.) Signup, and login (this will create an account in User Pool) 2.) Facebook login (user will just login to his facebook account)

Now, 1.) works fine, I'm able to have users signup and login. I am having problem in 2.). I was able to get the token from facebook (I am able to integrate facebook login button in my react native app)... Now after we get the token,, I want my users to use this facebook login and just continue using the app.

How do we achieve this? Are we supposed to create a new user in UserPool programatically if users login via facebook? Or we dont have to create the account manually (I assume that once the user logins using his facebook account, the next time he will use the app, he will be remembered)?

My point of confusion is, there is no cognitoUser if we are using federated identities or facebook. Please help me clear this confusion and what would be the best thing to proceed for number 2.

vvavepacket avatar Jun 27 '17 23:06 vvavepacket

I understand the after facebook returns a token, we can use that to access AWS resources like S3, DynamoDB, etc... But what if i want my facebook user to maintain an account on my application (storing all his activity, points, etc,, similar to a traditional user who signed up without facebook).. Does that mean we shall create a new account in UserPool using his facebook details?

vvavepacket avatar Jun 27 '17 23:06 vvavepacket

@vvavepacket Sounds like you have a question like this:

https://github.com/aws/amazon-cognito-identity-js/issues/353

This library is a direct port amazon cognito identity js, but with just the SRP_A built using a bridge and with memory storage async. Basically the same exact library just speed boost for SRP_A, so any linking of accounts should be the same.

Also found these:

https://aws.amazon.com/blogs/mobile/announcing-your-user-pools-in-amazon-cognito/

http://docs.aws.amazon.com/cognito/latest/developerguide/synchronizing-data.html

https://stackoverflow.com/questions/36780994/how-to-combine-cognito-user-pools-with-external-providers-like-facebook

I'm currently working around the clock on my job, so I haven't had the time to put together a Facebook example and test it out. We are just using the User Pools with Cognito Federated Identities.

jmparsons avatar Jun 28 '17 01:06 jmparsons

@vvavepacket

If a user is logged in via FB and you got a aws session token, the user has access to several aws services with this token, but just temporary and no extra user was created. In our app we are solving this problem this way that a normal cognito user will be created if a user is logging in via FB. The cognito user has the FB-UserId as name. Everytime if he will logging in again, we check if he is existing. If he is already existing we load the credentials of the associated cognito user.

spoeck avatar Jun 28 '17 10:06 spoeck

Cognito adds Social Identity Providers for UserPools (11 August): capture

Here is a link to the docs: http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-social.html But there isn't any code example yet.

@jmparsons do you have an idea if it can work with your lib?

spoeck avatar Sep 14 '17 11:09 spoeck

@spoeck I'd check with the guys who write the lib over at https://github.com/aws/amazon-cognito-identity-js/ this is just a react native port.

jmparsons avatar Sep 15 '17 17:09 jmparsons

Sorry to ressurect this one, but I'm having the same issues. I get the token from the login but where do I go from there? Help solve this one guys I plan to make a pull request after

MatheusParanhos avatar Oct 04 '17 19:10 MatheusParanhos

@MatheusParanhos I haven't had time to look into it. Hopefully someone else can chime in here.

jmparsons avatar Oct 05 '17 20:10 jmparsons

@jmparsons That would be sensational. Pleease someonee

MatheusParanhos avatar Oct 10 '17 17:10 MatheusParanhos

@jmparsons Any news? 🙏

frangeris avatar Oct 12 '17 21:10 frangeris

I would say look through the amazon cognito identity issues such as this one:

https://github.com/aws/amazon-cognito-identity-js/issues/353

My 9 to 5 is more like a 9 to 9 and I'm on tight deadlines, so I can't build out fb and test it right now.

jmparsons avatar Oct 13 '17 00:10 jmparsons

this feature would be really nice to have! 💪🏻

nineonine avatar Oct 24 '17 08:10 nineonine

Can somebody found the solution? :'(

joe455 avatar Dec 14 '17 13:12 joe455

The AWS docs are absolutely terrible. The partial code samples are really unhelpful. They show how to get the FB token, which you can also find from FB docs, but AWS docs don't show what you need to do with the FB token. How do you turn it into Cognito user??

RaphiStein avatar Jan 07 '18 07:01 RaphiStein

My understanding is that having a Facebook login to create an user in user pool you need to use a hosted page using this library: https://github.com/aws/amazon-cognito-auth-js - and more specifically the relatively new "Federated Identities" feature.

Unfortunately this library is designed for web apps, not react native. Using the hosted page in a new browser app probably would create the user, but I don't see how I can easily read the saved token / user credentials. Another option would be to use WebView, but I also don't see how I can read the token / user credentials back to the main app. Supposedly amazon-cognito-auth-js saves them to the localStorage, but this is not enabled in react native's WebView.

One more thing: the Facebook token can be used to fetch AWS credentials (and there are examples for this), but this seems different from integrating with an User Pool.

panuhorsmalahti avatar Feb 08 '18 10:02 panuhorsmalahti

@RaphiStein Any luck?

leesolway avatar Mar 13 '18 16:03 leesolway

I managed to crack this after several hours. Only a PoC so far, but logs the records in the console.

Here is what works for me:

// AWS.js
import AWS from 'aws-sdk/dist/aws-sdk-react-native';
import 'react-native-aws-cognito-js';
/**
 * ATTENTION - I had to change the require(aws-sdk) in amazon-cognito-js source as described on
 * https://github.com/AirLabsTeam/react-native-aws-cognito-js/issues/2
 */
import 'amazon-cognito-js';
// Configuration consts from .env
import { AWS_COGNITO_REGION, AWS_COGNITO_POOL_ID, AWS_COGNITO_DATASET, AWS_COGNITO_RECORD } from 'react-native-dotenv';

AWS.config.region = AWS_COGNITO_REGION;

// TODO: it's only a PoC -> move into a Component with progress indicator
export const sync_cognito = (accessToken: any) => {
  AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: AWS_COGNITO_POOL_ID,
    Logins: {
      'graph.facebook.com': accessToken,
    },
  });

  AWS.config.credentials.get(err => {
    if (err) {
      console.error(err);
    }
    // The CognitoSyncManager DataStore must be memory
    const syncClient = new AWS.CognitoSyncManager({ DataStore: AWS.CognitoSyncManager.StoreInMemory });
    syncClient.openOrCreateDataset(AWS_COGNITO_DATASET, (err, dataset) => {
      if (err) {
        console.error(err);
      }

      dataset.synchronize({
        onSuccess: (data, newRecords) => {
          dataset.get(AWS_COGNITO_RECORD, function(err, value) {
            if (err) {
              console.error(err);
            }

            console.log(value);
          });
        },
        onFailure: err => {
          console.error(err);
        },
      });
    });
  });
};

export default AWS;

Usage in the Component file:

// import the function
import { sync_cognito } from '../../sync/AWS';
// after FB login
AccessToken.getCurrentAccessToken().then(data => {
      sync_cognito(data.accessToken);
    });

sdenev avatar Mar 13 '18 17:03 sdenev

Hi !

Same struggle here !

AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: config.aws.cognito.identityPoolId, Logins: { 'graph.facebook.com': response.authResponse.accessToken } });

this works well but getCurrentUser() returns null and the AWS.config.credentials.sessionToken doesnt' seem to work !

Anybody already did this with Cognito ?

offaxis avatar May 25 '18 13:05 offaxis

@offaxis Because you are login by facebook, not by cognito user pool, so getCurrentUser() will not return anything. I think you should check out this library https://aws.github.io/aws-amplify/ which gives better abstraction over authentication with cognito.

canhnht avatar Jun 06 '18 08:06 canhnht

@canhnht Yes i found it !

I prefered use integrated UI Cognito Login, witch create a user in my user pool, but i got the same problem : getCurrentUser() does not return the user ! see => https://github.com/aws/amazon-cognito-auth-js/issues/62

offaxis avatar Jun 06 '18 08:06 offaxis