Skip to content

Commit

Permalink
fixes: user data formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Avdhesh-Varshney committed Mar 3, 2025
1 parent 2ba0340 commit 6b74fd7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 1 addition & 5 deletions backend/Controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import User from "../Models/user.model.js";
import { formatDataToSend, generateUsername, emailRegex, passwordRegex } from "../utils/helpers.js";

Expand Down Expand Up @@ -38,10 +37,7 @@ export const login = async (req, res) => {
const isMatch = await bcrypt.compare(password, user.personal_info.password);
if (!isMatch) return res.status(401).json({ error: "Incorrect password" });

// Generate JWT Token
const token = jwt.sign({ id: user._id, role: user.role }, process.env.SECRET_ACCESS_KEY, { expiresIn: "7d" });

return res.status(200).json({ token, user: formatDataToSend(user) });
return res.status(200).json(formatDataToSend(user));
} catch (err) {
return res.status(500).json({ error: "Internal Server Error" });
}
Expand Down
6 changes: 6 additions & 0 deletions backend/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import jwt from "jsonwebtoken";
import { nanoid } from "nanoid";
import User from "../Models/user.model.js";

export const emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
export const passwordRegex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;

export const formatDataToSend = (user) => {

// Generate JWT Token
const access_token = jwt.sign({ id: user._id, role: user.role }, process.env.SECRET_ACCESS_KEY, { expiresIn: "7d" });

return {
access_token,
profile_img: user.personal_info.profile_img,
username: user.personal_info.username,
fullname: user.personal_info.fullname,
Expand Down

0 comments on commit 6b74fd7

Please sign in to comment.