fedeya / next-auth-sanity

NextAuth Adapter and Provider for Sanity
https://sanity.io/plugins/next-auth-sanity
MIT License
76 stars 21 forks source link

How to implement Forgot Password #29

Closed Darpan-favfly closed 1 year ago

Darpan-favfly commented 1 year ago

How to implement Forgot Password option in my next-js project. I can't find any solution in readme docs or full example please help me to solve this... I am using Email, Password authentication in my project

Please give me some resources

fedeya commented 1 year ago

Hi @Darpan-favfly you can build your own forgot password system.

You have the user schema with the password of each user stored in your sanity project, the only requirement are to hash the passwords with argon2 when you save the new password.

For example too simple path:

  1. Get the email in a field and submit the form
  2. Check if the user is real with a sanity query *[_type == 'user' && email == $email][0]
  3. Store a unique token with expiration time in the user schema or another schema or in another db like a redis with some relation to user
  4. Send an email with one link like https://fake-app.com/recover-password?token=$forgotPasswordToken
  5. In that route you can find the token and know if it's related to user
  6. Hash the new password with argon2 and replace the old one with a mutation

I can't put things like this in the lib, because they don't make sense. this lib just store and finds data, you are open to do whatever you want with it.