next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Integration with custom AWS cognito/ amplify

Open hamidre13 opened this issue 5 years ago • 27 comments

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

hamidre13 avatar Aug 13 '21 19:08 hamidre13

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.

balazsorban44 avatar Aug 13 '21 19:08 balazsorban44

For sure. you can use Cognito in two ways.

  1. Hosted login pages
  2. 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: IMG_7763

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

hamidre13 avatar Aug 13 '21 19:08 hamidre13

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

balazsorban44 avatar Aug 16 '21 14:08 balazsorban44

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 .

hamidre13 avatar Aug 17 '21 01:08 hamidre13

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!

stale[bot] avatar Oct 16 '21 01:10 stale[bot]

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!

stale[bot] avatar Oct 23 '21 02:10 stale[bot]

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 🙂

chan-vince avatar Jan 18 '22 00:01 chan-vince

I think this issue should be re-opened, since it sounds like a feature request that should be in scope.

nk9 avatar Feb 12 '22 20:02 nk9

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!

yp717 avatar Mar 18 '22 18:03 yp717

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.

nk9 avatar Mar 18 '22 21:03 nk9

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?

yp717 avatar Mar 19 '22 00:03 yp717

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.

nk9 avatar Mar 19 '22 12:03 nk9

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?

joseph-mccombs avatar Mar 23 '22 19:03 joseph-mccombs

Yep, on Stack Overflow.

nk9 avatar Mar 23 '22 22:03 nk9

@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

zul-yang avatar Aug 26 '22 06:08 zul-yang

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.

nk9 avatar Aug 26 '22 08:08 nk9

This not being possible is a bit limiting for the use cases of this library w/ cognito

hdntc avatar Apr 13 '23 00:04 hdntc

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.

zarif-al avatar Apr 30 '23 09:04 zarif-al

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

Lagyu avatar May 08 '23 05:05 Lagyu

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 ?

Its-suLav-D avatar Aug 30 '23 19:08 Its-suLav-D