fix: don't delete token immediately after refresh. keep it for 6 sec to account for delays
This commit is contained in:
parent
e8d7edbe57
commit
93347d1947
4 changed files with 16 additions and 4 deletions
|
@ -1,11 +1,13 @@
|
|||
module.exports = {
|
||||
createClient: jest.fn(() => {
|
||||
const values = new Map();
|
||||
const expirations = new Map();
|
||||
return {
|
||||
isOpen: true,
|
||||
connect: jest.fn(),
|
||||
set: (key: string, value: string) => {
|
||||
set: (key: string, value: string, exp: number) => {
|
||||
values.set(key, value);
|
||||
expirations.set(key, exp);
|
||||
},
|
||||
get: (key: string) => {
|
||||
return values.get(key);
|
||||
|
@ -14,6 +16,9 @@ module.exports = {
|
|||
del: (key: string) => {
|
||||
return values.delete(key);
|
||||
},
|
||||
ttl: (key: string) => {
|
||||
return expirations.get(key);
|
||||
},
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
|
|
@ -51,6 +51,11 @@ class TipiCache {
|
|||
public async close() {
|
||||
return this.client.quit();
|
||||
}
|
||||
|
||||
public async ttl(key: string) {
|
||||
const client = await this.getClient();
|
||||
return client.ttl(key);
|
||||
}
|
||||
}
|
||||
|
||||
export default TipiCache.getInstance();
|
||||
|
|
|
@ -182,19 +182,20 @@ describe('Test: refreshToken', () => {
|
|||
expect(result?.token).not.toBe(session);
|
||||
});
|
||||
|
||||
it('Should delete old session from cache', async () => {
|
||||
it('Should put expiration in 6 seconds for old session', async () => {
|
||||
// Arrange
|
||||
const session = faker.random.alphaNumeric(32);
|
||||
await TipiCache.set(session, '1');
|
||||
|
||||
// Act
|
||||
const result = await AuthService.refreshToken(session);
|
||||
const expiration = await TipiCache.ttl(session);
|
||||
|
||||
// Assert
|
||||
expect(result).not.toBeNull();
|
||||
expect(result).toHaveProperty('token');
|
||||
expect(result?.token).not.toBe(session);
|
||||
expect(await TipiCache.get(session)).toBeUndefined();
|
||||
expect(expiration).toMatchObject({ EX: 6 });
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -81,7 +81,8 @@ const refreshToken = async (session?: string): Promise<TokenResponse | null> =>
|
|||
const userId = await TipiCache.get(session);
|
||||
if (!userId) return null;
|
||||
|
||||
await TipiCache.del(session);
|
||||
// Expire token in 6 seconds
|
||||
await TipiCache.set(session, userId, 6);
|
||||
|
||||
const newSession = v4();
|
||||
const token = jwt.sign({ id: userId, session: newSession }, getConfig().jwtSecret, { expiresIn: '1d' });
|
||||
|
|
Loading…
Add table
Reference in a new issue