Created
August 4, 2020 17:18
-
-
Save dtelaroli/78f8a17afceed3e7d398929f0fbbf950 to your computer and use it in GitHub Desktop.
How to get identity id from cognito with jwt token
This file contains hidden or 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
// npm install await-to-js | |
const { to } = require("await-to-js"); | |
const { CognitoIdentity } = require("aws-sdk"); | |
const identity = new CognitoIdentity(); | |
const IDENTITY_POOL_ID = "<your identity pool id>"; | |
exports.handler = async (event) => { | |
const { issuer } = event.identity; | |
const { authorization } = event.request.headers; | |
const [error, identityId] = await getId(authorization, issuer); | |
} | |
const getId = async (jwtToken, issuer) => { | |
const [error, result] = await to( | |
identity | |
.getId({ | |
IdentityPoolId: IDENTITY_POOL_ID, | |
Logins: { [issuer.replace("https://", "")]: jwtToken }, | |
}) | |
.promise() | |
); | |
if (error) return [error]; | |
return [null, result.IdentityId]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! This was really helpful!