next email library

Hello im working on password recovery by email feature in next js. which mailing lib/service u suggest
23 Replies
bakdaddy
bakdaddy2y ago
nodemailer, it's fairly simple you could even use React Email with it Theo made a video about React Email recently https://www.youtube.com/watch?v=Xa1WaSPu5K8&t=166s https://react.email/docs/integrations/nodemailer they give more integrations on their website, you can use the one you'd like https://react.email/docs/integrations/overview
Lumberjack
LumberjackOP2y ago
thanks i'll use react email with nodemailer integration or should i use resend instead ?
bakdaddy
bakdaddy2y ago
I used nodemailer in my project, works absolute fine for the pass reset purposes, but you could try both and use the one you are more comfortable with
dan
dan2y ago
Resend
Build, test, and send transactional emails at scale.
Lumberjack
LumberjackOP2y ago
thanks guys if u dont mind i want to send that verification token through that email how can i generate it and make it match both on server and client sides ?
bakdaddy
bakdaddy2y ago
I used jwt to encode user's id with custom secret phrase that I match on the server jwt js lib has encode and decode methods for it, very handy you can also set expiration date in it for auto rejection if too much time passed const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET!, { expiresIn: "15m", });
dan
dan2y ago
jwt works for that. either jwt or storing a secret token in the database.
bakdaddy
bakdaddy2y ago
and you can send the token as a query parameter on the link, like https://your-app.com/forgotPass?token={YOUR_TOKEN_HERE} then you send the token form the query as part of user's request for the password change, and validate it on the server
Lumberjack
LumberjackOP2y ago
what about the process.env.JWT_SECRET! should imake it with openssl?
bakdaddy
bakdaddy2y ago
yeah, that's what I did
Lumberjack
LumberjackOP2y ago
thanks budd what is the lib for jwt ?
bakdaddy
bakdaddy2y ago
'openssl rand -base64 32' - it's the terminal command for a secret jsonwebtoken https://www.npmjs.com/package/jsonwebtoken
Lumberjack
LumberjackOP2y ago
damn u're a life saver thanks mate
bakdaddy
bakdaddy2y ago
https://jwt.io/libraries - you can choose another there's one called Jose but i haven't used it
dan
dan2y ago
i think i normally use jose when i do jwt stuffs
bakdaddy
bakdaddy2y ago
this one is by the team behind Auth0 make sure you don't send the client in the recovery page any confirmation that they got the info right or wrong, or any confirmations of success or failure. just do it as 'Successfully sent for reset', so that the attacker can't know if they got any info right
Lumberjack
LumberjackOP2y ago
i didn't understand would explain it again ?
bakdaddy
bakdaddy2y ago
yeah, so when a user clicks "forgot password" do you give them the form to type in their email? when they put email/username, dont send them errors if they typed it incorrectly/you haven't found corresponding email/user in your db
Lumberjack
LumberjackOP2y ago
okay why
bakdaddy
bakdaddy2y ago
say it's a hacker, and they want to know if a certain user exists in your app, they can just try their email/username in reset-password form and if it doesn't match - they would know the user isn't registered they can then try to use already leaked password in your app, as people use same passwords on multiple websites, and the passwords tend to leak check those links for more info https://security.stackexchange.com/questions/213975/how-to-properly-create-a-password-reset-token https://cinquewebdev.medium.com/how-to-implement-forgot-password-functionality-with-jwt-authentication-e1381263026c https://www.smashingmagazine.com/2017/11/safe-password-resets-with-json-web-tokens/
Lumberjack
LumberjackOP2y ago
got it
bakdaddy
bakdaddy2y ago
make sure you generate token on the server so that your secret isn't leaked too so an api page or route handle
Lumberjack
LumberjackOP2y ago
yes ofc ill make it in the /auth/forgetpass/route.ts
Want results from more Discord servers?
Add your server