From fa8044ff5355b6bf2d7a987781fa0a7eb5020a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 09:29:15 -0600 Subject: [PATCH 01/17] actualizando paths --- backend/src/admin/dto/create-admin.dto.ts | 4 ++-- backend/src/admin/entities/admin.entity.ts | 4 ++-- backend/src/auth/admin/dto/admin-signin-res.dto.ts | 2 +- backend/src/town/dto/create-town-trad.dto.ts | 2 +- backend/src/town/entities/town-traduction.entity.ts | 2 +- backend/src/town/town.service.ts | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/backend/src/admin/dto/create-admin.dto.ts b/backend/src/admin/dto/create-admin.dto.ts index 2b67b15a..c5a5ca71 100644 --- a/backend/src/admin/dto/create-admin.dto.ts +++ b/backend/src/admin/dto/create-admin.dto.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; -import { ADMIN_ROLE } from 'src/enum/admin-role.enum'; -import { UserStatus } from 'src/enum/user-status.enum'; +import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; +import { UserStatus } from 'src/shared/enum/user-status.enum'; export class CreateAdminDto { @ApiProperty() diff --git a/backend/src/admin/entities/admin.entity.ts b/backend/src/admin/entities/admin.entity.ts index 2edb68eb..b7fc6cbd 100644 --- a/backend/src/admin/entities/admin.entity.ts +++ b/backend/src/admin/entities/admin.entity.ts @@ -1,5 +1,5 @@ -import { ADMIN_ROLE } from 'src/enum/admin-role.enum'; -import { UserStatus } from 'src/enum/user-status.enum'; +import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; +import { UserStatus } from 'src/shared/enum/user-status.enum'; import { Entity, Column, PrimaryColumn } from 'typeorm'; @Entity() diff --git a/backend/src/auth/admin/dto/admin-signin-res.dto.ts b/backend/src/auth/admin/dto/admin-signin-res.dto.ts index b19eefc7..c2aeac79 100644 --- a/backend/src/auth/admin/dto/admin-signin-res.dto.ts +++ b/backend/src/auth/admin/dto/admin-signin-res.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { ADMIN_ROLE } from 'src/enum/admin-role.enum'; +import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; export class AdminSigninResDto { @ApiProperty() diff --git a/backend/src/town/dto/create-town-trad.dto.ts b/backend/src/town/dto/create-town-trad.dto.ts index bb0bba99..eb9058fc 100644 --- a/backend/src/town/dto/create-town-trad.dto.ts +++ b/backend/src/town/dto/create-town-trad.dto.ts @@ -1,4 +1,4 @@ -import { LANGUAGES } from 'src/enum/languages.enum'; +import { LANGUAGES } from 'src/shared/enum/languages.enum'; export class CreateTownTraductionDto { townId: number; diff --git a/backend/src/town/entities/town-traduction.entity.ts b/backend/src/town/entities/town-traduction.entity.ts index 77922ec5..42a4d3f4 100644 --- a/backend/src/town/entities/town-traduction.entity.ts +++ b/backend/src/town/entities/town-traduction.entity.ts @@ -1,6 +1,6 @@ import { Entity, Column, PrimaryColumn, ManyToOne } from 'typeorm'; import { Town } from './town.entity'; -import { LANGUAGES } from 'src/enum/languages.enum'; +import { LANGUAGES } from 'src/shared/enum/languages.enum'; @Entity() export class TownTraduction { @PrimaryColumn({ name: 'townId' }) diff --git a/backend/src/town/town.service.ts b/backend/src/town/town.service.ts index 6f04b289..badd465d 100644 --- a/backend/src/town/town.service.ts +++ b/backend/src/town/town.service.ts @@ -6,7 +6,7 @@ import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; import { StateService } from 'src/state/state.service'; import { TownTraduction } from './entities/town-traduction.entity'; import { CreateTownTraductionDto } from './dto/create-town-trad.dto'; -import { LANGUAGES } from 'src/enum/languages.enum'; +import { LANGUAGES } from 'src/shared/enum/languages.enum'; import { DataSource } from 'typeorm'; import { ServerConstants } from 'src/constants/server.contants'; import { TownResDto } from './dto/town-res.dto'; -- GitLab From f81d342e4fb98527a574fc8ae962637019447503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 09:31:52 -0600 Subject: [PATCH 02/17] moviendo archivos --- backend/src/enum/admin-role.enum.ts | 4 ---- backend/src/enum/languages.enum.ts | 4 ---- backend/src/enum/user-status.enum.ts | 4 ---- 3 files changed, 12 deletions(-) delete mode 100644 backend/src/enum/admin-role.enum.ts delete mode 100644 backend/src/enum/languages.enum.ts delete mode 100644 backend/src/enum/user-status.enum.ts diff --git a/backend/src/enum/admin-role.enum.ts b/backend/src/enum/admin-role.enum.ts deleted file mode 100644 index 52e5c067..00000000 --- a/backend/src/enum/admin-role.enum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum ADMIN_ROLE { - ADMIN = 'admin', - SUPERADMIN = 'superadmin', -} diff --git a/backend/src/enum/languages.enum.ts b/backend/src/enum/languages.enum.ts deleted file mode 100644 index 78a3c146..00000000 --- a/backend/src/enum/languages.enum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum LANGUAGES { - EN = 'en', - ES = 'es', -} diff --git a/backend/src/enum/user-status.enum.ts b/backend/src/enum/user-status.enum.ts deleted file mode 100644 index 8a81d944..00000000 --- a/backend/src/enum/user-status.enum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum UserStatus { - ACTIVE = 'active', - INACTIVE = 'inactive', -} -- GitLab From 1c07b789026a59c5f0392fa2b3dde73ce157a984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 10:27:58 -0600 Subject: [PATCH 03/17] agregando decorador para authenticacion --- backend/src/auth/role.decorator.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backend/src/auth/role.decorator.ts diff --git a/backend/src/auth/role.decorator.ts b/backend/src/auth/role.decorator.ts new file mode 100644 index 00000000..b69b3734 --- /dev/null +++ b/backend/src/auth/role.decorator.ts @@ -0,0 +1,3 @@ +import { Reflector } from '@nestjs/core'; + +export const Roles = Reflector.createDecorator(); -- GitLab From e482aa1a951f22dbd130e7f656b30ced816b8d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 10:28:48 -0600 Subject: [PATCH 04/17] moviendo archivos --- backend/src/shared/enum/admin-role.enum.ts | 6 ++++++ backend/src/shared/enum/languages.enum.ts | 4 ++++ backend/src/shared/enum/user-status.enum.ts | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 backend/src/shared/enum/admin-role.enum.ts create mode 100644 backend/src/shared/enum/languages.enum.ts create mode 100644 backend/src/shared/enum/user-status.enum.ts diff --git a/backend/src/shared/enum/admin-role.enum.ts b/backend/src/shared/enum/admin-role.enum.ts new file mode 100644 index 00000000..b3aeeef1 --- /dev/null +++ b/backend/src/shared/enum/admin-role.enum.ts @@ -0,0 +1,6 @@ +export enum ADMIN_ROLE { + ADMIN = 'admin', + SUPERADMIN = 'superadmin', +} + +export const ADMIN_ROLES = [ADMIN_ROLE.ADMIN, ADMIN_ROLE.SUPERADMIN]; diff --git a/backend/src/shared/enum/languages.enum.ts b/backend/src/shared/enum/languages.enum.ts new file mode 100644 index 00000000..78a3c146 --- /dev/null +++ b/backend/src/shared/enum/languages.enum.ts @@ -0,0 +1,4 @@ +export enum LANGUAGES { + EN = 'en', + ES = 'es', +} diff --git a/backend/src/shared/enum/user-status.enum.ts b/backend/src/shared/enum/user-status.enum.ts new file mode 100644 index 00000000..8a81d944 --- /dev/null +++ b/backend/src/shared/enum/user-status.enum.ts @@ -0,0 +1,4 @@ +export enum UserStatus { + ACTIVE = 'active', + INACTIVE = 'inactive', +} -- GitLab From 2c5b792d871429fbac21bd9e7ca82e7c1d9ae042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 10:30:30 -0600 Subject: [PATCH 05/17] creando guard para la autenticacione --- backend/src/auth/admin/auth.guard.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 backend/src/auth/admin/auth.guard.ts diff --git a/backend/src/auth/admin/auth.guard.ts b/backend/src/auth/admin/auth.guard.ts new file mode 100644 index 00000000..2bcfacf8 --- /dev/null +++ b/backend/src/auth/admin/auth.guard.ts @@ -0,0 +1,24 @@ +import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common'; +import { Reflector } from '@nestjs/core'; +import { AuthService } from 'src/shared/service/auth.service'; +import { Roles } from '../role.decorator'; + +@Injectable() +export class AuthGuard implements CanActivate { + constructor( + private authService: AuthService, + private reflector: Reflector, + ) {} + async canActivate(context: ExecutionContext): Promise { + const requiredRole = this.reflector.get(Roles, context.getHandler()); + console.log(requiredRole); + + if (!requiredRole) return true; + const request = context.switchToHttp().getRequest(); + const { authorization }: any = request.headers; + if (!authorization) throw new UnauthorizedException('session expired! Please sign In'); + const role = await this.authService.validateToken(authorization); + if (!requiredRole.includes(role)) throw new UnauthorizedException('Unauthorized access'); + return true; + } +} -- GitLab From 48963cf09d4600141ffa5542a373f844a89cfa1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 10:30:58 -0600 Subject: [PATCH 06/17] agregando modulo general para la auth --- backend/src/shared/service/auth.module.ts | 11 +++++++++++ backend/src/shared/service/auth.service.ts | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 backend/src/shared/service/auth.module.ts create mode 100644 backend/src/shared/service/auth.service.ts diff --git a/backend/src/shared/service/auth.module.ts b/backend/src/shared/service/auth.module.ts new file mode 100644 index 00000000..02d7b024 --- /dev/null +++ b/backend/src/shared/service/auth.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { JwtService } from '@nestjs/jwt'; +import { AuthService } from './auth.service'; + +@Module({ + controllers: [], + providers: [JwtService, AuthService], + imports: [], + exports: [AuthService], +}) +export class AuthModule {} diff --git a/backend/src/shared/service/auth.service.ts b/backend/src/shared/service/auth.service.ts new file mode 100644 index 00000000..72d99288 --- /dev/null +++ b/backend/src/shared/service/auth.service.ts @@ -0,0 +1,18 @@ +import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { JwtService } from '@nestjs/jwt'; +import { JwtConstants } from 'src/constants/jwt.constants'; +import { PayloadJwtDto } from 'src/shared/dto/payload-jwt.dto'; + +@Injectable() +export class AuthService { + constructor(private jwtService: JwtService) {} + + async validateToken(token: string): Promise { + try { + const payload: PayloadJwtDto = await this.jwtService.verify(token, { secret: JwtConstants.SECRET }); + return payload.role; + } catch (error) { + throw new UnauthorizedException('Invalid token'); + } + } +} -- GitLab From c300975f6a20c187bfba027df418163bddbd60dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 10:31:29 -0600 Subject: [PATCH 07/17] agregando try catch --- backend/src/admin/admin.service.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index e614d781..3ec4fcc2 100644 --- a/backend/src/admin/admin.service.ts +++ b/backend/src/admin/admin.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, UnauthorizedException } from '@nestjs/common'; import { CreateAdminDto } from './dto/create-admin.dto'; import { Admin } from './entities/admin.entity'; import { Repository } from 'typeorm'; @@ -12,6 +12,10 @@ export class AdminService { } async findOne(email: string): Promise { - return await this.adminRepository.findOneByOrFail({ email }); + try { + return await this.adminRepository.findOneByOrFail({ email }); + } catch (error) { + throw new UnauthorizedException('Admin not found'); + } } } -- GitLab From b120febf00d75c19756724d75c42308047d13f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:42:13 -0600 Subject: [PATCH 08/17] renombrando forma de autenticacion --- backend/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/main.ts b/backend/src/main.ts index a45b1c90..733878db 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -16,7 +16,7 @@ async function bootstrap() { name: 'Authorization', in: 'header', }, - 'JWT', + 'jwt', ) .build(); const document = SwaggerModule.createDocument(app, config); -- GitLab From 1447dfb1bcd79d058f2f2b9a23fb81f0d7b8c5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:43:05 -0600 Subject: [PATCH 09/17] agregando autorizacion para crear un pueblo --- backend/src/town/town.controller.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/src/town/town.controller.ts b/backend/src/town/town.controller.ts index a21509d3..a8767c9a 100644 --- a/backend/src/town/town.controller.ts +++ b/backend/src/town/town.controller.ts @@ -1,17 +1,18 @@ import { Controller, Get, Post, Param, Delete, UseInterceptors, UploadedFile, Body, Query } from '@nestjs/common'; import { TownService } from './town.service'; -import { ApiBody, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiBody, ApiConsumes, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; import { FileValidationPipe } from 'src/shared/pipe/file-validation.pipe'; import { fileInterceptor } from 'src/shared/interceptors/file-save.interceptor'; import { CreateTownDto } from './dto/create-town.dto'; +import { Roles } from 'src/auth/role.decorator'; +import { SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum'; @Controller() @ApiTags('Pueblos') export class TownController { - constructor( - private readonly townService: TownService, - // private readonly stateService: StateService, - ) {} + constructor(private readonly townService: TownService) {} + @Roles(SUPERADMIN_ROLES) + @ApiBearerAuth('jwt') @ApiBody({ type: CreateTownDto }) @ApiConsumes('multipart/form-data') @Post('town') -- GitLab From 5714331b97e81a6fde559e4b1548d5043217973a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:45:23 -0600 Subject: [PATCH 10/17] payload dto --- backend/src/shared/dto/payload-jwt.dto.ts | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 backend/src/shared/dto/payload-jwt.dto.ts diff --git a/backend/src/shared/dto/payload-jwt.dto.ts b/backend/src/shared/dto/payload-jwt.dto.ts new file mode 100644 index 00000000..5726376d --- /dev/null +++ b/backend/src/shared/dto/payload-jwt.dto.ts @@ -0,0 +1,4 @@ +export class PayloadJwtDto { + email: string; + role: string; +} -- GitLab From fa459a010828c92d27f3320910bc78803bb869d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:45:42 -0600 Subject: [PATCH 11/17] agregando array de roles de superadmin --- backend/src/shared/enum/admin-role.enum.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/shared/enum/admin-role.enum.ts b/backend/src/shared/enum/admin-role.enum.ts index b3aeeef1..e398a935 100644 --- a/backend/src/shared/enum/admin-role.enum.ts +++ b/backend/src/shared/enum/admin-role.enum.ts @@ -4,3 +4,4 @@ export enum ADMIN_ROLE { } export const ADMIN_ROLES = [ADMIN_ROLE.ADMIN, ADMIN_ROLE.SUPERADMIN]; +export const SUPERADMIN_ROLES = [ADMIN_ROLE.SUPERADMIN]; -- GitLab From 6d8db2c70a77a84bf6e8730139e749bff59245e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:47:46 -0600 Subject: [PATCH 12/17] insertando superadmin y admin cuando se inicia la app --- .../database-seeder.service.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/backend/src/database-seeder/database-seeder.service.ts b/backend/src/database-seeder/database-seeder.service.ts index d76e3660..9a3fd5c9 100644 --- a/backend/src/database-seeder/database-seeder.service.ts +++ b/backend/src/database-seeder/database-seeder.service.ts @@ -4,12 +4,17 @@ import { State } from 'src/state/entities/state.entity'; import { StateService } from 'src/state/state.service'; import { Repository } from 'typeorm'; import * as data from './states.json'; +import { CreateAdminDto } from 'src/admin/dto/create-admin.dto'; +import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; +import { UserStatus } from 'src/shared/enum/user-status.enum'; +import { AuthAdminService } from 'src/auth/admin/authAdminservice'; @Injectable() export class DatabaseSeederService implements OnModuleInit { constructor( @InjectRepository(State) private stateRepo: Repository, private readonly stateService: StateService, + private readonly authAdminService: AuthAdminService, ) {} async insertStates() { @@ -19,7 +24,57 @@ export class DatabaseSeederService implements OnModuleInit { } } + async insertSuperAdmin() { + const createSuperAdmin: CreateAdminDto = { + email: 'superadmin@gmail.com', + password: '123', + name: 'Super Admin', + lastName: 'super', + role: ADMIN_ROLE.SUPERADMIN, + status: UserStatus.ACTIVE, + }; + const createAdmin: CreateAdminDto = { + email: 'admin@gmail.com', + password: '123', + name: 'Admin', + lastName: 'admin', + role: ADMIN_ROLE.ADMIN, + status: UserStatus.ACTIVE, + }; + let tokenSuper = '', + tokenAdmin = ''; + try { + tokenSuper = await this.authAdminService.signUp({ ...createSuperAdmin }); + } catch (error) { + tokenSuper = ( + await this.authAdminService.signIn({ + email: createSuperAdmin.email, + password: createSuperAdmin.password, + }) + ).token; + } + try { + tokenAdmin = await this.authAdminService.signUp({ ...createAdmin }); + } catch (error) { + tokenAdmin = ( + await this.authAdminService.signIn({ + email: createAdmin.email, + password: createSuperAdmin.password, + }) + ).token; + } + console.log( + `Super Admin created with email: ${createSuperAdmin.email} + , password: ${createSuperAdmin.password}, and token: ${tokenSuper}`, + ); + console.log( + `Admin created with email: ${createAdmin.email}, + password: ${createAdmin.password}, and token: ${tokenAdmin}`, + ); + } + async onModuleInit() { await this.insertStates(); + await this.insertSuperAdmin(); } } -- GitLab From 7418e32eee31fee1cb6b21d14a864adc54df74ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:48:26 -0600 Subject: [PATCH 13/17] agregando auth guard a todas las rutas --- backend/src/app.module.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 5a809a0c..befa2d43 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -17,6 +17,9 @@ import { join } from 'path'; import { Town } from './town/entities/town.entity'; import { TownModule } from './town/town.module'; import { TownTraduction } from './town/entities/town-traduction.entity'; +import { AuthModule } from './shared/service/auth.module'; +import { APP_GUARD } from '@nestjs/core'; +import { AuthGuard } from './auth/admin/auth.guard'; @Module({ imports: [ @@ -38,12 +41,13 @@ import { TownTraduction } from './town/entities/town-traduction.entity'; StateModule, DatabaseSeederModule, TownModule, + AuthModule, ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'static'), }), ], controllers: [AppController], - providers: [AppService, DatabaseSeederModule], + providers: [AppService, DatabaseSeederModule, { provide: APP_GUARD, useClass: AuthGuard }], exports: [TypeOrmModule], }) export class AppModule {} -- GitLab From 18790c5098dad06156ae953baee61aff61f2d444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:51:16 -0600 Subject: [PATCH 14/17] agregando servicios --- backend/src/database-seeder/database-seeder.module.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/src/database-seeder/database-seeder.module.ts b/backend/src/database-seeder/database-seeder.module.ts index eb8e2f96..54d83e1b 100644 --- a/backend/src/database-seeder/database-seeder.module.ts +++ b/backend/src/database-seeder/database-seeder.module.ts @@ -4,9 +4,14 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { State } from 'src/state/entities/state.entity'; import { StateService } from 'src/state/state.service'; import { Town } from 'src/town/entities/town.entity'; +import { Admin } from 'src/admin/entities/admin.entity'; +import { AuthAdminService } from 'src/auth/admin/authAdminservice'; +import { AdminService } from 'src/admin/admin.service'; +import { JwtService } from '@nestjs/jwt'; +import { EncryptionService } from 'src/auth/encryption/encryption.service'; @Module({ - providers: [DatabaseSeederService, StateService], - imports: [TypeOrmModule.forFeature([State, Town])], + providers: [DatabaseSeederService, StateService, AuthAdminService, AdminService, JwtService, EncryptionService], + imports: [TypeOrmModule.forFeature([State, Town, Admin])], }) export class DatabaseSeederModule {} -- GitLab From 8d9179cfce7cd4e601b4f2807376e3e88f38f6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:51:46 -0600 Subject: [PATCH 15/17] corrigiendo error al obtener bearer token --- backend/src/auth/admin/auth.guard.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/src/auth/admin/auth.guard.ts b/backend/src/auth/admin/auth.guard.ts index 2bcfacf8..067114b2 100644 --- a/backend/src/auth/admin/auth.guard.ts +++ b/backend/src/auth/admin/auth.guard.ts @@ -11,12 +11,11 @@ export class AuthGuard implements CanActivate { ) {} async canActivate(context: ExecutionContext): Promise { const requiredRole = this.reflector.get(Roles, context.getHandler()); - console.log(requiredRole); - if (!requiredRole) return true; const request = context.switchToHttp().getRequest(); - const { authorization }: any = request.headers; + let { authorization }: any = request.headers; if (!authorization) throw new UnauthorizedException('session expired! Please sign In'); + authorization = authorization.split(' ')[1]; const role = await this.authService.validateToken(authorization); if (!requiredRole.includes(role)) throw new UnauthorizedException('Unauthorized access'); return true; -- GitLab From 4cdfd05fe9e5b81abd43fc5911b6dd0280251e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:52:19 -0600 Subject: [PATCH 16/17] agregando email y role al payload --- backend/src/auth/admin/authAdminservice.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/src/auth/admin/authAdminservice.ts b/backend/src/auth/admin/authAdminservice.ts index 3d780937..f94aa66b 100644 --- a/backend/src/auth/admin/authAdminservice.ts +++ b/backend/src/auth/admin/authAdminservice.ts @@ -7,7 +7,7 @@ import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto'; import { JwtConstants } from 'src/constants/jwt.constants'; import { AdminSigninResDto } from './dto/admin-signin-res.dto'; import { Admin } from 'src/admin/entities/admin.entity'; -import { ADMIN_ROLE } from 'src/enum/admin-role.enum'; +import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum'; @Injectable() export class AuthAdminService { @@ -36,7 +36,10 @@ export class AuthAdminService { if (!validPwd) { throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED); } - const accessToken = await this.jwtService.sign({ email: admin.email }, { secret: JwtConstants.SECRET }); + const accessToken = await this.jwtService.sign( + { email: admin.email, role: admin.role }, + { secret: JwtConstants.SECRET }, + ); const adminSigninResDto: AdminSigninResDto = { email: admin.email, name: admin.name, -- GitLab From cf50a47c0ae654588b2940c52dfe665ca89986d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 7 May 2024 11:52:59 -0600 Subject: [PATCH 17/17] =?UTF-8?q?agregando=20autorizaci=C3=B3n=20para=20cr?= =?UTF-8?q?ear=20un=20admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/auth/admin/authAdmincontroller.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/src/auth/admin/authAdmincontroller.ts b/backend/src/auth/admin/authAdmincontroller.ts index bc2e0906..ff6b3d09 100644 --- a/backend/src/auth/admin/authAdmincontroller.ts +++ b/backend/src/auth/admin/authAdmincontroller.ts @@ -2,8 +2,10 @@ import { Body, Controller, Post } from '@nestjs/common'; import { AuthAdminService } from './authAdminservice'; import { CreateAdminDto } from 'src/admin/dto/create-admin.dto'; import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto'; -import { ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiBody, ApiCreatedResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger'; import { AdminSigninResDto } from './dto/admin-signin-res.dto'; +import { Roles } from '../role.decorator'; +import { ADMIN_ROLE, SUPERADMIN_ROLES } from 'src/shared/enum/admin-role.enum'; @Controller() @ApiTags('Create admin account and sign in as admin') @@ -20,10 +22,12 @@ export class AuthAdminController { }, }, }) + @Roles(SUPERADMIN_ROLES) @Post('admin/signup') + @ApiBearerAuth('jwt') async signUp(@Body() createAdminDto: CreateAdminDto) { - console.log(createAdminDto); try { + createAdminDto.role = ADMIN_ROLE.ADMIN; const accessToken = await this.authAdminService.signUp(createAdminDto); return { token: accessToken }; } catch (e) { -- GitLab