-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Reset-password):User who forgot password can reset it via email
- User who forgot password can request resetting it - Sending reset-password email containing link along with token to reset password - Reset password using the provided token - Token is used only once [Delivers #187419058]
- Loading branch information
YvetteNyibuka
committed
Apr 23, 2024
1 parent
abbc596
commit 3bba3d8
Showing
9 changed files
with
225 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { User } from '../database/models/User'; | ||
import { generatePasswordResetToken } from '../helpers/security.helpers'; | ||
import { ACCESS_TOKEN_SECRET, SENDER_EMAIL, SENDER_NAME } from '../utils/keys'; | ||
import Jwt from 'jsonwebtoken'; | ||
import { senderEmail } from '../helpers/nodemailer'; | ||
import { hashPassword } from '../utils/password'; | ||
|
||
const usedTokens: { [token: string]: boolean } = {}; | ||
|
||
export const forgotPassword = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { email } = req.body; | ||
|
||
const isUserExist:any = await User.findOne({ where: { email: email } }); | ||
|
||
if (!isUserExist) { | ||
return res.status(404).json({ | ||
message: 'User not found', | ||
}); | ||
} | ||
|
||
const resetToken = generatePasswordResetToken({ id: isUserExist.id as any, email: isUserExist.email }); | ||
usedTokens[resetToken] = false; | ||
|
||
// console.log("reset token====================", resetToken); | ||
|
||
res.setHeader('Authorization', resetToken); | ||
const port = process.env.PORT || 3000; | ||
|
||
const confirmlink: any = `http://localhost:${port}/passwordReset?token=${resetToken}`; | ||
const mailOptions = { | ||
from: `${SENDER_EMAIL}`, | ||
to: email, | ||
subject: 'Reset Password', | ||
html: ` | ||
<p>Click <a href="${confirmlink}">here</a> to reset your password</p> | ||
`, | ||
}; | ||
res.status(200).json({message: "email sent successfully", resetToken}) | ||
senderEmail(mailOptions); | ||
|
||
|
||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'An error occurred while processing your request.' }); | ||
} | ||
}; | ||
|
||
|
||
export const resetPassword = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { password } = req.body; | ||
const { token } = req.params; | ||
|
||
if (!token) { | ||
return res.status(400).json({ | ||
message: 'Token is required', | ||
}); | ||
} | ||
|
||
if (!ACCESS_TOKEN_SECRET) { | ||
throw new Error("ACCESS_TOKEN_SECRET is not defined"); | ||
} | ||
|
||
const decoded: any = Jwt.verify(token, ACCESS_TOKEN_SECRET); | ||
|
||
if (!decoded || !decoded.id) { | ||
return res.status(400).json({ | ||
message: 'Invalid token', | ||
}); | ||
} | ||
|
||
const resetingUser = await User.findOne({ where: { id: decoded.id } }); | ||
|
||
if (!resetingUser) { | ||
return res.status(404).json({ | ||
message: 'User not found', | ||
}); | ||
} | ||
|
||
// Check if token has been used | ||
if (usedTokens[token]) { | ||
return res.status(400).json({ | ||
message: 'Token has already been used', | ||
}); | ||
} | ||
const hashedPassword: string = await hashPassword(password) as string; | ||
usedTokens[token] = true; | ||
await resetingUser.update({ password: hashedPassword }); | ||
|
||
res.status(200).json({ message: 'Password reset successfully' }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'An error occurred while processing your request.' }); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import nodemailer from 'nodemailer'; | ||
import { SENDER_EMAIL, SENDER_NAME, SENDER_PASSWORD } from '../utils/keys'; | ||
|
||
interface MailOptions { | ||
to: string; | ||
subject: string; | ||
html: any; | ||
} | ||
|
||
const sender = nodemailer.createTransport({ | ||
service: "gmail", | ||
secure: true, | ||
port:5432, | ||
auth: { | ||
user: `${SENDER_EMAIL}`, | ||
pass: `${SENDER_PASSWORD}`, | ||
}, | ||
tls: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
|
||
// SEND EMAIL FUNCTION | ||
export function senderEmail({ to, subject, html }: MailOptions) { | ||
const mailOptions = { | ||
from: `"${SENDER_NAME}" <${SENDER_EMAIL}>`, | ||
to, | ||
subject, | ||
html, | ||
}; | ||
|
||
sender.sendMail(mailOptions, (error) => { | ||
if (error) { | ||
console.log("EMAILING USER FAILED:", error); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.