From de6d75238c19067a7989d4d8a858596022871726 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Tue, 16 Nov 2021 18:39:52 +0100 Subject: [PATCH] refactor(auth-token): lazy-load relations Signed-off-by: David Mehren --- src/auth/auth-token.entity.ts | 4 ++-- src/auth/auth.service.spec.ts | 16 ++++++++-------- src/auth/auth.service.ts | 6 +++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/auth/auth-token.entity.ts b/src/auth/auth-token.entity.ts index 80d9da025..668956813 100644 --- a/src/auth/auth-token.entity.ts +++ b/src/auth/auth-token.entity.ts @@ -24,7 +24,7 @@ export class AuthToken { @ManyToOne((_) => User, (user) => user.authTokens, { onDelete: 'CASCADE', // This deletes the AuthToken, when the associated User is deleted }) - user: User; + user: Promise; @Column() label: string; @@ -53,7 +53,7 @@ export class AuthToken { ): Omit { const newToken = new AuthToken(); newToken.keyId = keyId; - newToken.user = user; + newToken.user = Promise.resolve(user); newToken.label = label; newToken.accessTokenHash = accessToken; newToken.validUntil = validUntil; diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index f9d5249bc..af0c2fd12 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -93,7 +93,7 @@ describe('AuthService', () => { .digest('hex'); jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, }); const authTokenFromCall = await service.getAuthTokenAndValidate( @@ -102,7 +102,7 @@ describe('AuthService', () => { ); expect(authTokenFromCall).toEqual({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, }); }); @@ -116,7 +116,7 @@ describe('AuthService', () => { it('AuthToken has wrong hash', async () => { jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: 'the wrong hash', }); await expect( @@ -127,7 +127,7 @@ describe('AuthService', () => { const accessTokenHash = await hashPassword(token); jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, validUntil: new Date(1549312452000), }); @@ -142,7 +142,7 @@ describe('AuthService', () => { it('works', async () => { jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ ...authToken, - user: user, + user: Promise.resolve(user), lastUsed: new Date(1549312452000), }); jest @@ -178,7 +178,7 @@ describe('AuthService', () => { }); jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, }); jest @@ -212,14 +212,14 @@ describe('AuthService', () => { it('works', async () => { jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({ ...authToken, - user: user, + user: Promise.resolve(user), }); jest .spyOn(authTokenRepo, 'remove') .mockImplementationOnce(async (token, __): Promise => { expect(token).toEqual({ ...authToken, - user: user, + user: Promise.resolve(user), }); return authToken; }); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 88a982e67..343fb45a4 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -46,7 +46,11 @@ export class AuthService { } const accessToken = await this.getAuthTokenAndValidate(keyId, secret); await this.setLastUsedToken(keyId); - return await this.usersService.getUserByUsername(accessToken.user.username); + return await this.usersService.getUserByUsername( + ( + await accessToken.user + ).username, + ); } async createTokenForUser(