Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions Backend/src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User } from "../models/user.models.js";
import ApiResponse from "../utils/ApiResponse.js";
import { COOKIE_OPTIONS } from "../constants.js";
import jwt from "jsonwebtoken";
import { sendVerificationEmail } from "../utils/nodemailer/email.js";
import { sendVerificationEmail ,sendUsername } from "../utils/nodemailer/email.js";

const registerUser = asyncHandler( async (req,res) => {
const { username, email, password } = req.body;
Expand Down Expand Up @@ -256,10 +256,9 @@ const changeEmail = asyncHandler(async(req,res)=>{
})

const verifyNewEmail = asyncHandler (async (req,res)=>{
//req.email pe abb naya email hai merepe already
const { newEmail,code } = req.body;
const user = req.user;
if (!code) {
if (!code || !newEmail) {
throw new ApiError(400, "All Fields Are Required.")
}
if (user.verificationCode !== code) {
Expand All @@ -279,6 +278,31 @@ const verifyNewEmail = asyncHandler (async (req,res)=>{
.json(new ApiResponse(200,updatedUser,"Email changed Successfully!"))
})

const forgotUsername = asyncHandler( async (req,res)=>{
const { email }=req.body;

if(!email){
throw new ApiError(400,"All fields are required")
}

const user = await User.findOne({ email: email });

if(!user){
throw new ApiError(404,"Email not registered");
}

try {
await sendUsername(email, user.username);

return res
.status(201)
.json(new ApiResponse(201,user,"Mail sent successfully"))

} catch (error) {
throw new ApiError(500, error.message || "Internal Server Error.");
}
})

export {
registerUser,
loginUser,
Expand All @@ -288,5 +312,6 @@ export {
changeUsername,
addDetails,
changeEmail,
verifyNewEmail
verifyNewEmail,
forgotUsername
};
3 changes: 2 additions & 1 deletion Backend/src/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from "express";
import { registerUser, loginUser, logoutUser, verifyCode, changeUsername, changePassword ,addDetails, changeEmail, verifyNewEmail} from "../controllers/user.controller.js";
import { registerUser, loginUser, logoutUser, verifyCode, changeUsername, changePassword ,addDetails, changeEmail, verifyNewEmail, forgotUsername} from "../controllers/user.controller.js";
import { registerUserSchema, loginUserSchema, verifyCodeSchema, changePasswordSchema, changeUsernameSchema } from "../utils/zodSchema/userValidatorSchema.js";
import validationSchema from "../middlewares/zodValidator.middleware.js";
import verifyToken from "../middlewares/auth.middleware.js";
Expand All @@ -9,6 +9,7 @@ const router = Router()
router.route("/register").post(validationSchema(registerUserSchema), registerUser);
router.route("/login").post(validationSchema(loginUserSchema), loginUser);
router.route("/verifyCode").post(validationSchema(verifyCodeSchema) ,verifyCode);
router.route("/forgotUsername").post(forgotUsername)

// secured routes
router.route("/logout").post(verifyToken, logoutUser);
Expand Down
17 changes: 16 additions & 1 deletion Backend/src/utils/nodemailer/email.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Verification_Email_Template } from "./template/emailTemplate.js";
import {Acoount_recovery_template} from "./template/usernameTemplate.js"
import transporter from "../../libs/emailConfig.js";

const sendVerificationEmail = async (email, verificationCode) => {
Expand All @@ -15,4 +16,18 @@ const sendVerificationEmail = async (email, verificationCode) => {
}
};

export { sendVerificationEmail };
const sendUsername = async (email,username)=>{
try {
const response = await transporter.sendMail({
from: '"Assemble" <testme2004.04@gmail.com>',
to: email,
subject: "Account Recovery",
html: Acoount_recovery_template.replace("{username}", username),
});
console.log("Email Send Successfully: ", response);
} catch (error) {
console.log(error.message);
}
}

export { sendVerificationEmail ,sendUsername};
78 changes: 78 additions & 0 deletions Backend/src/utils/nodemailer/template/usernameTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export const Acoount_recovery_template = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Verify Your Email</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
max-width: 600px;
margin: 30px auto;
background: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
overflow: hidden;
border: 1px solid #ddd;
}
.header {
background-color: #4CAF50;
color: white;
padding: 20px;
text-align: center;
font-size: 26px;
font-weight: bold;
}
.content {
padding: 25px;
color: #333;
line-height: 1.8;
}
.verification-code {
display: block;
margin: 20px 0;
font-size: 22px;
color: #4CAF50;
background: #e8f5e9;
border: 1px dashed #4CAF50;
padding: 10px;
text-align: center;
border-radius: 5px;
font-weight: bold;
letter-spacing: 2px;
}
.footer {
background-color: #f4f4f4;
padding: 15px;
text-align: center;
color: #777;
font-size: 12px;
border-top: 1px solid #ddd;
}
p {
margin: 0 0 15px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">Account Recovery</div>
<div class="content">
<p>Hello,</p>
<p>The username for your account registered with us:</p>
<span class="username">{username}</span>
<p>If you did not send this request, no further action is required. If you have any questions, feel free to contact our support team.</p>
</div>
<div class="footer">
<p>&copy; ${new Date().getFullYear()} Assemble. All rights reserved.</p>
</div>
</div>
</body>
</html>
`;