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 21a39d752..813017000 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -89,7 +89,7 @@ describe('AuthService', () => { const accessTokenHash = await hashPassword(token); jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, }); const authTokenFromCall = await service.getAuthTokenAndValidate( @@ -98,7 +98,7 @@ describe('AuthService', () => { ); expect(authTokenFromCall).toEqual({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, }); }); @@ -112,7 +112,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( @@ -123,7 +123,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), }); @@ -138,7 +138,7 @@ describe('AuthService', () => { it('works', async () => { jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({ ...authToken, - user: user, + user: Promise.resolve(user), lastUsed: new Date(1549312452000), }); jest @@ -170,7 +170,7 @@ describe('AuthService', () => { }); jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({ ...authToken, - user: user, + user: Promise.resolve(user), accessTokenHash: accessTokenHash, }); jest @@ -204,14 +204,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 789e9b7fb..90bb67094 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -53,7 +53,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(