Skip to content

Commit 42336b5

Browse files
committed
refactor(server): use verificationToken model instead of tokenService
1 parent b316922 commit 42336b5

File tree

6 files changed

+55
-271
lines changed

6 files changed

+55
-271
lines changed

packages/backend/server/src/__tests__/auth/token.spec.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

packages/backend/server/src/core/auth/controller.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import {
2626
URLHelper,
2727
UseNamedGuard,
2828
} from '../../base';
29+
import { Models, TokenType } from '../../models';
2930
import { UserService } from '../user';
3031
import { validators } from '../utils/validators';
3132
import { Public } from './guard';
3233
import { AuthService } from './service';
3334
import { CurrentUser, Session } from './session';
34-
import { TokenService, TokenType } from './token';
3535

3636
interface PreflightResponse {
3737
registered: boolean;
@@ -57,7 +57,7 @@ export class AuthController {
5757
private readonly url: URLHelper,
5858
private readonly auth: AuthService,
5959
private readonly user: UserService,
60-
private readonly token: TokenService,
60+
private readonly models: Models,
6161
private readonly config: Config,
6262
private readonly runtime: Runtime
6363
) {
@@ -194,7 +194,10 @@ export class AuthController {
194194
}
195195
}
196196

197-
const token = await this.token.createToken(TokenType.SignIn, email);
197+
const token = await this.models.verificationToken.create(
198+
TokenType.SignIn,
199+
email
200+
);
198201

199202
const magicLink = this.url.link(callbackUrl, {
200203
token,
@@ -248,9 +251,13 @@ export class AuthController {
248251

249252
validators.assertValidEmail(email);
250253

251-
const tokenRecord = await this.token.verifyToken(TokenType.SignIn, token, {
252-
credential: email,
253-
});
254+
const tokenRecord = await this.models.verificationToken.verify(
255+
TokenType.SignIn,
256+
token,
257+
{
258+
credential: email,
259+
}
260+
);
254261

255262
if (!tokenRecord) {
256263
throw new InvalidEmailToken();

packages/backend/server/src/core/auth/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,21 @@ import { AuthController } from './controller';
99
import { AuthGuard, AuthWebsocketOptionsProvider } from './guard';
1010
import { AuthResolver } from './resolver';
1111
import { AuthService } from './service';
12-
import { TokenService, TokenType } from './token';
1312

1413
@Module({
1514
imports: [FeatureModule, UserModule, QuotaModule],
1615
providers: [
1716
AuthService,
1817
AuthResolver,
19-
TokenService,
2018
AuthGuard,
2119
AuthWebsocketOptionsProvider,
2220
],
23-
exports: [AuthService, AuthGuard, AuthWebsocketOptionsProvider, TokenService],
21+
exports: [AuthService, AuthGuard, AuthWebsocketOptionsProvider],
2422
controllers: [AuthController],
2523
})
2624
export class AuthModule {}
2725

2826
export * from './guard';
2927
export { ClientTokenType } from './resolver';
30-
export { AuthService, TokenService, TokenType };
28+
export { AuthService };
3129
export * from './session';

packages/backend/server/src/core/auth/resolver.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import {
2121
Throttle,
2222
URLHelper,
2323
} from '../../base';
24+
import { Models, TokenType } from '../../models';
2425
import { Admin } from '../common';
2526
import { UserService } from '../user';
2627
import { UserType } from '../user/types';
2728
import { validators } from '../utils/validators';
2829
import { Public } from './guard';
2930
import { AuthService } from './service';
3031
import { CurrentUser } from './session';
31-
import { TokenService, TokenType } from './token';
3232

3333
@ObjectType('tokenType')
3434
export class ClientTokenType {
@@ -49,7 +49,7 @@ export class AuthResolver {
4949
private readonly url: URLHelper,
5050
private readonly auth: AuthService,
5151
private readonly user: UserService,
52-
private readonly token: TokenService
52+
private readonly models: Models
5353
) {}
5454

5555
@SkipThrottle()
@@ -96,7 +96,7 @@ export class AuthResolver {
9696
}
9797

9898
// NOTE: Set & Change password are using the same token type.
99-
const valid = await this.token.verifyToken(
99+
const valid = await this.models.verificationToken.verify(
100100
TokenType.ChangePassword,
101101
token,
102102
{
@@ -121,9 +121,13 @@ export class AuthResolver {
121121
@Args('email') email: string
122122
) {
123123
// @see [sendChangeEmail]
124-
const valid = await this.token.verifyToken(TokenType.VerifyEmail, token, {
125-
credential: user.id,
126-
});
124+
const valid = await this.models.verificationToken.verify(
125+
TokenType.VerifyEmail,
126+
token,
127+
{
128+
credential: user.id,
129+
}
130+
);
127131

128132
if (!valid) {
129133
throw new InvalidEmailToken();
@@ -152,7 +156,7 @@ export class AuthResolver {
152156
throw new EmailVerificationRequired();
153157
}
154158

155-
const token = await this.token.createToken(
159+
const token = await this.models.verificationToken.create(
156160
TokenType.ChangePassword,
157161
user.id
158162
);
@@ -195,7 +199,10 @@ export class AuthResolver {
195199
throw new EmailVerificationRequired();
196200
}
197201

198-
const token = await this.token.createToken(TokenType.ChangeEmail, user.id);
202+
const token = await this.models.verificationToken.create(
203+
TokenType.ChangeEmail,
204+
user.id
205+
);
199206

200207
const url = this.url.link(callbackUrl, { token });
201208

@@ -215,9 +222,13 @@ export class AuthResolver {
215222
}
216223

217224
validators.assertValidEmail(email);
218-
const valid = await this.token.verifyToken(TokenType.ChangeEmail, token, {
219-
credential: user.id,
220-
});
225+
const valid = await this.models.verificationToken.verify(
226+
TokenType.ChangeEmail,
227+
token,
228+
{
229+
credential: user.id,
230+
}
231+
);
221232

222233
if (!valid) {
223234
throw new InvalidEmailToken();
@@ -233,7 +244,7 @@ export class AuthResolver {
233244
}
234245
}
235246

236-
const verifyEmailToken = await this.token.createToken(
247+
const verifyEmailToken = await this.models.verificationToken.create(
237248
TokenType.VerifyEmail,
238249
user.id
239250
);
@@ -249,7 +260,10 @@ export class AuthResolver {
249260
@CurrentUser() user: CurrentUser,
250261
@Args('callbackUrl') callbackUrl: string
251262
) {
252-
const token = await this.token.createToken(TokenType.VerifyEmail, user.id);
263+
const token = await this.models.verificationToken.create(
264+
TokenType.VerifyEmail,
265+
user.id
266+
);
253267

254268
const url = this.url.link(callbackUrl, { token });
255269

@@ -266,9 +280,13 @@ export class AuthResolver {
266280
throw new EmailTokenNotFound();
267281
}
268282

269-
const valid = await this.token.verifyToken(TokenType.VerifyEmail, token, {
270-
credential: user.id,
271-
});
283+
const valid = await this.models.verificationToken.verify(
284+
TokenType.VerifyEmail,
285+
token,
286+
{
287+
credential: user.id,
288+
}
289+
);
272290

273291
if (!valid) {
274292
throw new InvalidEmailToken();
@@ -287,7 +305,7 @@ export class AuthResolver {
287305
@Args('userId') userId: string,
288306
@Args('callbackUrl') callbackUrl: string
289307
): Promise<string> {
290-
const token = await this.token.createToken(
308+
const token = await this.models.verificationToken.create(
291309
TokenType.ChangePassword,
292310
userId
293311
);

0 commit comments

Comments
 (0)