AWS Cognito Correct User Flow

Matt M :

I'm trying to migrate my PHP based system to AWS but the biggest thing I'm struggling with is user management/authentication side of things. I've made API Gateways and Lambda functions to get data from my RDS instance and I can use API key or ID token for authentication to protect the data they generate. What I'm really struggling with is the Cognito side of things. In my head, the system would work as follows?

  1. User goes to my website and clicks the login button.
  2. User is redirected to the hosted cognito UI and logs in.
  3. The user is redirected to the chosen page and the id token is displayed in the URL
  4. I use JavaScript to store the ID token in local storage (not sure this is the right way)
  5. I can then pull the ID token out and use it in SDK to run and authenticate my APIs

In PHP I would check the session existed before loading the page to prevent users who aren't logged in accessing my system if the session didn't exist they would be redirected to the login page. How do I do this with Cognito/JWT, do I need to verify the JWT with a Lambda function at the top of every HTML page for my site? I basically want to only allow users who are signed in via Cognito user pool to have access to all the HTML, js, CSS files of my system, except the home page.

Robert Moskal :

It sounds like you have most of the cognito stuff worked out.

If you are calling the api gateway from the web client, you are most likely passing that jwt token in the header and those api calls will fail before the user logs in.

If you are using a client side framework (React, Vue, etc), then you probably have access to a router that renders various UI components. This works analgously to what you do on the server in php, and it's pretty easy to lock down certain routes or the rendering of certain components based on a user being logged in. Doing it without a framework will probably require a significant amount of effort.

You can also stand up a php server, validate the jwt token and do what you normally do for authenticated users.

Or you could take the hyper-modern, serverless approach. You could host your html/css in an S3 bucket and put a Cloudfront distribution in front of that. You could write some Lambda@Edge functions to enforce permissions on the content in the S3 bucket.

Likely, if you are creating an api based javascript application you won't need to protect your markup/css.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=293564&siteId=1