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
- Get token from Facebook API
- Store token in localStorage
- Send the request to /list with the header Authorization
- 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