Commit 7a40b1f8 authored by Omar Luna's avatar Omar Luna
Browse files

Merge branch pueblosmagicosconia:main into main

parents 2b67846c dfb7c587
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
        "@nestjs/jwt": "^10.2.0",
        "@nestjs/mapped-types": "*",
        "@nestjs/platform-express": "^10.0.0",
        "@nestjs/serve-static": "^4.0.2",
        "@nestjs/swagger": "^7.3.0",
        "@nestjs/typeorm": "^10.0.2",
        "bcrypt": "^5.1.1",
@@ -2040,6 +2041,37 @@
      "integrity": "sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==",
      "dev": true
    },
    "node_modules/@nestjs/serve-static": {
      "version": "4.0.2",
      "resolved": "https://registry.npmjs.org/@nestjs/serve-static/-/serve-static-4.0.2.tgz",
      "integrity": "sha512-cT0vdWN5ar7jDI2NKbhf4LcwJzU4vS5sVpMkVrHuyLcltbrz6JdGi1TfIMMatP2pNiq5Ie/uUdPSFDVaZX/URQ==",
      "dependencies": {
        "path-to-regexp": "0.2.5"
      },
      "peerDependencies": {
        "@fastify/static": "^6.5.0 || ^7.0.0",
        "@nestjs/common": "^9.0.0 || ^10.0.0",
        "@nestjs/core": "^9.0.0 || ^10.0.0",
        "express": "^4.18.1",
        "fastify": "^4.7.0"
      },
      "peerDependenciesMeta": {
        "@fastify/static": {
          "optional": true
        },
        "express": {
          "optional": true
        },
        "fastify": {
          "optional": true
        }
      }
    },
    "node_modules/@nestjs/serve-static/node_modules/path-to-regexp": {
      "version": "0.2.5",
      "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.2.5.tgz",
      "integrity": "sha512-l6qtdDPIkmAmzEO6egquYDfqQGPMRNGjYtrU13HAXb3YSRrt7HSb1sJY0pKp6o2bAa86tSB6iwaW2JbthPKr7Q=="
    },
    "node_modules/@nestjs/swagger": {
      "version": "7.3.0",
      "resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-7.3.0.tgz",
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/serve-static": "^4.0.2",
    "@nestjs/swagger": "^7.3.0",
    "@nestjs/typeorm": "^10.0.2",
    "bcrypt": "^5.1.1",
+12 −2
Original line number Diff line number Diff line
@@ -9,6 +9,11 @@ import { AuthAdminModule } from './auth/admin/authAdmin.module';
import { UserModule } from './user/user.module';
import { User } from './user/entities/user.entity';
import { AuthUserModule } from './auth/user/authUser.module';
import { StateModule } from './state/state.module';
import { DatabaseSeederModule } from './database-seeder/database-seeder.module';
import { State } from './state/entities/state.entity';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
@Module({
  imports: [
    TypeOrmModule.forRoot({
@@ -18,15 +23,20 @@ import { AuthUserModule } from './auth/user/authUser.module';
      username: DbConstants.DB_USER,
      password: DbConstants.DB_PASSWORD,
      database: DbConstants.DB_NAME,
      entities: [Admin, User],
      entities: [Admin, User, State],
      synchronize: DbConstants.DB_SYNC,
    }),
    AuthAdminModule,
    AdminModule,
    UserModule,
    AuthUserModule,
    StateModule,
    DatabaseSeederModule,
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'static'),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
  providers: [AppService, DatabaseSeederModule],
})
export class AppModule {}
+10 −12
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import {
import { AuthUserService } from './authUserservice';
import { CreateUserDto } from 'src/user/dto/create-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
import { UserSigninResDto } from './dto/user-signin-res.dto';

@Controller('')
@ApiTags('Create user account and sign in as user')
@@ -18,31 +19,28 @@ export class AuthUserController {
  @ApiBody({ type: CreateUserDto })
  @ApiBearerAuth()
  @ApiCreatedResponse({
    content: {
      'application/json': {
        example: {
          token: 'token',
        },
      },
    },
    type: UserSigninResDto,
  })
  @Post('user/signup')
  async signUp(@Body() createAdminDto: CreateUserDto) {
    try {
      const accessToken = await this.authUserService.signUp(createAdminDto);
      return { token: accessToken };
      const adminSigninResDto =
        await this.authUserService.signUp(createAdminDto);
      return { user: adminSigninResDto };
    } catch (e) {
      throw e;
    }
  }
  @ApiBody({ type: LoginUserDto })
  @ApiCreatedResponse({
    type: UserSigninResDto,
  })
  @ApiUnauthorizedResponse()
  @Post('user/signin')
  async signIn(@Body() loginAdminDto: LoginUserDto) {
    try {
      const adminSigninResDto =
        await this.authUserService.signIn(loginAdminDto);
      return adminSigninResDto;
      const userSigninResDto = await this.authUserService.signIn(loginAdminDto);
      return { user: userSigninResDto };
    } catch (e) {
      throw e;
    }
+13 −4
Original line number Diff line number Diff line
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
  BadRequestException,
  Injectable,
  UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { EncryptionService } from '../encryption/encryption.service';
import { JwtConstants } from 'src/constants/jwt.constants';
@@ -16,7 +20,7 @@ export class AuthUserService {
    private encryptionService: EncryptionService,
  ) {}

  async signUp(createAdminDto: CreateUserDto): Promise<string> {
  async signUp(createAdminDto: CreateUserDto): Promise<UserSigninResDto> {
    const loginAdminDto: LoginUserDto = {
      email: createAdminDto.email,
      password: createAdminDto.password,
@@ -25,27 +29,32 @@ export class AuthUserService {
      createAdminDto.password,
    );
    createAdminDto.password = hashedPwd;
    if (await this.userService.userExists(createAdminDto.email)) {
      throw new BadRequestException('User already exists');
    }
    await this.userService.create(createAdminDto);

    const adminSigninResDto: UserSigninResDto =
      await this.signIn(loginAdminDto);
    return adminSigninResDto.token;
    return adminSigninResDto;
  }

  async signIn(logInAdmin: LoginUserDto): Promise<UserSigninResDto> {
    const user: User = await this.userService.findOne(logInAdmin.email);
    if (!user) throw new UnauthorizedException('Invalid credentials');
    const validPwd: boolean = await this.encryptionService.comparePassword(
      logInAdmin.password,
      user.password,
    );
    if (!validPwd) {
      throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED);
      throw new UnauthorizedException('Invalid credentials');
    }
    const accessToken = await this.jwtService.sign(
      { email: user.email, name: user.name, lastName: user.lastName },
      { secret: JwtConstants.SECRET },
    );
    const userSigninResDto: UserSigninResDto = {
      userId: user.userId,
      email: user.email,
      name: user.name,
      lastName: user.lastName,
Loading