Skip to content

Commit

Permalink
feat: add logout functionality for user sessions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirogab committed Sep 15, 2024
1 parent 565a5f0 commit ab8093a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"trailingComma": "all",
"singleQuote": true,
"endOfLine": "auto",
"printWidth": 300,
"printWidth": 120,
"tabWidth": 2,
"semi": true
}
Expand Down
9 changes: 8 additions & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { AuthHelper } from './helpers';
import { ErrorHandlingMiddleware } from './middlewares';
import { EmailTemplateRepository, RegisterTokenRepository, SessionRepository, UserRepository } from './repositories';
import { AppRouter, AuthRouter, RegisterRouter, UserRouter } from './routers';
import { AuthService, CreateRegisterTokenService, CreateUserService, GetRegisterTokenService } from './services';
import {
AuthService,
CreateRegisterTokenService,
CreateUserService,
GetRegisterTokenService,
LogoutService,
} from './services';

// Adapters
container.registerSingleton<UniqueIdAdapter>('UniqueIdAdapter', UniqueIdAdapter);
Expand All @@ -34,6 +40,7 @@ container.registerSingleton<UserRepository>('UserRepository', UserRepository);
container.registerSingleton<CreateRegisterTokenService>('CreateRegisterTokenService', CreateRegisterTokenService);
container.registerSingleton<GetRegisterTokenService>('GetRegisterTokenService', GetRegisterTokenService);
container.registerSingleton<CreateUserService>('CreateUserService', CreateUserService);
container.registerSingleton<LogoutService>('LogoutService', LogoutService);
container.registerSingleton<AuthService>('AuthService', AuthService);

// Controllers
Expand Down
13 changes: 11 additions & 2 deletions src/controllers/auth.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
import { inject, injectable } from 'tsyringe';

import { HttpStatusCodesEnum } from '@/constants';
import type { AuthService, AuthServiceDto } from '@/interfaces';
import type { AuthService, AuthServiceDto, LogoutService } from '@/interfaces';

@injectable()
export class AuthController {
constructor(
@inject('AuthService')
private readonly authService: AuthService,

@inject('LogoutService')
private readonly logoutService: LogoutService,
) {}

public async auth(request: FastifyRequest, reply: FastifyReply) {
public async login(request: FastifyRequest, reply: FastifyReply) {
const { email, password } = request.body as AuthServiceDto;

const response = await this.authService.execute({ email, password });

return reply.code(HttpStatusCodesEnum.OK).send(response);
}

public async logout(_request: FastifyRequest, reply: FastifyReply) {
await this.logoutService.execute({ user_id: '123' });

return reply.code(HttpStatusCodesEnum.NO_CONTENT).send();
}
}
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export * from './routers/router';
export * from './services/create-register-token.service';
export * from './services/get-register-token.service';
export * from './services/create-user.service';
export * from './services/logout.service';
export * from './services/auth.service';
7 changes: 7 additions & 0 deletions src/interfaces/services/logout.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type LogoutServiceDto = {
user_id: string;
};

export interface LogoutService {
execute(dto: LogoutServiceDto): Promise<void>;
}
4 changes: 3 additions & 1 deletion src/routers/auth.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class AuthRouter implements Router {
_?: unknown,
done?: (err?: Error) => void,
) {
app.post('/', this.authController.auth.bind(this.authController));
app.post('/', this.authController.login.bind(this.authController));

app.delete('/', this.authController.logout.bind(this.authController));

if (done) {
done();
Expand Down
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './create-register-token.service';
export * from './get-register-token.service';
export * from './create-user.service';
export * from './logout.service';
export * from './auth.service';
19 changes: 19 additions & 0 deletions src/services/logout.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { inject, injectable } from 'tsyringe';

import type {
LogoutServiceDto,
LogoutService as LogoutServiceInterface,
SessionRepository,
} from '@/interfaces';

@injectable()
export class LogoutService implements LogoutServiceInterface {
constructor(
@inject('SessionRepository')
private sessionRepository: SessionRepository,
) {}

async execute({ user_id }: LogoutServiceDto): Promise<void> {
await this.sessionRepository.deleteByUserId({ user_id });
}
}

0 comments on commit ab8093a

Please sign in to comment.