Skip to content

Commit

Permalink
fix: update user profile updates access and refresh tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
juslam19 committed Nov 14, 2023
1 parent 4365820 commit 20e615f
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions backend/user-service/src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ export async function getCurrentUser(req: Request, res: Response) {
username: true,
password: true,
email: true,
languages: true,
role: true,
token: true,
},
Expand Down Expand Up @@ -576,6 +577,7 @@ export async function updateAccessToken(req: Request, res: Response) {
password: true,
email: true,
role: true,
languages: true,
token: true,
},
});
Expand Down Expand Up @@ -677,24 +679,52 @@ export const updateUserProfile: RequestHandler[] = [
},
});

res.json({
message: "User profile updated successfully",
user: updatedUser,
// UPDATING BOTH TOKENS
// Fetch the latest user data from the database
const user = await prisma.user.findFirst({
where: {
id: userId
},
include: {
languages: true,
},
});

if (!user) {
return res.status(401).json({ message: "Had issues retrieving user while updating tokens" });
}

//
const userWithoutPassword = {
id: user.id,
role: user.role,
email: user.email,
languages: user.languages,
username: user.username,
} as UserWithoutPassword;
const updatedAccessToken = await generateAccessToken(userWithoutPassword);
const updatedRefreshToken = await generateRefreshToken(userWithoutPassword);

await prisma.user.update({
where: { id: userId },
data: { token: updatedRefreshToken },
});

// Log the user out
res.clearCookie("accessToken", {
res.cookie("accessToken", updatedAccessToken, {
httpOnly: true,
secure: true,
sameSite: "none",
});
res.clearCookie("refreshToken", {
res.cookie("refreshToken", updatedRefreshToken, {
httpOnly: true,
secure: true,
sameSite: "none",
});
res.end();

res.json({
message: "User profile updated successfully",
user: updatedUser,
});

} catch (error) {
if (
Expand Down

0 comments on commit 20e615f

Please sign in to comment.