Skip to content

Commit

Permalink
fix login extended req
Browse files Browse the repository at this point in the history
  • Loading branch information
sevelinCa committed Jul 21, 2024
1 parent ef2ce98 commit 51ea6cd
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,35 @@ interface ExtendedRequest extends ExpressRequest {
};
}

export const login = async (req: ExtendedRequest, res: Response) => {
if (req.session.twoFAError) {
res.status(401).json({ message: req.session.twoFAError });
} else {
try {
const email = req.session.email || req.body.email;
const password = req.session.password || req.body.password;

const { existUser, vendorId } = await loginFunc({ email, password });
if (!existUser) {
return res.status(404).json({ message: "User not found" });
}
export const login = async (req: Request, res: Response) => {
try {
const email = req.body.email;
const password = req.body.password;

const isPasswordValid = await bcrypt.compare(
password,
existUser.password
);
if (!isPasswordValid) {
return res
.status(401)
.json({ message: "Invalid credentials. Try again" });
}
const { existUser, vendorId } = await loginFunc({ email, password });
if (!existUser) {
return res.status(404).json({ message: "User not found" });
}

const token = await generateToken(existUser);
res.cookie("token", token, { httpOnly: true });
const isPasswordValid = await bcrypt.compare(password, existUser.password);
if (!isPasswordValid) {
return res.status(401).json({ message: "Invalid credentials. Try again" });
}

req.session.email = null;
req.session.password = null;
const token = await generateToken(existUser);
res.cookie("token", token, { httpOnly: true });

return res.status(200).json({
message: "Login successful",
token,
user: existUser,vendorId
});
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Unable to log in" });
}
return res.status(200).json({
message: "Login successful",
token,
user: existUser,
vendorId
});
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Unable to log in" });
}
};

export const register = async (req: Request, res: Response) => {
const { name, email, password } = req.body;
if (!name || !email || !password) {
Expand Down

0 comments on commit 51ea6cd

Please sign in to comment.