Skip to content

Commit

Permalink
feat: add middleware auth-jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-silveiraa authored and kvnbg committed Jun 11, 2024
1 parent 86d6ca8 commit 977dd98
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/middlewares/auth/auth-jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('jwtAuth middleware', () => {
});

it('should return status code 401 with error message invalid token', async () => {
const token = 'token';
const token = 'invalidToken';

req = { headers: { authorization: `Bearer ${token}` } };

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/auth/auth-jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AuthenticationJWT {
}

const payload = this.jwtHelper.parseToken(token);
if (!payload) {
if (payload instanceof Error) {
return res.status(401).json({ error: 'Invalid token' });
}

Expand Down
17 changes: 9 additions & 8 deletions src/shared/infra/jwt/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JWTHelper } from './jwt.js';
import type { TokenPayload } from './jwt.js';

describe('JWTHelper', () => {
const secretKey = '123';
Expand All @@ -14,18 +15,17 @@ describe('JWTHelper', () => {
});

describe('refreshToken', () => {
const token = jwt.createToken(payload);

it('should refresh token', () => {
const token = jwt.createToken(payload);
const newToken = jwt.refreshToken(token);
const exist = newToken ? true : false;

expect(exist).toBeTruthy();
expect(newToken).not.toBe(token);
});

it('should not refresh an invalid token', () => {
const invalidToken = 'invalidToken';
const newToken = jwt.refreshToken(invalidToken);
const newToken = jwt.refreshToken('invalidToken');

expect(newToken).toBe('invalid token');
});
Expand All @@ -34,16 +34,17 @@ describe('JWTHelper', () => {
describe('parseToken', () => {
it('should parse a valid token', () => {
const token = jwt.createToken(payload);
const parsedPayload = jwt.parseToken(token);
const parsedPayload = jwt.parseToken(token) as TokenPayload;

expect(parsedPayload?.userId).toContain(payload.userId);
expect(parsedPayload?.userId).toBe(payload.userId);
});

it('should return null for an invalid token', () => {
it('should return error for an invalid token', () => {
const invalidToken = 'invalidToken';
const parsedPayload = jwt.parseToken(invalidToken);
const error = new Error('Invalid token');

expect(parsedPayload).toBeNull();
expect(parsedPayload).toEqual(error);
});
});
});
25 changes: 8 additions & 17 deletions src/shared/infra/jwt/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
import jwt from 'jsonwebtoken';

interface TokenPayload {
export interface TokenPayload {
userId: string;
}

export class JWTHelper {
private secretKey: string;

constructor(secretKey: string) {
this.secretKey = secretKey;
}
constructor(private readonly secretKey: string) {}

createToken(token: TokenPayload, expiresIn: string = '1h'): string {
return jwt.sign(token, this.secretKey, { expiresIn });
}

parseToken(token: string): null | TokenPayload {
parseToken(token: string): Error | TokenPayload {
try {
const payload = jwt.verify(token, this.secretKey) as TokenPayload;
return payload;
} catch {
return null;
return new Error('Invalid token');
}
}

refreshToken(token: string): Error | string {
try {
const payload = this.parseToken(token);
if (!payload) return 'invalid token';
return this.createToken(payload);
} catch {
const message = new Error('error creating token');
return message;
}
refreshToken(token: string): string {
const payload = this.parseToken(token);
if (payload instanceof Error) return 'invalid token';
return jwt.sign(payload, this.secretKey);
}
}

0 comments on commit 977dd98

Please sign in to comment.