-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed project structure, redesigned handlers implementation from fu…
…nction to class
- Loading branch information
Showing
33 changed files
with
979 additions
and
960 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,79 @@ | ||
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify"; | ||
import { FastifyInstance } from "fastify"; | ||
import { IAuthService } from "../services/interfaces/AuthServiceInterface"; | ||
import { UserCredentials } from "../database/entities/User"; | ||
import { AUTH_EXCEPTIONS } from "../exceptions/AuthExceptions"; | ||
import { AUTH_EXCEPTIONS } from "../shared/exceptions/AuthExceptions"; | ||
import { AuthUserSchema, ChangePasswordSchema } from "../validation/schemas/AuthSchemas"; | ||
import { extractJwtPayload } from "../auth/jwt/PayloadExtractor"; | ||
import { extractToken } from "../utils/common/TokenExtractor"; | ||
import { isException } from "../utils/guards/ExceptionGuard"; | ||
import { USER_EXCEPTIONS } from "../exceptions/UserExceptions"; | ||
import { extractToken } from "../shared/utils/common/TokenExtractor"; | ||
import { isException } from "../shared/utils/guards/ExceptionGuard"; | ||
import { USER_EXCEPTIONS } from "../shared/exceptions/UserExceptions"; | ||
import { Handler } from "./Handler"; | ||
import { AuthentificationPreHandler } from "../auth/AuthPreHandler"; | ||
|
||
export const handleAuthRoutes = ( | ||
server: FastifyInstance, | ||
authService: IAuthService, | ||
authenticate: ( | ||
request: FastifyRequest, | ||
reply: FastifyReply, | ||
done: HookHandlerDoneFunction | ||
) => void | ||
) => { | ||
server.post<{ | ||
Body: UserCredentials, | ||
Reply: { | ||
200: { token: string, expiresIn: string }, | ||
400: typeof AUTH_EXCEPTIONS.WrongCredentials, | ||
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable | ||
} | ||
}>("/auth", { | ||
schema: AuthUserSchema | ||
}, async (request, reply) => { | ||
const credentials: UserCredentials = request.body | ||
|
||
const result = await authService.authorizeAndGenerateToken( | ||
credentials.email, | ||
credentials.password | ||
) | ||
if (isException(result)) { | ||
reply.code(result.statusCode).send(result) | ||
return | ||
} | ||
export class AuthHandler extends Handler<IAuthService> { | ||
constructor( | ||
server: FastifyInstance, | ||
authentificationPreHandler: AuthentificationPreHandler, | ||
authService: IAuthService | ||
) { | ||
super(server, authentificationPreHandler, authService) | ||
} | ||
|
||
reply.code(200).send(result) | ||
public override handleRoutes(): void { | ||
this.server.post<{ | ||
Body: UserCredentials, | ||
Reply: { | ||
200: { token: string, expiresIn: string }, | ||
400: typeof AUTH_EXCEPTIONS.WrongCredentials, | ||
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable | ||
} | ||
}>("/auth", { | ||
schema: AuthUserSchema | ||
}, async (request, reply) => { | ||
const credentials: UserCredentials = request.body | ||
|
||
const result = await this.service.authorizeAndGenerateToken( | ||
credentials.email, | ||
credentials.password | ||
) | ||
if (isException(result)) { | ||
reply.code(result.statusCode).send(result) | ||
return | ||
} | ||
|
||
}) | ||
|
||
server.patch<{ | ||
Body: { oldPassword: string, newPassword: string }, | ||
Reply: { | ||
200: { success: true }, | ||
400: typeof AUTH_EXCEPTIONS.WrongCredentials | typeof AUTH_EXCEPTIONS.NewPasswordIsSame, | ||
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable | ||
} | ||
}>("/auth/password", { | ||
schema: ChangePasswordSchema, | ||
preHandler: authenticate | ||
}, async (request, reply) => { | ||
const passwords = request.body | ||
const { login } = extractJwtPayload( | ||
extractToken(request) | ||
) | ||
|
||
const state = await authService.changePassword( | ||
login, | ||
passwords.oldPassword, | ||
passwords.newPassword | ||
) | ||
|
||
if (isException(state)) { | ||
reply.code(state.statusCode).send(state) | ||
return | ||
} | ||
reply.code(200).send(result) | ||
|
||
reply.code(200).send(state) | ||
|
||
}) | ||
} | ||
}) | ||
|
||
this.server.patch<{ | ||
Body: { oldPassword: string, newPassword: string }, | ||
Reply: { | ||
200: { success: true }, | ||
400: typeof AUTH_EXCEPTIONS.WrongCredentials | typeof AUTH_EXCEPTIONS.NewPasswordIsSame, | ||
503: typeof AUTH_EXCEPTIONS.ServiceUnavailable | typeof USER_EXCEPTIONS.ServiceUnavailable | ||
} | ||
}>("/auth/password", { | ||
schema: ChangePasswordSchema, | ||
preHandler: this.authentificationPreHandler | ||
}, async (request, reply) => { | ||
const passwords = request.body | ||
const { login } = extractJwtPayload( | ||
extractToken(request) | ||
) | ||
|
||
const state = await this.service.changePassword( | ||
login, | ||
passwords.oldPassword, | ||
passwords.newPassword | ||
) | ||
|
||
if (isException(state)) { | ||
reply.code(state.statusCode).send(state) | ||
return | ||
} | ||
|
||
reply.code(200).send(state) | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { FastifyInstance, FastifyReply, FastifyRequest, HookHandlerDoneFunction } from "fastify"; | ||
import { AuthentificationPreHandler } from "../auth/AuthPreHandler"; | ||
|
||
export abstract class Handler<TService> { | ||
constructor( | ||
protected server: FastifyInstance, | ||
protected authentificationPreHandler: AuthentificationPreHandler, | ||
protected service: TService | ||
) {} | ||
|
||
public abstract handleRoutes(): void | ||
} |
Oops, something went wrong.