Integration with custom AWS cognito/ amplify
Description 📓
We love next-auth and also AWS Cognito, but the hosted UI for AWS Cognito is ugly. As of now we could not find an easy way to have our own custom UI for AWS Cognito that can also integrates with next-auth after login. That would be great to have API to support that.
How to reproduce ☕️
N/A
Contributing 🙌🏽
Yes, I am willing to help implement this feature in a PR
Could you elaborate more on what you would expect? That is a very vague description, and I don't have any experience with Cognito myself.
You can create your own sign in page if that is helpful, just use a button with signIn("cognito") and that will log you directly into Cognito.
For sure. you can use Cognito in two ways.
- Hosted login pages
- use your own custom UI with the help of amazon-cognito-identity-js or aws-amplify package
With next Auth and signIn("cognito") . you will be redirected to an ugly plage like this:

This page is the hosted login page for AWS Cognito and has very limited customization capability. This solution will not work if we want to host our own login/logout page and have control over all the design.
Now if we want to have control over the design we will have to use the packages that I mentioned above. Now after login we like to give the session info to the next auth to handle the rest for us instead of us taking care of session management, URL management,...
Basically we can use those package to login but we want to handle to token and user info to next-auth after that
I don't think that is within the scope of this project, unfortunately. We don't have control over the page you are redirected when clicking signin, only the result coming back from there to our /api/auth/callback/cognito endpoint, where we pick up the flow and finish the process.
you will have to create that page yourself, but I assume it would be alright to do it in a Next.js page. you could then define that page as the authorizeUrl in your provider config
AWS lets you either use the hosted sing up page or use an npm package (amplify, aws-conito) to the signup/sign in. We can definitely design the signup/sing in page but we like to then hand over our access token and refresh token to next-auth. Thus , what we are looking for is not and actual page design but an API in back end to tell next-auth that the user is signed in with following access, and refresh tokens .
Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep it open. (Read more at #912) Thanks!
Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!
AWS lets you either use the hosted sing up page or use an npm package (amplify, aws-conito) to the signup/sign in. We can definitely design the signup/sing in page but we like to then hand over our access token and refresh token to next-auth. Thus , what we are looking for is not and actual page design but an API in back end to tell next-auth that the user is signed in with following access, and refresh tokens .
Did you end up getting anywhere with solving this? I'm looking for something that's exactly what you describe 🙂
I think this issue should be re-opened, since it sounds like a feature request that should be in scope.
I also think this issue should be re-opened...it seems quite heavy to have to pull in Amplify just to customise the UI of an app that otherwise has nothing to do with Amplify and only uses Cognito + next/auth.
The benefits next/auth provide still make it one of the most convenient ways to add auth to a next app with minimal boilerplate and a feature like this could help use it in the context of aws/cognito/next!
I obviously agree. However, I will say that I ultimately dropped NextAuth and switched to Amplify UI. After some stumbling around, I got it working quite well.
While I'd prefer the light weight NextAuth if all I needed was a simple sign in/sign up/logout flow, I needed Cognito's support for MFA using TOTP and SMS tokens. I don't believe NextAuth would have supported that, at least not out of the box, whereas Amplify UI's Authenticator component supports this flow automatically. It even includes a QR code to set up the TOTP app (e.g. Authy).
So I'd just urge people to fully understand their requirements before picking a library to interact with their IdP.
Yeah that makes sense 😊 you can use some amplify features to interface with cognito from the core and auth modules. Do you know why you need to use the amplify cli setup for the UI components but not to use the auth stand alone?
Do you know why you need to use the amplify cli setup for the UI components but not to use the auth stand alone?
Not sure what you mean, exactly. I am using Amplify UI without the CLI.
I also agree that this should be opened... I also am trying to add a custom template due to Cognito's clunky UI, so that we can support a custom flow involving inviteCodes. All we can currently use is the template provided by Cognito. @nk9 do you have an example of using amplify with cognito you could point me to?
Yep, on Stack Overflow.
@nk9 , apologies for reviving an old issue. When using Amplify UI, are you limited to storing the tokens in localstorage as well? I remember being this an issue: https://github.com/aws-amplify/amplify-js/issues/3224
I am not sure, I'm just using Amplify UI's Authenticator component. It's taking care of any cookie management and storage itself, and I just fetch the tokens when needed using the API.
This not being possible is a bit limiting for the use cases of this library w/ cognito
Hi!. We also came across this issue while trying to use next-auth with aws-cognito.
Currently, instead of using CognitoProvider we are using the CredentialsProvider paired with aws-amplify. I am not sure if this is the best way, but its working for us so far. We can have a custom sign-in experience while using cognito.
This can be another sample implementation with CredentialsProvider.
They use CognitoIdentityProviderClient to authorize users.
https://github.com/cds-snc/platform-forms-client/blob/develop/pages/api/auth/%5B...nextauth%5D.ts
AWS lets you either use the hosted sing up page or use an npm package (amplify, aws-conito) to the signup/sign in. We can definitely design the signup/sing in page but we like to then hand over our access token and refresh token to next-auth. Thus , what we are looking for is not and actual page design but an API in back end to tell next-auth that the user is signed in with following access, and refresh tokens .
Did we make any progress on this ?