Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckimrie/633c0fc2961f6f2db43c8e78c1af3ba4 to your computer and use it in GitHub Desktop.
Save ckimrie/633c0fc2961f6f2db43c8e78c1af3ba4 to your computer and use it in GitHub Desktop.
//-----------------------------------------------//
//On page load
// - Browser -
//-----------------------------------------------//
AWS.config.update({
region: "eu-west-1"
});
var credConfig = {
IdentityPoolId: "XXXXX-XXX-XXX"
Logins:{}
};
//Do we have a token stored from a previous login?
if(localStorage.getItem('Token')){
//If using Federated Logins, use `cognito-identity.amazonaws.com`
credConfig.Logins["cognito-identity.amazonaws.com"] = localStorage.getItem('Token');
}
AWS.config.update({
credentials: new AWS.CognitoIdentityCredentials(credConfig)
});
//-----------------------------------------------//
//Upgrade local CognitoIdentityCredentials after successful
//Login with Lambda & Federated Identities
// - Browser -
//-----------------------------------------------//
onLoginApiResponse(apiResponse) {
AWS.config.update({
credentials: new AWS.CognitoIdentityCredentials({
IdentityId: apiResponse.IdentityId,
Logins: {
"cognito-identity.amazonaws.com": apiResponse.Token
}
})
});
//Save the retrieved Token for next page refresh
localStorage.setItem('Token', apiResponse.Token);
}
//#############################################################################//
//-----------------------------------------------//
//Example Lambda Cognito Identity upgrade after
//successful login
// - Lambda -
//-----------------------------------------------//
exports.handler = function(event, context, callback){
//Some login function
DoSomeSortOfLogin();
//Success...
var cognitoidentity = new AWS.CognitoIdentity();
var cognitoParams = {
IdentityPoolId: "XXXXX-XXX-XXX",
Logins: {},
TokenDuration: 7200
};
//User Id from our local DB
cognitoParams.Logins["developer.provider.name.set.in.cognito.config"] = "123456";
//Use current cognito identity if available
if(context.identity && context.identity.cognitoIdentityId){
cognitoParams.IdentityId = context.identity.cognitoIdentityId;
}
//Get a Token from Cognito so that the browser can upgrade to an authorised user
cognitoidentity.getOpenIdTokenForDeveloperIdentity(cognitoParams, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment