Skip to content

Commit 1064946

Browse files
committed
chore(api): auth module refactoring
1 parent 52696ab commit 1064946

File tree

11 files changed

+45
-101
lines changed

11 files changed

+45
-101
lines changed

modules/libs/protocol/auth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ export const RevokedTokenStorageKey = (token: JwtToken) => `tokens:revoked:${tok
6565
/* Authentication */
6666
/* -------------------------------------------------------------------------- */
6767

68-
export interface OtpLogInRequest {
68+
export interface OtpSignInRequest {
6969
/** Login to sign up with */
7070
login: string;
7171

7272
/** OTP to validate */
7373
otp: string;
7474
}
7575

76-
export interface OtpLogInResponse {
76+
export interface OtpSignInResponse {
7777
/** Access token to authenticate user */
7878
accessToken: string;
7979

@@ -94,12 +94,12 @@ export interface RefreshTokensResponse {
9494
refreshToken: string;
9595
}
9696

97-
export interface LogOutRequest {
97+
export interface SignOutRequest {
9898
/** Refresh token */
9999
refreshToken: string;
100100
}
101101

102-
export interface LogOutResponse {
102+
export interface SignOutResponse {
103103
}
104104

105105
/* -------------------------------------------------------------------------- */

modules/libs/protocol/routes.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export const Routes = (baseUrl: string = '') => ({
22
auth: {
33
root: () => `${baseUrl}/auth`,
4-
login: (method: string) => `${baseUrl}/auth/login/${method}`,
5-
logout: () => `${baseUrl}/auth/logout`,
4+
signIn: (method: string) => `${baseUrl}/auth/signin/${method}`,
5+
signOut: () => `${baseUrl}/auth/signout`,
6+
7+
signup: (method: string) => `${baseUrl}/auth/signup/${method}`,
68
tokens: {
7-
refresh: () => `${baseUrl}/auth/tokens/refresh`,
9+
refresh: () => `${baseUrl}/auth/refresh`,
810
},
911
profile: () => `${baseUrl}/auth/profile`,
1012
},

modules/services/api/src/auth/auth.module.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { JwtModule } from '@nestjs/jwt';
33
import { TypeOrmModule } from '@nestjs/typeorm';
44
import { Role, User, UserRole } from '@vidya/entities';
55

6-
import { LoginController } from './controllers/login.controller';
76
import { OtpController } from './controllers/otp.controller';
8-
import { ProfileController } from './controllers/profile.controller';
97
import { TokensController } from './controllers/tokens.controller';
8+
import { UserAuthenticationController } from './controllers/user-authentication.controller';
109
import { AuthRolesMappingProfile } from './mappers/roles.mapper';
1110
import { AuthService } from './services/auth.service';
1211
import { AuthUsersService } from './services/auth-users.service';
@@ -18,12 +17,7 @@ import { RevokedTokensService } from './services/revokedTokens.service';
1817
TypeOrmModule.forFeature([User, Role, UserRole]),
1918
JwtModule.register({ global: true }),
2019
],
21-
controllers: [
22-
LoginController,
23-
OtpController,
24-
TokensController,
25-
ProfileController,
26-
],
20+
controllers: [UserAuthenticationController, OtpController, TokensController],
2721
providers: [
2822
OtpService,
2923
AuthUsersService,

modules/services/api/src/auth/controllers/otp.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { OtpService } from '@vidya/api/auth/services';
1818
import { Routes } from '@vidya/protocol';
1919

2020
@Controller()
21-
@ApiTags('Authentication')
21+
@ApiTags('Authentication :: One-Time Password')
2222
export class OtpController {
2323
constructor(private readonly otpService: OtpService) {}
2424

modules/services/api/src/auth/controllers/profile.controller.ts

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

modules/services/api/src/auth/controllers/login.controller.ts renamed to modules/services/api/src/auth/controllers/user-authentication.controller.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { JwtToken, OtpType, Routes } from '@vidya/protocol';
3030

3131
@Controller()
3232
@ApiTags('Authentication')
33-
export class LoginController {
33+
export class UserAuthenticationController {
3434
constructor(
3535
@Inject(AuthConfig.KEY)
3636
private readonly authConfig: ConfigType<typeof AuthConfig>,
@@ -41,20 +41,20 @@ export class LoginController {
4141
) {}
4242

4343
/* -------------------------------------------------------------------------- */
44-
/* POST /auth/login/otp */
44+
/* POST /auth/signin/otp */
4545
/* -------------------------------------------------------------------------- */
4646

47-
@Post(Routes().auth.login('otp'))
47+
@Post(Routes().auth.signIn('otp'))
4848
@ApiOperation({
49-
summary: 'Authorizes user',
50-
operationId: 'auth::login',
49+
summary: 'Signs user in with OTP',
50+
operationId: 'auth::signIn',
5151
description:
52-
`Authorizes user by OTP.\n\n` +
53-
`Returns access and refresh tokens if the user has been authorized.`,
52+
`Signs user in with one-time password.\n\n` +
53+
`Returns access and refresh tokens if the user has been authenticated.`,
5454
})
5555
@ApiOkResponse({
56-
type: dto.OtpLogInRequest,
57-
description: 'User has been authorized.',
56+
type: dto.OtpSignInRequest,
57+
description: 'User has been authenticated.',
5858
})
5959
@ApiUnauthorizedResponse({
6060
type: dto.ErrorResponse,
@@ -64,10 +64,10 @@ export class LoginController {
6464
type: dto.ErrorResponse,
6565
description: 'Too many requests',
6666
})
67-
async loginWithOtp(
68-
@Body() request: dto.OtpLogInRequest,
69-
): Promise<dto.OtpLogInResponse> {
70-
// TODO rate limit login attempts
67+
async signinWithOtp(
68+
@Body() request: dto.OtpSignInRequest,
69+
): Promise<dto.OtpSignInResponse> {
70+
// TODO rate limit login attempts by login
7171

7272
// validate OTP, if invalid send 401 Unauthorized response
7373
const otp = await this.otpService.validate(request.login, request.otp);
@@ -91,7 +91,7 @@ export class LoginController {
9191
: undefined,
9292
);
9393

94-
return new dto.OtpLogInResponse({
94+
return new dto.OtpSignInResponse({
9595
accessToken: tokens.accessToken,
9696
refreshToken: tokens.refreshToken,
9797
});
@@ -101,17 +101,18 @@ export class LoginController {
101101
/* POST /auth/logout */
102102
/* -------------------------------------------------------------------------- */
103103

104-
@Post(Routes().auth.logout())
104+
@Post(Routes().auth.signOut())
105105
@UseGuards(AuthenticatedUser)
106106
@ApiBearerAuth()
107107
@ApiOperation({
108-
summary: 'Log out user',
109-
operationId: 'auth::logout',
108+
summary: 'Signs user out',
109+
operationId: 'auth::signOut',
110110
description:
111-
`Log out user.\n\n` + `Revokes the refresh token and logs out the user.`,
111+
`Signs user out.\n\n` +
112+
`Revokes the refresh token and signs the user out.`,
112113
})
113114
@ApiOkResponse({
114-
type: dto.LogOutResponse,
115+
type: dto.SignOutResponse,
115116
description: 'User has been logged out.',
116117
})
117118
@ApiBadRequestResponse({
@@ -123,9 +124,9 @@ export class LoginController {
123124
description: 'Unauthorized request.',
124125
})
125126
async logoutUser(
126-
@Body() request: dto.LogOutRequest,
127+
@Body() request: dto.SignOutRequest,
127128
@UserAccessToken() userAccessToken: JwtToken,
128-
): Promise<dto.LogOutResponse> {
129+
): Promise<dto.SignOutResponse> {
129130
// revoke access token to prevent reusing it
130131
await this.revokedTokensService.revoke(userAccessToken);
131132

@@ -136,6 +137,6 @@ export class LoginController {
136137
}
137138

138139
// user logged out
139-
return new dto.LogOutResponse();
140+
return new dto.SignOutResponse();
140141
}
141142
}

modules/services/api/src/auth/dto/auth.dto.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { IsNotEmpty, IsString } from 'class-validator';
77
/* Authentcation */
88
/* -------------------------------------------------------------------------- */
99

10-
export class OtpLogInRequest implements protocol.OtpLogInRequest {
10+
export class OtpSignInRequest implements protocol.OtpSignInRequest {
1111
@ApiProperty({ example: 'example@example.com' })
1212
@IsNotEmpty()
1313
login: string;
@@ -18,7 +18,7 @@ export class OtpLogInRequest implements protocol.OtpLogInRequest {
1818
otp: string;
1919
}
2020

21-
export class OtpLogInResponse implements protocol.OtpLogInResponse {
21+
export class OtpSignInResponse implements protocol.OtpSignInResponse {
2222
constructor(options: { accessToken: string; refreshToken: string }) {
2323
this.accessToken = options.accessToken;
2424
this.refreshToken = options.refreshToken;
@@ -40,14 +40,14 @@ export class RefreshTokensRequest implements protocol.RefreshTokensRequest {
4040
refreshToken: string;
4141
}
4242

43-
export class LogOutRequest implements protocol.LogOutRequest {
43+
export class SignOutRequest implements protocol.SignOutRequest {
4444
@ApiProperty({ example: 'refreshToken' })
4545
@IsString()
4646
@IsNotEmpty()
4747
refreshToken: string;
4848
}
4949

50-
export class LogOutResponse implements protocol.LogOutResponse {}
50+
export class SignOutResponse implements protocol.SignOutResponse {}
5151

5252
/* -------------------------------------------------------------------------- */
5353
/* Tokens */

modules/services/api/src/edu/controllers/roles/roles.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const Crud = CrudDecorators({
3030
});
3131

3232
@Controller()
33-
@ApiTags('Roles')
33+
@ApiTags('Education :: Roles')
3434
@ApiBearerAuth()
3535
@UseGuards(AuthenticatedUser)
3636
export class RolesController {

modules/services/api/src/edu/controllers/users/userRoles.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Crud = CrudDecorators({
1919
});
2020

2121
@Controller()
22-
@ApiTags('Users')
22+
@ApiTags('Education :: Users')
2323
@UseGuards(AuthenticatedUser)
2424
export class UserRolesController {
2525
constructor(

modules/services/api/src/edu/controllers/users/users.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const Crud = CrudDecorators({
3030

3131
@Controller()
3232
@ApiBearerAuth()
33-
@ApiTags('Users')
33+
@ApiTags('Education :: Users')
3434
@UseGuards(AuthenticatedUser)
3535
export class UsersController {
3636
constructor(

0 commit comments

Comments
 (0)