# Trouble trying to use auth + cognito

**URL:** <https://forum.serverless.com/t/trouble-trying-to-use-auth-cognito/3184>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [November 24, 2017, 2:40pm UTC](https://forum.serverless.com/t/trouble-trying-to-use-auth-cognito/3184 "2017-11-24T14:40:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafinha2k](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rafinha2k/32/1327_2.png) [@rafinha2k](https://forum.serverless.com/u/rafinha2k)\
**Post date:** [November 24, 2017, 2:40pm UTC](https://forum.serverless.com/t/trouble-trying-to-use-auth-cognito/3184/1 "2017-11-24T14:40:14Z")

</div>

Hi guys, I’m, having some trouble to make cognito + facebook work with my lambdas, not sure what I’m doing wrong.

**I’m using the serverless offline plugin to test**

1. Get token from Facebook API
2. Store token in localStorage
3. Send the request to /list with the header Authorization
4. Auth lambda function validades the token, and return if the user can execute the function.

- So far, everything is working, the problem happens, when I change the token, to force an error.
- So, token changed to something wrong, and the auth lambda return deny (OK perfect)
- If i change back the token to the correct one, and send a request, my backend always return:

> Missing credentials in config

So I need stop my offline server, and restart again to make it works again.

Why? What’s is wrong? ☹

Here is my code:

Backend  
serverless.yml

```
...
functions:
  auth:
    handler: auth.main
  
  list:
    handler: list.main
    events:
      - http:
        path: list
        method: get
        cors: true
        authorizer: auth

```

auth.js

```
'use strict';
const AWS = require('aws-sdk');

module.exports.main = (event, context, callback) => {
  const generatePolicy = (principalId, effect, resource) => {
  const authResponse = {};

  authResponse.principalId = principalId;

  if (effect && resource) {
    const policyDocument = {};

    policyDocument.Version = '2012-10-17';
    policyDocument.Statement = [];

    const statementOne = {};

    statementOne.Action = 'execute-api:Invoke';
    statementOne.Effect = effect;
    statementOne.Resource = resource;
    policyDocument.Statement[0] = statementOne;
    authResponse.policyDocument = policyDocument;
  }

  return authResponse;
};

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'us-east-1:lalala',
  Logins: {
    'graph.facebook.com': event.authorizationToken
  }
});

AWS.config.credentials.get((err) => {
  if (err) {
    callback('Unauthorized');
    return false;
  }

  callback(null, generatePolicy('user', 'Allow', event.methodArn));
});
}

```

Thanks guys 🙂
