mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 03:06:31 -05:00
refactor(auth-token): lazy-load relations
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
b25e6fc365
commit
de6d75238c
3 changed files with 15 additions and 11 deletions
|
@ -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<User>;
|
||||
|
||||
@Column()
|
||||
label: string;
|
||||
|
@ -53,7 +53,7 @@ export class AuthToken {
|
|||
): Omit<AuthToken, 'id' | 'createdAt'> {
|
||||
const newToken = new AuthToken();
|
||||
newToken.keyId = keyId;
|
||||
newToken.user = user;
|
||||
newToken.user = Promise.resolve(user);
|
||||
newToken.label = label;
|
||||
newToken.accessTokenHash = accessToken;
|
||||
newToken.validUntil = validUntil;
|
||||
|
|
|
@ -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<AuthToken> => {
|
||||
expect(token).toEqual({
|
||||
...authToken,
|
||||
user: user,
|
||||
user: Promise.resolve(user),
|
||||
});
|
||||
return authToken;
|
||||
});
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue