Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facebook token, now whats next #12

Open
vvavepacket opened this issue Jun 27, 2017 · 19 comments
Open

facebook token, now whats next #12

vvavepacket opened this issue Jun 27, 2017 · 19 comments

Comments

@vvavepacket
Copy link

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
Copy link
Author

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?

@jmparsons
Copy link
Contributor

@vvavepacket Sounds like you have a question like this:

amazon-archives/amazon-cognito-identity-js#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.

@spoeck
Copy link

spoeck commented Jun 28, 2017

@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
Copy link

spoeck commented Sep 14, 2017

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?

@jmparsons
Copy link
Contributor

@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.

@MatheusParanhos
Copy link

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

@jmparsons
Copy link
Contributor

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

@MatheusParanhos
Copy link

@jmparsons That would be sensational. Pleease someonee

@frangeris
Copy link

@jmparsons Any news? 🙏

@jmparsons
Copy link
Contributor

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

amazon-archives/amazon-cognito-identity-js#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.

@nineonine
Copy link

this feature would be really nice to have! 💪🏻

@joe455
Copy link

joe455 commented Dec 14, 2017

Can somebody found the solution? :'(

@RaphiStein
Copy link

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??

@panuhorsmalahti
Copy link

panuhorsmalahti commented Feb 8, 2018

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.

@leesolway
Copy link

@RaphiStein Any luck?

@sdenev
Copy link

sdenev commented Mar 13, 2018

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);
    });

@offaxis
Copy link

offaxis commented May 25, 2018

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 ?

@canhnht
Copy link

canhnht commented Jun 6, 2018

@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.

@offaxis
Copy link

offaxis commented Jun 6, 2018

@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 => amazon-archives/amazon-cognito-auth-js#62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests