-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from kaali001/kaali001/144/forgot-password
feat: Implemented forgot password. issue: #144
- Loading branch information
Showing
14 changed files
with
344 additions
and
16 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,54 @@ | ||
import React, { useState } from "react"; | ||
import axios from "axios"; | ||
import toast from "react-hot-toast"; | ||
|
||
const ForgotPassword = () => { | ||
const [email, setEmail] = useState(""); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
try { | ||
const response = await axios.post( | ||
"http://localhost:3000/api/users/forgot-password", | ||
{ email }, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
} | ||
} | ||
); | ||
toast.success(response.data.message); | ||
} catch (error) { | ||
toast.error(error.response.data.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex items-center justify-center min-h-screen bg-slate-100"> | ||
<div className="bg-white p-8 rounded-lg shadow-md w-full max-w-md"> | ||
<h2 className="text-3xl font-bold text-center my-4 mb-12 text-gray-600">Find Your Account</h2> | ||
<form onSubmit={handleSubmit} className="space-y-6"> | ||
<div className="my-3"> | ||
<label className="block text-lg text-gray-400 mb-2">Enter your email below.</label> | ||
<input | ||
type="email" | ||
className="appearance-none border pl-4 border-gray-300 shadow-sm focus:shadow-md focus:placeholder-gray-600 transition rounded-md w-full py-3 text-gray-600 leading-tight focus:outline-none focus:ring-gray-600 focus:shadow-outline" | ||
value={email} | ||
placeholder="Email" | ||
onChange={(e) => setEmail(e.target.value)} | ||
required | ||
/> | ||
</div> | ||
<button | ||
type="submit" | ||
className="w-full py-2 px-4 bg-indigo-500 text-white rounded-md hover:bg-indigo-600" | ||
> | ||
Send Reset Link | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ForgotPassword; |
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,76 @@ | ||
import React, { useState } from "react"; | ||
import { useParams } from "react-router-dom"; | ||
import axios from "axios"; | ||
import toast from "react-hot-toast"; | ||
import { Navigate } from "react-router-dom"; | ||
const ResetPassword = () => { | ||
const { token } = useParams(); | ||
const { id } = useParams(); | ||
const [password, setPassword] = useState(""); | ||
const [confirmPassword, setConfirmPassword] = useState(""); | ||
const [reset, setreset] = useState(false); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
if (password !== confirmPassword) { | ||
return toast.error("Passwords do not match"); | ||
} | ||
try { | ||
const response = await axios.post( | ||
`http://localhost:3000/api/users/reset-password/${token}/${id}`, | ||
{ password }, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
} | ||
} | ||
); | ||
toast.success(response.data.message); | ||
setreset(true); | ||
} catch (error) { | ||
toast.error(error.response.data.message); | ||
} | ||
}; | ||
|
||
if (reset) { | ||
return <Navigate replace to="/login" />; | ||
} | ||
|
||
return ( | ||
<div className="flex items-center justify-center min-h-screen bg-slate-100"> | ||
<div className="bg-white p-8 rounded-lg shadow-md w-full max-w-md"> | ||
<h2 className="text-2xl font-bold text-center mb-6">Reset Password</h2> | ||
<form onSubmit={handleSubmit}> | ||
<div className="mb-4"> | ||
<label className="block text-gray-700">New Password:</label> | ||
<input | ||
type="password" | ||
className="appearance-none border pl-4 border-gray-300 shadow-sm focus:shadow-md focus:placeholder-gray-600 transition rounded-md w-full py-3 text-gray-600 leading-tight focus:outline-none focus:ring-gray-600 focus:shadow-outline" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
required | ||
/> | ||
</div> | ||
<div className="mb-4"> | ||
<label className="block text-gray-700">Confirm Password:</label> | ||
<input | ||
type="password" | ||
className="appearance-none border pl-4 border-gray-300 shadow-sm focus:shadow-md focus:placeholder-gray-600 transition rounded-md w-full py-3 text-gray-600 leading-tight focus:outline-none focus:ring-gray-600 focus:shadow-outline" | ||
value={confirmPassword} | ||
onChange={(e) => setConfirmPassword(e.target.value)} | ||
required | ||
/> | ||
</div> | ||
<button | ||
type="submit" | ||
className="w-full py-2 px-4 bg-indigo-500 text-white rounded-md hover:bg-indigo-600" | ||
> | ||
Reset Password | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ResetPassword; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { prisma } from "../app.js"; | ||
import bcrypt from "bcryptjs"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { sendResetEmail } from "../utils/email.js"; | ||
|
||
export const requestPasswordReset = async (req, res) => { | ||
const { email } = req.body; | ||
const user = await prisma.mainuser.findUnique({ where: { email } }); | ||
if (!user) { | ||
return res.status(404).json({ message: "User not found" }); | ||
} | ||
|
||
const token = uuidv4(); | ||
const hashedToken = await bcrypt.hash(token, 10); | ||
|
||
|
||
// Ensure any previous tokens for this user are deleted | ||
await prisma.passwordResetToken.deleteMany({ | ||
where: { userId: user.email } | ||
}); | ||
|
||
const passwd = await prisma.passwordResetToken.create({ | ||
data: { | ||
token: hashedToken, | ||
userId: user.email, | ||
expiresAt: new Date(Date.now() + 3600000), // 1 hour from now | ||
} | ||
}); | ||
|
||
const resetUrl = `http://localhost:5173/reset-password/${token}/${passwd.id}`; | ||
await sendResetEmail(email, resetUrl); | ||
|
||
res.status(200).json({ message: "Password reset link has been sent to your email" }); | ||
}; | ||
export const resetPassword = async (req, res) => { | ||
const { token } = req.params; | ||
const { password } = req.body; | ||
const {id} = req.params; | ||
const resetToken = await prisma.passwordResetToken.findUnique({ | ||
where: { | ||
expiresAt: { gte: new Date() }, | ||
|
||
id:parseInt(id, 10) | ||
} | ||
}); | ||
|
||
if (!resetToken) { | ||
return res.status(400).json({ message: "Invalid or expired token1" }); | ||
} | ||
|
||
const isValid = await bcrypt.compare(token, resetToken.token); | ||
if (!isValid) { | ||
return res.status(400).json({ message: "Invalid or expired token2" }); | ||
} | ||
|
||
const hashedPassword = await bcrypt.hash(password, 10); | ||
|
||
await prisma.mainuser.update({ | ||
where: { email: resetToken.userId }, | ||
data: { passwd: hashedPassword } | ||
}); | ||
|
||
await prisma.passwordResetToken.delete({ where: { id: resetToken.id } }); | ||
|
||
res.status(200).json({ message: "Password has been reset successfully" }); | ||
}; |
Oops, something went wrong.