Commit 0f2ce54c authored by Diego Iván's avatar Diego Iván
Browse files

agregando resource de auth

parent f688d14c
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
import { Module } from '@nestjs/common';
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 { AdminService } from 'src/admin/admin.service';
import { JwtService } from '@nestjs/jwt';

@Module({
  controllers: [AuthAdminController],
  providers: [AuthAdminService, AdminService, JwtService, EncryptionService],
  imports: [TypeOrmModule.forFeature([Admin])],
})
export class AuthAdminModule {}
+31 −0
Original line number Diff line number Diff line
import { Body, Controller, Post } from '@nestjs/common';
import { EncryptionService } from '../encryption/encryption.service';
import { AuthAdminService } from './authAdminservice';
import { CreateAdminDto } from 'src/admin/dto/create-admin.dto';
import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto';

@Controller('auth')
export class AuthAdminController {
  constructor(private readonly authAdminService: AuthAdminService) {}

  @Post('admin/signup')
  async signUp(@Body() createAdminDto: CreateAdminDto) {
    console.log(createAdminDto);
    try {
      const accessToken = await this.authAdminService.signUp(createAdminDto);
      return { token: accessToken };
    } catch (e) {
      console.log(e);
    }
  }

  @Post('admin/signin')
  async signIn(@Body() loginAdminDto: LoginAdminDto) {
    try {
      const accessToken = await this.authAdminService.signIn(loginAdminDto);
      return { token: accessToken };
    } catch (e) {
      console.log(e);
    }
  }
}
+45 −0
Original line number Diff line number Diff line
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AdminService } from 'src/admin/admin.service';
import { JwtService } from '@nestjs/jwt';
import { EncryptionService } from '../encryption/encryption.service';
import { CreateAdminDto } from 'src/admin/dto/create-admin.dto';
import { LoginAdminDto } from 'src/auth/admin/dto/login-admin.dto';
import { JwtConstants } from 'src/constants/jwt.constants';

@Injectable()
export class AuthAdminService {
  constructor(
    private adminService: AdminService,
    private jwtService: JwtService,
    private encryptionService: EncryptionService,
  ) {}

  async signUp(createAdminDto: CreateAdminDto): Promise<string> {
    const loginAdminDto: LoginAdminDto = {
      email: createAdminDto.email,
      password: createAdminDto.password,
    };
    const hashedPwd = await this.encryptionService.hashPassword(
      createAdminDto.password,
    );
    createAdminDto.password = hashedPwd;
    await this.adminService.create(createAdminDto);

    const accessToken = await this.signIn(loginAdminDto);
    return accessToken;
  }

  async signIn(logInAdmin: LoginAdminDto): Promise<string> {
    const admin = await this.adminService.findOne(logInAdmin.email);
    const validPwd: boolean = await this.encryptionService.comparePassword(
      logInAdmin.password,
      admin.password,
    );
    if (!validPwd) throw new UnauthorizedException('Invalid credentials');
    const accessToken = await this.jwtService.sign(
      { email: admin.email },
      { secret: JwtConstants.SECRET },
    );
    return accessToken;
  }
}
+4 −0
Original line number Diff line number Diff line
export class LoginAdminDto {
  email;
  password;
}