Skip to content

Commit d32ec89

Browse files
committed
changed project structure, redesigned handlers implementation from function to class
1 parent dc5b6e9 commit d32ec89

33 files changed

+979
-960
lines changed

source/api/v1/auth/AuthPreHandler.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
2-
import { extractToken } from "../utils/common/TokenExtractor";
2+
import { extractToken } from "../shared/utils/common/TokenExtractor";
33
import { extractJwtPayload } from "./jwt/PayloadExtractor";
44
import { validateSignature } from "./jwt/SignatureValidator";
5-
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
5+
import { USER_EXCEPTIONS } from "../shared/exceptions/UserExceptions";
66
import { IAuthService } from "../services/interfaces/AuthServiceInterface";
7-
import { isException } from "../utils/guards/ExceptionGuard";
7+
import { isException } from "../shared/utils/guards/ExceptionGuard";
8+
9+
export type AuthentificationPreHandler = (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => void
810

911
export const authenticationFactory = (authService: IAuthService) =>
10-
async (request: FastifyRequest, reply: FastifyReply, _done: HookHandlerDoneFunction) => {
12+
async (request: FastifyRequest, reply: FastifyReply, _done: HookHandlerDoneFunction): Promise<AuthentificationPreHandler> => {
1113
const token = extractToken(request)
1214
if (!token) {
1315
reply.code(401).send(USER_EXCEPTIONS.NotAuthorized)
@@ -26,19 +28,11 @@ async (request: FastifyRequest, reply: FastifyReply, _done: HookHandlerDoneFunct
2628
return
2729
}
2830

29-
// try {
3031
const relevanceState = await authService.checkTokenRelevance(payload.login, token)
3132
if (isException(relevanceState)) {
3233
reply.code(
3334
relevanceState.statusCode
3435
).send(relevanceState)
3536
return
36-
}
37-
// } catch (exception: any) {
38-
// reply.code(
39-
// exception.statusCode
40-
// ).send(exception)
41-
// return
42-
// }
43-
37+
}
4438
}

source/api/v1/auth/jwt/SignatureValidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as jwt from 'jsonwebtoken'
2-
import { CONFIG } from '../../config/ServerConfiguration'
2+
import { CONFIG } from '../../shared/config/ServerConfiguration'
33

44
export const validateSignature = (token: string): boolean => {
55
try {

source/api/v1/auth/jwt/TokenGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as jwt from 'jsonwebtoken'
2-
import { CONFIG } from '../../config/ServerConfiguration'
2+
import { CONFIG } from '../../shared/config/ServerConfiguration'
33

44
export const generateToken = (login: string): string => {
55
return jwt.sign({ login }, CONFIG.jwtSecret, {expiresIn: CONFIG.jwtExpiration})
Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,79 @@
1-
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
1+
import { FastifyInstance } from "fastify";
22
import { IAuthService } from "../services/interfaces/AuthServiceInterface";
33
import { UserCredentials } from "../database/entities/User";
4-
import { AUTH_EXCEPTIONS } from "../exceptions/AuthExceptions";
4+
import { AUTH_EXCEPTIONS } from "../shared/exceptions/AuthExceptions";
55
import { AuthUserSchema, ChangePasswordSchema } from "../validation/schemas/AuthSchemas";
66
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor";
7-
import { extractToken } from "../utils/common/TokenExtractor";
8-
import { isException } from "../utils/guards/ExceptionGuard";
9-
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions";
7+
import { extractToken } from "../shared/utils/common/TokenExtractor";
8+
import { isException } from "../shared/utils/guards/ExceptionGuard";
9+
import { USER_EXCEPTIONS } from "../shared/exceptions/UserExceptions";
10+
import { Handler } from "./Handler";
11+
import { AuthentificationPreHandler } from "../auth/AuthPreHandler";
1012

11-
export const handleAuthRoutes = (
12-
server: FastifyInstance,
13-
authService: IAuthService,
14-
authenticate: (
15-
request: FastifyRequest,
16-
reply: FastifyReply,
17-
done: HookHandlerDoneFunction
18-
) => void
19-
) => {
20-
server.post<{
21-
Body: UserCredentials,
22-
Reply: {
23-
200: { token: string, expiresIn: string },
24-
400: typeof AUTH_EXCEPTIONS.WrongCredentials,
25-
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
26-
}
27-
}>("/auth", {
28-
schema: AuthUserSchema
29-
}, async (request, reply) => {
30-
const credentials: UserCredentials = request.body
31-
32-
const result = await authService.authorizeAndGenerateToken(
33-
credentials.email,
34-
credentials.password
35-
)
36-
if (isException(result)) {
37-
reply.code(result.statusCode).send(result)
38-
return
39-
}
13+
export class AuthHandler extends Handler<IAuthService> {
14+
constructor(
15+
server: FastifyInstance,
16+
authentificationPreHandler: AuthentificationPreHandler,
17+
authService: IAuthService
18+
) {
19+
super(server, authentificationPreHandler, authService)
20+
}
4021

41-
reply.code(200).send(result)
22+
public override handleRoutes(): void {
23+
this.server.post<{
24+
Body: UserCredentials,
25+
Reply: {
26+
200: { token: string, expiresIn: string },
27+
400: typeof AUTH_EXCEPTIONS.WrongCredentials,
28+
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
29+
}
30+
}>("/auth", {
31+
schema: AuthUserSchema
32+
}, async (request, reply) => {
33+
const credentials: UserCredentials = request.body
34+
35+
const result = await this.service.authorizeAndGenerateToken(
36+
credentials.email,
37+
credentials.password
38+
)
39+
if (isException(result)) {
40+
reply.code(result.statusCode).send(result)
41+
return
42+
}
4243

43-
})
44-
45-
server.patch<{
46-
Body: { oldPassword: string, newPassword: string },
47-
Reply: {
48-
200: { success: true },
49-
400: typeof AUTH_EXCEPTIONS.WrongCredentials | typeof AUTH_EXCEPTIONS.NewPasswordIsSame,
50-
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
51-
}
52-
}>("/auth/password", {
53-
schema: ChangePasswordSchema,
54-
preHandler: authenticate
55-
}, async (request, reply) => {
56-
const passwords = request.body
57-
const { login } = extractJwtPayload(
58-
extractToken(request)
59-
)
60-
61-
const state = await authService.changePassword(
62-
login,
63-
passwords.oldPassword,
64-
passwords.newPassword
65-
)
66-
67-
if (isException(state)) {
68-
reply.code(state.statusCode).send(state)
69-
return
70-
}
44+
reply.code(200).send(result)
7145

72-
reply.code(200).send(state)
73-
74-
})
75-
}
46+
})
47+
48+
this.server.patch<{
49+
Body: { oldPassword: string, newPassword: string },
50+
Reply: {
51+
200: { success: true },
52+
400: typeof AUTH_EXCEPTIONS.WrongCredentials | typeof AUTH_EXCEPTIONS.NewPasswordIsSame,
53+
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable
54+
}
55+
}>("/auth/password", {
56+
schema: ChangePasswordSchema,
57+
preHandler: this.authentificationPreHandler
58+
}, async (request, reply) => {
59+
const passwords = request.body
60+
const { login } = extractJwtPayload(
61+
extractToken(request)
62+
)
63+
64+
const state = await this.service.changePassword(
65+
login,
66+
passwords.oldPassword,
67+
passwords.newPassword
68+
)
69+
70+
if (isException(state)) {
71+
reply.code(state.statusCode).send(state)
72+
return
73+
}
74+
75+
reply.code(200).send(state)
76+
77+
})
78+
}
79+
}

source/api/v1/handlers/Handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify";
2+
import { AuthentificationPreHandler } from "../auth/AuthPreHandler";
3+
4+
export abstract class Handler<TService> {
5+
constructor(
6+
protected server: FastifyInstance,
7+
protected authentificationPreHandler: AuthentificationPreHandler,
8+
protected service: TService
9+
) {}
10+
11+
public abstract handleRoutes(): void
12+
}

0 commit comments

Comments
 (0)