Magic Passcode
Your question I'm curious if there's currently a way to have the same behavior as the magic link, but instead send a 4-6 digit code to their email that the user can then enter in themselves on the site.
Also kind of like this but with email code instead of SMS code.
What are you trying to do I'm working on a PWA and a magic link will take you directly to the mobile browser (ex: iOS Safari) instead of directly to the PWA. This is a way to get around that.
Feedback
- [x] Found the documentation helpful
- [ ] Found documentation but was incomplete
- [x] Could not find relevant documentation
- [ ] Found the example project helpful
- [ ] Did not find the example project helpful
Also experience the same user experience issue - another way to solve this would be to handle email auth the same way https://magic.link/ or Vercel do. Rather than opening a new session, the link authenticates the original session.
@glenames I thought about this. I guess I should take a deeper look into that.
@alex-cory Its not possible with the current email auth implementation - its a suggested alternate feature request I guess. I think it's a fair bit trickier to implement because it would require a subscription to the database from the client requesting access.
@glenames @iaincollins How does this flow look?
Also, what is the recommended way of adding the jwt or session token or csrf token to the frontend. I'm not quite sure how next-auth is setting these on the frontend so when calling useSession it will have the correct values to fetch the current session.
- [ ]
/loginfrontend- [ ] type in email, click login/signup - POST
{ email, isAddedToHomescreen }to/api/auth/magic-link - [ ] if isAddedToHomescreen, subscribe to channel
[email protected]- [ ] on trigger event from channel
[email protected]- set JWT from socket body
- [ ] on trigger event from channel
- [ ] type in email, click login/signup - POST
- [ ]
/api/auth/magic-linkPOST- [ ] create/get user with post body.email
- [ ] create JWT with
userId,email,isAddedToHomescreenand whatever else should go into the JWT - [ ] email user with link
/magic-link?jwt=<the-jwt>
- [ ]
/magic-link?jwt=<the-jwt>SSR page- in
getServerSideProps- [ ] deserialize JWT to get email, trigger socket with JWT from query params as the socket body, send to channel
[email protected]
- [ ] deserialize JWT to get email, trigger socket with JWT from query params as the socket body, send to channel
- [ ] frontend
- if
isAddedToHomescreendisplay message saying "you have been logged in, now go back to your app added to your homescreen" - else client side redirect to home page
- if
- in
I'd be interested in helping put a PR together for this.
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!
keep alive
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!
keep alive
Thanks for bumping! Actually yes this is possible, but I don't think was ever documented.
-
A
generateVerificationToken()callback can be used with email Providers. That will generate a unique token - and hash it in the database, so it is not stored in clear text. You can use this in conjunction with a customsendVerificationRequest()callback to send either a custom email or trigger an SMS message.By default if
generateVerificationToken()is not set, then it will generate some random bytes for the token: https://github.com/nextauthjs/next-auth/blob/main/src/server/lib/signin/email.jsAn implementation would look something like this:
Providers.Email({
generateVerificationToken: () => { return "ABC123" },
sendVerificationRequest: ({ identifier: email, url, token, site, provider }) => { /* your function */ }
})
~A caveat is that, unlike all other callbacks, generateVerificationToken is assumed to be synchronous - i.e. it does not use await when it is called, which is a mistake and we should change that.~ (Maintainer edit. It IS called with await since 3.6.0)
-
Finally, you would also want to create a custom page for the
/api/auth/verify-requestroute, as documented here: https://next-auth.js.org/configuration/pagesNote you can actually have multiple providers that work this way (e.g. both Email and a custom one that does SMS) and handle them with pages that render differently, as the 'provider ID' is passed to the verification page, you would just need to set a custom
idproperty on each provider instance.
For more information on customising the email provider, please see: https://next-auth.js.org/providers/email
Additionally, there is also this guide to using SMS for sign in with Everify: https://everify.dev/blog/two-factor-authentication-for-nextjs
This approach uses the credentials callback, which is perfect if you want stateless sign in without a user database - or if you want to use an existing user database in your own way.
I want to have built-in support for sending email codes like this, and potentially even make that the default behaviour for the email provider, as it makes it much easier to support cross-device sign in (e.g. start sign in on desktop, get email on phone, enter code on email and you are done).
I would also still support signing in by just clicking the button in the email; the messaging would just need to be right to not confuse users (as once the button was clicked the token would "used up" so you wouldn't want them clicking it on their phone by mistake).
We would need the built-in verify-request page to look a little different to accommodate short tokens like this (i.e. it would need to have form input elements for the code).
The final consideration is that shorter tokens really ought to have a shorter expiry time. This functionality is currently baked in to each database adapter (in the createVerificationRequest(), getVerificationRequest() and deleteVerificationRequest() methods).
Ideally I would like to see this abstracted out so that it is possible to use the Email provider without a database adapter, if you have these methods defined on the provider, although this is already possible with the existing callbacks (which can be very powerful) as shown in the example blog post.
For your interest, when #1378 is merged, generateVerificationToken will be an async function! 🎉
So #1378 is merged and I think Iain gave a good explanation. Can this be closed @alex-cory?
As said by @iaincollins I need to customize the verify-request page to let the user insert the pin. Once there, what shouold I do with the PIN? My idea is that I have to send it to api/auth/callback/email?email=<EMAIL>&token=<PIN>. Do I have access to email in verify-request page?
@ramiel were you able to solve this?
Yes, I was. The only way is to use the version of signin that does not redirect (https://next-auth.js.org/getting-started/client#using-the-redirect-false-option). That way I remain on the page where the user just inserted their email and I don't loose it. Otherwise, in the callback page there's no way to know the email of the sigin process (which I still think it's a problem and should be fixed in next-auth). Of course I had to handle all the logic to show the email input and the pin input.
Regardless of what is written in this issue, next-auth is not ready to have a login with pin just by customising the generateVerificationToken function.
Just jumping in to say I'd also really appreciate this ability! Currently I'm using sessions + magic links for authentication in my habit tracking project and I'm running into the same problem with magic links.
Here's an example mobile login flow where magic links on iOS are not working as well as I'd hope
- User opens Safari browser, navigates to website and clicks login
- User enters their email to login / create account
- User goes to their email app (gmail in my case) and clicks magic link they received
- gmail opens the link in the Safari (in-app) browser
- note: this browser does not share local storage with the main Safari browser
- authentication is successful in the Safari (in-app) browser
- user goes back to their main Safari browser, where they initiated the login request from
- user refreshes the page and they are still unauthenticated
Just reiterating two possible solutions from above, either of which would be fine with me
- email authentication flow supports using a 6 digit OTP (replaces link button in email) that the user enters on the screen after entering their email and pressing the login button
- the magic link in the email the user receives authenticates the original session it was created from
Thank you for all of the work on this great library, the NextJS development experience has been a breath of fresh air thanks to all the cool libraries like this one :)
OTP is already possible. We implemented it with next-auth at Hypersay Events .
@ramiel I agree it looks possible but I wish it had first party support though the Email provider using a configuration option like method:otp
If you have some time on your hands I'd really appreciate a write up of the steps needed to use OTP for email auth! I see you have a blog, I think this would make an awesome post if you happen to have the time / desire to do one :)
@trentprynn your wish is an order :D https://www.ramielcreations.com/nexth-auth-magic-code
@ramiel so awesome! thank you so much, I really appreciate it :)
@ramiel very nice post! I agree it would be nice if we could implement it built-in, and when I finally have time, I might look into it!
