Skip to content

Commit

Permalink
fix: add back stuff to login
Browse files Browse the repository at this point in the history
data in login is used on frontend (stored to redux) then read by user
profile
  • Loading branch information
seelengxd committed Nov 14, 2023
1 parent 0a2d9fa commit 375d7c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
25 changes: 13 additions & 12 deletions backend/user-service/src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ export const logIn: RequestHandler[] = [
const userWithoutPassword = {
id: user.id,
role: user.role,
email: user.email,
languages: user.languages,
username: user.username,
} as UserWithoutPassword;
const accessToken = await generateAccessToken(userWithoutPassword);
const refreshToken = await generateRefreshToken(userWithoutPassword);
Expand Down Expand Up @@ -156,7 +159,7 @@ export async function logOut(req: Request, res: Response) {
const refreshToken = req.cookies["refreshToken"]; // If JWT token is stored in a cookie
if (refreshToken) {
const decoded = (await authenticateRefreshToken(
refreshToken,
refreshToken
)) as JwtPayload;
const userId = decoded.user.id; // user ID is used for identification
if (userId) {
Expand All @@ -170,7 +173,7 @@ export async function logOut(req: Request, res: Response) {
// This means access token has expired
console.log("Cannot remove login refresh token from server: " + error);
console.log(
"You might have removed it somehow. Suggested that you login again to remove old refreshToken from server.",
"You might have removed it somehow. Suggested that you login again to remove old refreshToken from server."
);
console.log("Proceeding with rest of log out procedure...");
}
Expand Down Expand Up @@ -209,7 +212,7 @@ export const oAuthAuthenticate: RequestHandler[] = [
"Access-Control-Allow-Origin": "*",
Accept: "application/json",
},
},
}
);

const resp = await response.text();
Expand Down Expand Up @@ -304,13 +307,11 @@ export const oAuthNewUser: RequestHandler[] = [
});

if (user !== null) {
res
.status(400)
.json({
errors: [
`Github user with ID ${githubUserId} already exists in the system`,
],
});
res.status(400).json({
errors: [
`Github user with ID ${githubUserId} already exists in the system`,
],
});
return;
}

Expand Down Expand Up @@ -531,7 +532,7 @@ export async function updateAccessToken(req: Request, res: Response) {
} else {
try {
const decoded = (await authenticateRefreshToken(
refreshToken,
refreshToken
)) as JwtPayload;
const userWithoutPassword = decoded.user;

Expand Down Expand Up @@ -602,7 +603,7 @@ export const updateUserProfile: RequestHandler[] = [
const accessToken = req.cookies["accessToken"]; // If JWT token is stored in a cookie

const decoded = (await authenticateAccessToken(
accessToken,
accessToken
)) as JwtPayload;

const userId = decoded.user.id; // user ID is used for identification
Expand Down
7 changes: 5 additions & 2 deletions backend/user-service/src/middleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export interface User {
export interface UserWithoutPassword {
id: number;
role: string;
username: string;
email: string;
languages: { id: number; language: string }[];
}

export interface JwtPayload {
Expand All @@ -26,7 +29,7 @@ export interface JwtPayload {
export async function verifyAccessToken(
req: Request,
res: Response,
next: NextFunction,
next: NextFunction
) {
const accessToken = req.cookies["accessToken"]; // If JWT token is stored in a cookie

Expand All @@ -49,7 +52,7 @@ export async function verifyAccessToken(
export async function protectAdmin(
req: Request,
res: Response,
next: NextFunction,
next: NextFunction
) {
const accessToken = req.cookies["accessToken"]; // If JWT token is stored in a cookie
const decoded = (await authenticateAccessToken(accessToken)) as JwtPayload;
Expand Down

0 comments on commit 375d7c4

Please sign in to comment.