-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
164796888-feature(passwordReset): add function to reset password
- add password reset functionality [Delivers #164796888]
- Loading branch information
1 parent
0f6ee0d
commit 7c36b20
Showing
4 changed files
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import bcrypt from 'bcryptjs'; | ||
import { User } from '../../models'; | ||
import Auth from '../../middlewares/authenticator'; | ||
import { responseFormat, errorResponseFormat } from '../../utils/index'; | ||
|
||
export const forgotPassword = async (req, res) => { | ||
try { | ||
const { email } = req.body; | ||
const user = await User.findOne({ where: { email } }); | ||
if (!user) { return res.status(404).json(errorResponseFormat({ status: 'fail', message: 'Account associated with email cannot with found' })); } | ||
const passwordResetToken = Auth.generateToken({ email }); | ||
await user.update({ passwordResetToken, resetTokenExpires: Date.now() + 36000 }); | ||
return res.status(200).json(responseFormat({ status: 'success', data: `Password reset instruction was successfully sent to ${req.body.email}` })); | ||
} catch (error) { | ||
return res.status(500).json({ status: 'error', message: 'Something went wrong' }); | ||
} | ||
}; | ||
|
||
export const resetPassword = async (req, res) => { | ||
try { | ||
const { resetToken } = req.params; | ||
const user = await User.findOne({ where: { passwordResetToken: resetToken, } }); | ||
if (!user) { return res.status(404).json(errorResponseFormat({ status: 'fail', message: 'Password link has expired!' })); } | ||
return res.status(200).json(responseFormat({ status: 'success', data: 'Password reset link is valid' })); | ||
} catch (error) { | ||
return res.status(500).json({ status: 'error', message: 'Something went wrong' }); | ||
} | ||
}; | ||
|
||
export const updatePassword = async (req, res) => { | ||
try { | ||
const { password } = req.body; | ||
const hashPassword = bcrypt.hashSync(password, bcrypt.genSaltSync(10)); | ||
// eslint-disable-next-line max-len | ||
await User.update({ password: hashPassword, passwordResetToken: null, resetTokenExpires: null }); | ||
return res.status(202).json(responseFormat({ status: 'success', data: 'Password was successfully updated!' })); | ||
} catch (error) { | ||
return res.status(500).json({ status: 'error', message: '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
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