Commit 520270b9 authored by Diego Iván's avatar Diego Iván
Browse files

cambiando de carpeta auth source

parent 07d12bc0
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -3,10 +3,12 @@ import { AdminService } from './admin.service';
import { AdminController } from './admin.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Admin } from './entities/admin.entity';
import { Town } from 'src/town/entities/town.entity';

@Module({
  controllers: [AdminController],
  providers: [AdminService],
  imports: [TypeOrmModule.forFeature([Admin])],
  imports: [TypeOrmModule.forFeature([Admin, Town])],
  exports: [AdminService],
})
export class AdminModule {}
+7 −2
Original line number Diff line number Diff line
@@ -3,12 +3,17 @@ import { CreateAdminDto } from './dto/create-admin.dto';
import { Admin } from './entities/admin.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Town } from 'src/town/entities/town.entity';

@Injectable()
export class AdminService {
  constructor(@InjectRepository(Admin) private adminRepository: Repository<Admin>) {}
  constructor(
    @InjectRepository(Admin) private adminRepository: Repository<Admin>,
    @InjectRepository(Town) private townRepository: Repository<Town>,
  ) {}
  async create(createAdminDto: CreateAdminDto) {
    await this.adminRepository.insert(createAdminDto);
    const town = await this.townRepository.findOneByOrFail({ townId: createAdminDto.idTown });
    await this.adminRepository.insert({ ...createAdminDto, idTown: town });
  }

  async findOne(email: string): Promise<Admin> {
+2 −4
Original line number Diff line number Diff line
@@ -17,9 +17,8 @@ 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';
import { AuthAdminGuard } from './auth/admin/authAdmin.guard';
import { PlaceModule } from './place/place.module';
import { Place } from './place/entities/place.entity';
import { PointOfInterestModule } from './pointOfInterest/PointOfInterest.module';
@@ -59,7 +58,6 @@ import { PlaceTraduction } from './place/entities/place-traduction.entity';
    StateModule,
    DatabaseSeederModule,
    TownModule,
    AuthModule,
    PlaceModule,
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'static'),
@@ -67,7 +65,7 @@ import { PlaceTraduction } from './place/entities/place-traduction.entity';
    PointOfInterestModule,
  ],
  controllers: [AppController],
  providers: [AppService, DatabaseSeederModule, { provide: APP_GUARD, useClass: AuthGuard }],
  providers: [AppService, DatabaseSeederModule, { provide: APP_GUARD, useClass: AuthAdminGuard }],
  exports: [TypeOrmModule],
})
export class AppModule {}
+10 −5
Original line number Diff line number Diff line
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';
import { AuthAdminService } from './authAdmin.service';
import { AdminService } from 'src/admin/admin.service';

@Injectable()
export class AuthGuard implements CanActivate {
export class AuthAdminGuard implements CanActivate {
  constructor(
    private authService: AuthService,
    private authAdminService: AuthAdminService,
    private adminService: AdminService,
    private reflector: Reflector,
  ) {}
  async canActivate(context: ExecutionContext): Promise<boolean> {
@@ -16,8 +18,11 @@ export class AuthGuard implements CanActivate {
    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');
    const jwtPayload = await this.authAdminService.validateToken(authorization);
    if (!requiredRole.includes(jwtPayload.role)) throw new UnauthorizedException('Unauthorized access');
    const admin = await this.adminService.findOne(jwtPayload.email);
    console.log({ admin });
    request.admin = { ...admin };
    return true;
  }
}
+6 −3
Original line number Diff line number Diff line
@@ -3,13 +3,16 @@ import { AuthAdminController } from './authAdmincontroller';
import { EncryptionService } from '../encryption/encryption.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Admin } from 'src/admin/entities/admin.entity';
import { AuthAdminService } from './authAdminservice';
import { AuthAdminService } from './authAdmin.service';
import { AdminService } from 'src/admin/admin.service';
import { JwtService } from '@nestjs/jwt';
import { AuthAdminGuard } from './authAdmin.guard';
import { Town } from 'src/town/entities/town.entity';

@Module({
  controllers: [AuthAdminController],
  providers: [AuthAdminService, AdminService, JwtService, EncryptionService],
  imports: [TypeOrmModule.forFeature([Admin])],
  providers: [AuthAdminService, AdminService, JwtService, EncryptionService, AuthAdminGuard],
  imports: [TypeOrmModule.forFeature([Admin, Town])],
  exports: [AuthAdminService],
})
export class AuthAdminModule {}
Loading