AWS Cognito Hosted UI -- How to use the returned JWT token?

Viewed 1040

I am using Amazon Cognito and its hosted UI to help create a web application. I am able to use to log in using the hosted UI and the redirect link successfully points me to where I want it to go. I also recieve a correctly formatted JWT token on redirect.

I've also successfully parsed that JWT token into a JS object and am able to verify its validity. With the correct username, expiration time, issue time, etc., etc.

The problem I'm having now is that I simply don't know where to go from here. Is the fact that I have that JWT enough to continue on and assume that the user is valid? If I want to be able to persist the user across different webpages on the site, is it safe to store the JWT -- or, at least, parts of it -- in session storage and act as if that is valid user?

If not, then what am I supposed to do next with the JWT? I have looked through many of the various articles on using the JWT with Cognito and they all seem to be about decoding the JWT, but not about what I'm supposed to do with the JWT once I have it decoded. For context, all I am really trying to get from the JWT is the correct user name, I am planning on storing a lot of other important user details elsewhere.

I'm sorry if there is something very obvious I'm missing about where to go next, but I'm very new to all this AWS stuff and a lot the of documentation is confusing, and not beginner friendly. Thank you for your time, any help at all is greatly appreciated.

EDIT: I wanted to include that I am writing the log in / verification / redirect code on vanilla, client-side javascript. I am using the standard aws SDK, the aws-cognito SDK, and the amazon-cognito-identity SDK. My imports look like this:

   <script src="https://sdk.amazonaws.com/js/aws-sdk-2.686.0.min.js"</script>
   <script src="../../aws-cognito/dist/amazon-cognito-auth.min.js" type="text/javascript"></script>
   <script src="../../amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"></script>
1 Answers

my advise for you would be to look into the AWS Client Side SDK which handle the storing, refreshing and other nice functionality for you.

https://docs.amplify.aws/start/q/integration/js - Main Client side SDK.

https://github.com/amazon-archives/amazon-cognito-auth-js - older SDK used for Cognito Oauth endpoints/ Hosted UI.

Both SDK's will store the result from your HostedUI authentication is local storage in the browser. There you can check if the tokens are valid and even access them if you require to send them to an authorized resource server.

This of course can all be implemented without the use of those SDK's but they help and can give you poinnters. Additionally, the AWS SDK and Cognito identity SDK do not have any functionality that will help you integrate with the HostedUI and Oauth endpoints. That can be done using the SDK's provided above.

Related