Skip to content

Instantly share code, notes, and snippets.

@dtelaroli
Created August 4, 2020 17:18
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 dtelaroli/78f8a17afceed3e7d398929f0fbbf950 to your computer and use it in GitHub Desktop.
Save dtelaroli/78f8a17afceed3e7d398929f0fbbf950 to your computer and use it in GitHub Desktop.
How to get identity id from cognito with jwt token
// 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];
};
@AidanHanda
Copy link

Thank you! This was really helpful!

@dtelaroli
Copy link
Author

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment