1
- import { FastifyInstance , FastifyReply , FastifyRequest , HookHandlerDoneFunction } from "fastify" ;
1
+ import { FastifyInstance } from "fastify" ;
2
2
import { IAuthService } from "../services/interfaces/AuthServiceInterface" ;
3
3
import { UserCredentials } from "../database/entities/User" ;
4
- import { AUTH_EXCEPTIONS } from "../exceptions/AuthExceptions" ;
4
+ import { AUTH_EXCEPTIONS } from "../shared/ exceptions/AuthExceptions" ;
5
5
import { AuthUserSchema , ChangePasswordSchema } from "../validation/schemas/AuthSchemas" ;
6
6
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" ;
10
12
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
+ }
40
21
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
+ }
42
43
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 )
71
45
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
+ }
0 commit comments