Skip to content

Commit

Permalink
add clarifying error message for token error vs authorization error
Browse files Browse the repository at this point in the history
  • Loading branch information
youngbryanyu committed Feb 29, 2024
1 parent 5fda654 commit e6fb35c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion backend/src/features/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export enum AuthHeaders {
}

export enum AuthResponseMessages {
_401_Unauthorized = "You are not authenticated with a valid access token or aren't authorized to access this resource."
_401_InvalidToken = 'You are not authenticated with a valid access token',
_401_NoAccess = "You aren't authorized to access this resource"
}
10 changes: 5 additions & 5 deletions backend/src/features/auth/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AuthController {
/* Check if headers contains bearer schema */
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
res.status(401).json({
message: AuthResponseMessages._401_Unauthorized
message: AuthResponseMessages._401_InvalidToken
});
return;
}
Expand All @@ -35,7 +35,7 @@ class AuthController {
next();
} catch (error) {
res.status(401).json({
message: AuthResponseMessages._401_Unauthorized
message: AuthResponseMessages._401_InvalidToken
});
return;
}
Expand All @@ -61,23 +61,23 @@ class AuthController {
/* Check if decrypted token's UID is undefined */
if (tokenUid === undefined) {
res.status(401).json({
message: AuthResponseMessages._401_Unauthorized
message: AuthResponseMessages._401_NoAccess
});
return;
}

/* Check if `userId` is in body */
if (req.body.userId !== undefined && tokenUid !== req.body.userId) {
res.status(401).json({
message: AuthResponseMessages._401_Unauthorized
message: AuthResponseMessages._401_NoAccess
});
return;
}

/* Check if `userId` is in path params */
if (req.params.userId !== undefined && tokenUid !== req.params.userId) {
res.status(401).json({
message: AuthResponseMessages._401_Unauthorized
message: AuthResponseMessages._401_NoAccess
});
return;
}
Expand Down
12 changes: 6 additions & 6 deletions backend/tests/features/auth/authControllers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('AuthController Tests', () => {

/* Test against expected */
expect(response.statusCode).toBe(401);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_Unauthorized);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_InvalidToken);
expect(TestController.testFunction).not.toHaveBeenCalled();
});

Expand All @@ -68,7 +68,7 @@ describe('AuthController Tests', () => {

/* Test against expected */
expect(response.statusCode).toBe(401);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_Unauthorized);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_InvalidToken);
expect(TestController.testFunction).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -109,7 +109,7 @@ describe('AuthController Tests', () => {

/* Test against expected */
expect(response.statusCode).toBe(401);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_Unauthorized);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_InvalidToken);
expect(TestController.testFunction).not.toHaveBeenCalled();
});
});
Expand All @@ -129,7 +129,7 @@ describe('AuthController Tests', () => {

/* Test against expected */
expect(response.statusCode).toBe(401);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_Unauthorized);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_NoAccess);
expect(TestController.testFunction).not.toHaveBeenCalled();
});

Expand All @@ -148,7 +148,7 @@ describe('AuthController Tests', () => {

/* Test against expected */
expect(response.statusCode).toBe(401);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_Unauthorized);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_NoAccess);
expect(TestController.testFunction).not.toHaveBeenCalled();
});

Expand All @@ -167,7 +167,7 @@ describe('AuthController Tests', () => {

/* Test against expected */
expect(response.statusCode).toBe(401);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_Unauthorized);
expect(response._getJSONData().message).toBe(AuthResponseMessages._401_NoAccess);
expect(TestController.testFunction).not.toHaveBeenCalled();
});

Expand Down

0 comments on commit e6fb35c

Please sign in to comment.