Skip to content

Commit

Permalink
refactor(server): use verificationToken model instead of tokenService (
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 14, 2025
1 parent 290b207 commit ee99b0c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 271 deletions.
93 changes: 0 additions & 93 deletions packages/backend/server/src/__tests__/auth/token.spec.ts

This file was deleted.

19 changes: 13 additions & 6 deletions packages/backend/server/src/core/auth/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import {
URLHelper,
UseNamedGuard,
} from '../../base';
import { Models, TokenType } from '../../models';
import { UserService } from '../user';
import { validators } from '../utils/validators';
import { Public } from './guard';
import { AuthService } from './service';
import { CurrentUser, Session } from './session';
import { TokenService, TokenType } from './token';

interface PreflightResponse {
registered: boolean;
Expand All @@ -57,7 +57,7 @@ export class AuthController {
private readonly url: URLHelper,
private readonly auth: AuthService,
private readonly user: UserService,
private readonly token: TokenService,
private readonly models: Models,
private readonly config: Config,
private readonly runtime: Runtime
) {
Expand Down Expand Up @@ -194,7 +194,10 @@ export class AuthController {
}
}

const token = await this.token.createToken(TokenType.SignIn, email);
const token = await this.models.verificationToken.create(
TokenType.SignIn,
email
);

const magicLink = this.url.link(callbackUrl, {
token,
Expand Down Expand Up @@ -248,9 +251,13 @@ export class AuthController {

validators.assertValidEmail(email);

const tokenRecord = await this.token.verifyToken(TokenType.SignIn, token, {
credential: email,
});
const tokenRecord = await this.models.verificationToken.verify(
TokenType.SignIn,
token,
{
credential: email,
}
);

if (!tokenRecord) {
throw new InvalidEmailToken();
Expand Down
6 changes: 2 additions & 4 deletions packages/backend/server/src/core/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ import { AuthController } from './controller';
import { AuthGuard, AuthWebsocketOptionsProvider } from './guard';
import { AuthResolver } from './resolver';
import { AuthService } from './service';
import { TokenService, TokenType } from './token';

@Module({
imports: [FeatureModule, UserModule, QuotaModule],
providers: [
AuthService,
AuthResolver,
TokenService,
AuthGuard,
AuthWebsocketOptionsProvider,
],
exports: [AuthService, AuthGuard, AuthWebsocketOptionsProvider, TokenService],
exports: [AuthService, AuthGuard, AuthWebsocketOptionsProvider],
controllers: [AuthController],
})
export class AuthModule {}

export * from './guard';
export { ClientTokenType } from './resolver';
export { AuthService, TokenService, TokenType };
export { AuthService };
export * from './session';
52 changes: 35 additions & 17 deletions packages/backend/server/src/core/auth/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import {
Throttle,
URLHelper,
} from '../../base';
import { Models, TokenType } from '../../models';
import { Admin } from '../common';
import { UserService } from '../user';
import { UserType } from '../user/types';
import { validators } from '../utils/validators';
import { Public } from './guard';
import { AuthService } from './service';
import { CurrentUser } from './session';
import { TokenService, TokenType } from './token';

@ObjectType('tokenType')
export class ClientTokenType {
Expand All @@ -49,7 +49,7 @@ export class AuthResolver {
private readonly url: URLHelper,
private readonly auth: AuthService,
private readonly user: UserService,
private readonly token: TokenService
private readonly models: Models
) {}

@SkipThrottle()
Expand Down Expand Up @@ -96,7 +96,7 @@ export class AuthResolver {
}

// NOTE: Set & Change password are using the same token type.
const valid = await this.token.verifyToken(
const valid = await this.models.verificationToken.verify(
TokenType.ChangePassword,
token,
{
Expand All @@ -121,9 +121,13 @@ export class AuthResolver {
@Args('email') email: string
) {
// @see [sendChangeEmail]
const valid = await this.token.verifyToken(TokenType.VerifyEmail, token, {
credential: user.id,
});
const valid = await this.models.verificationToken.verify(
TokenType.VerifyEmail,
token,
{
credential: user.id,
}
);

if (!valid) {
throw new InvalidEmailToken();
Expand Down Expand Up @@ -152,7 +156,7 @@ export class AuthResolver {
throw new EmailVerificationRequired();
}

const token = await this.token.createToken(
const token = await this.models.verificationToken.create(
TokenType.ChangePassword,
user.id
);
Expand Down Expand Up @@ -195,7 +199,10 @@ export class AuthResolver {
throw new EmailVerificationRequired();
}

const token = await this.token.createToken(TokenType.ChangeEmail, user.id);
const token = await this.models.verificationToken.create(
TokenType.ChangeEmail,
user.id
);

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

Expand All @@ -215,9 +222,13 @@ export class AuthResolver {
}

validators.assertValidEmail(email);
const valid = await this.token.verifyToken(TokenType.ChangeEmail, token, {
credential: user.id,
});
const valid = await this.models.verificationToken.verify(
TokenType.ChangeEmail,
token,
{
credential: user.id,
}
);

if (!valid) {
throw new InvalidEmailToken();
Expand All @@ -233,7 +244,7 @@ export class AuthResolver {
}
}

const verifyEmailToken = await this.token.createToken(
const verifyEmailToken = await this.models.verificationToken.create(
TokenType.VerifyEmail,
user.id
);
Expand All @@ -249,7 +260,10 @@ export class AuthResolver {
@CurrentUser() user: CurrentUser,
@Args('callbackUrl') callbackUrl: string
) {
const token = await this.token.createToken(TokenType.VerifyEmail, user.id);
const token = await this.models.verificationToken.create(
TokenType.VerifyEmail,
user.id
);

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

Expand All @@ -266,9 +280,13 @@ export class AuthResolver {
throw new EmailTokenNotFound();
}

const valid = await this.token.verifyToken(TokenType.VerifyEmail, token, {
credential: user.id,
});
const valid = await this.models.verificationToken.verify(
TokenType.VerifyEmail,
token,
{
credential: user.id,
}
);

if (!valid) {
throw new InvalidEmailToken();
Expand All @@ -287,7 +305,7 @@ export class AuthResolver {
@Args('userId') userId: string,
@Args('callbackUrl') callbackUrl: string
): Promise<string> {
const token = await this.token.createToken(
const token = await this.models.verificationToken.create(
TokenType.ChangePassword,
userId
);
Expand Down
Loading

0 comments on commit ee99b0c

Please sign in to comment.