-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Implement refresh token strategy
- Loading branch information
Showing
14 changed files
with
279 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const TimeUtils = { | ||
parseDurationToSeconds: (duration: string): number => { | ||
const timeValue = parseInt(duration.slice(0, -1), 10); // Get the numeric part | ||
const timeUnit = duration.slice(-1); // Get the unit part (e.g., 'h', 'm', 's', 'd') | ||
|
||
switch (timeUnit) { | ||
case 's': // Seconds | ||
return timeValue; | ||
case 'm': // Minutes | ||
return timeValue * 60; | ||
case 'h': // Hours | ||
return timeValue * 3600; | ||
case 'd': // Days | ||
return timeValue * 86400; | ||
default: | ||
throw new Error('Invalid time unit. Use "s", "m", "h", or "d".'); | ||
} | ||
}, | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { authContract } from '@shared/contracts/auth.contract'; | ||
import { ROLES } from '@shared/entities/users/roles.enum'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
import { TestManager } from 'api/test/utils/test-manager'; | ||
|
||
describe('Refresh token', () => { | ||
let testManager: TestManager; | ||
|
||
beforeAll(async () => { | ||
testManager = await TestManager.createTestManager(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await testManager.clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testManager.close(); | ||
}); | ||
|
||
describe('AuthenticationController', () => { | ||
it('should return a new token pair when the refresh token is valid', async () => { | ||
// Given | ||
const user = await testManager.mocks().createUser({ | ||
email: '[email protected]', | ||
isActive: true, | ||
role: ROLES.PARTNER, | ||
}); | ||
|
||
const { jwtToken: accessToken, refreshToken } = | ||
await testManager.logUserIn(user); | ||
|
||
// When | ||
const refreshRes = await testManager | ||
.request() | ||
.post(authContract.refreshToken.path) | ||
.send({ refreshToken }); | ||
|
||
const newAccessToken = refreshRes.body.accessToken; | ||
const newRefreshToken = refreshRes.body.refreshToken; | ||
|
||
// Then | ||
expect(refreshRes.status).toBe(200); | ||
expect(newAccessToken).not.toBe(accessToken); | ||
expect(newRefreshToken).not.toBe(refreshToken); | ||
}); | ||
|
||
it('should return a 401 status code when the refresh token sent is valid but the user is not found or inactive', async () => { | ||
// Given | ||
const user = await testManager.mocks().createUser({ | ||
email: '[email protected]', | ||
isActive: true, | ||
role: ROLES.PARTNER, | ||
}); | ||
|
||
const { refreshToken } = await testManager.logUserIn(user); | ||
|
||
user.isActive = false; | ||
await testManager.getDataSource().getRepository(User).save(user); | ||
|
||
// When | ||
const refreshRes = await testManager | ||
.request() | ||
.post(authContract.refreshToken.path) | ||
.send({ refreshToken }); | ||
|
||
// Then | ||
expect(refreshRes.status).toBe(401); | ||
}); | ||
|
||
it('should return a 401 status code when the refresh token sent is not valid', async () => { | ||
const res = await testManager | ||
.request() | ||
.post(authContract.refreshToken.path) | ||
.send({ refreshToken: 'fake_token' }); | ||
|
||
expect(res.status).toBe(401); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.