AWS Cognito login with Facebook

2.2k views Asked by At

I have an Angular 10 site. I have an AWS Lambda (ASP.NET Core 3.1) that does authentication with AWS Cognito for users with email/password. But I want to allow users to also use Facebook (and eventually Google) to log in as well. In the Facebook/Google scenario my thought is not to allow access to AWS services directly for now (like S3, etc) but to interact with with my other lambda via a bearer token. I have a Cognito User Pool for which I created a Facebook identity provider and mappings. I read somewhere that I'd need an Identity Pool. So I created that and put in my Cognito user pool as a provider as well as Facebook.

Using the JavaScript code:

loginWithFacebook = () => {
    const login$ = from(this.facebookService.login());
    login$.subscribe(
      (response: LoginResponse) => {
        console.log(response);
        this.facebookLoginToAWS(response);
      },
      error => {
        console.error(error);
      }
    );
  };

I can get a Facebook auth response no problem. Then using this code that you see on every blog, Stack Overflow post, and even in AWS's documentation (of course, substituting my own IdenityPoolId):

private facebookLoginToAWS = (facebookResponse: LoginResponse) => {
    console.log('facebookLoginToAWS', facebookResponse);

      if (facebookResponse.status === 'connected' && facebookResponse.authResponse) { 

        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
          IdentityPoolId: 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
          Logins: { 'graph.facebook.com': facebookResponse.authResponse.accessToken }
        }, { 
          region: 'eu-west-1'
        });

        AWS.config.credentials.get((err) => {
          if (err) {
            return console.log("Error", err);
          }

          console.log("Cognito credentials", AWS.config.credentials);
          console.log("Cognito Identity Id", AWS.config.credentials.identityId);
        });

      } else if (facebookResponse.status === 'not_authorized') {
        document.getElementById('facebookStatus').innerHTML = 'Please log into this app.';
      } else {
        document.getElementById('facebookStatus').innerHTML = 'Please log into Facebook.';
      }
  };

I can get back a session token (in addition to a ton of other stuff like accesKeyId, identityId, and secretAccessKey).

But what do I do with that session token? Perhaps I'm confused, but I would think because there is a mapping between Facebook fields and Cognito fields, that somehow that Facebook user would be migrated into the Cognito user pool and I could get a JWT token for that user for my other lambdas. But after checking the AWS dashboard, I can see a log in (I think) in the Identity Pool but there is no corresponding User Pool entry.

enter image description here

Do I somehow have to manually migrate it over (using the AWS JavaScript SDK)? I don't want to use the Amplify library. Am I thinking about it wrong? Do I somehow use the session token as a bearer token with my other lambdas? Do I need to add that person as a user pool user somehow?

0

There are 0 answers