Last active
April 7, 2023 06:40
-
-
Save ckimrie/633c0fc2961f6f2db43c8e78c1af3ba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-----------------------------------------------// | |
//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