Skip to content

Commit

Permalink
feat: namingchanges and some fixes for the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyNico43 committed Apr 19, 2024
1 parent 98cbca9 commit 451a4a4
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
import { AuthModule } from './common/auth/auth.module';
import { UsersModule } from './user/users.module';
import { UserModule } from './user/user.module';

@Module({
imports: [
Expand All @@ -21,7 +21,7 @@ import { UsersModule } from './user/users.module';
},
cache: true,
}),
UsersModule,
UserModule,
AuthModule,
],
})
Expand Down
8 changes: 4 additions & 4 deletions src/common/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Logger, Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { JwtModule } from '@nestjs/jwt';
import { UsersModule } from 'src/user/users.module';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { AuthGuard } from './auth.guard';
import { APP_GUARD } from '@nestjs/core';
import { AuthService } from './auth.service';
import { UserModule } from 'src/user/user.module';

@Module({
imports: [
UsersModule,
UserModule,
JwtModule.register({
global: true,
secret: process.env.JWT_SECRET,
Expand Down
17 changes: 13 additions & 4 deletions src/common/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { User } from '@prisma/client';
import * as bcrypt from 'bcrypt';
import { ErrorCodes } from 'enums/error-codes.enum';
import { UsersService } from 'src/user/users.service';
import { UserService } from 'src/user/user.service';

@Injectable()
export class AuthService {
constructor(
private usersService: UsersService,
private userService: UserService,
private jwtService: JwtService,
) {}

private readonly logger = new Logger(AuthService.name);

async login(email: string, pass: string): Promise<string> {
this.logger.log(`Attempting login for user with email: ${email}`);
const user = await this.usersService.findUserByEmail(email);
if (!user) {
let user: User | undefined;
try {
user = await this.userService.findUserByEmail(email);
} catch (err) {
this.logger.error(`Login for user with email: ${email} failed, not found in database`);
throw new InternalServerErrorException({
error: ErrorCodes.WRONG_CREDENTIALS,
});
}
if (!user) {
this.logger.error(`Login for user with email: ${email} failed, no user returned from database`);
throw new InternalServerErrorException({
error: ErrorCodes.API_ERROR,
});
}
if (!(await bcrypt.compare(pass, user.password))) {
this.logger.error(`Login for user with email: ${email} failed, invalid password`);
throw new InternalServerErrorException({
Expand Down
16 changes: 8 additions & 8 deletions src/user/users.controller.ts → src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { CurrentUserId } from 'src/common/auth/current-user-id.decrator';
import { Public } from 'src/common/auth/public.decorator';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UsersService } from './users.service';
import { UserService } from './user.service';

//TODO: change to user
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
@Controller('user')
export class UserController {
constructor(private userService: UserService) {}

@Public()
@Post('signup')
create(@Body() createUserDto: CreateUserDto): Promise<void> {
return this.usersService.createUser(createUserDto);
return this.userService.createUser(createUserDto);
}

@Get()
Expand All @@ -29,7 +29,7 @@ export class UsersController {
};
}>
> {
return this.usersService.findUserById(id);
return this.userService.findUserById(id);
}

@Put(':id')
Expand All @@ -47,7 +47,7 @@ export class UsersController {
};
}>
> {
return this.usersService.updateUser(id, updateUserDto);
return this.userService.updateUser(id, updateUserDto);
}

@Delete(':id')
Expand All @@ -62,6 +62,6 @@ export class UsersController {
};
}>
> {
return this.usersService.deleteUser(id);
return this.userService.deleteUser(id);
}
}
11 changes: 11 additions & 0 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { UserController } from './user.controller';
import { UserService } from './user.service';

@Module({
controllers: [UserController],
providers: [UserService, PrismaClient],
exports: [UserService],
})
export class UserModule {}
30 changes: 15 additions & 15 deletions src/user/users.service.ts → src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

@Injectable()
export class UsersService {
export class UserService {
constructor(
private prisma: PrismaClient,
private configService: ConfigService,
) {}

private readonly logger = new Logger(UsersService.name);
private readonly logger = new Logger(UserService.name);

createUser(createUserDto: CreateUserDto): Promise<void> {
async createUser(createUserDto: CreateUserDto): Promise<void> {
this.logger.log(`Attempting to create user with email: ${createUserDto.email}`);
const userInDatabase = await this.prisma.user.findUnique({
where: {
email: createUserDto.email,
},
});
if (userInDatabase) {
this.logger.error(`User with email: ${createUserDto.email} already exists`);
throw new InternalServerErrorException({
error: ErrorCodes.ALREADY_EXISTS,
});
}
return this.prisma.$transaction(async (tx: PrismaClient) => {
try {
this.logger.log(`Attempting to create user with email: ${createUserDto.email}`);
const userInDatabase = await tx.user.findUnique({
where: {
email: createUserDto.email,
},
});
if (userInDatabase) {
this.logger.error(`User with email: ${createUserDto.email} already exists`);
throw new InternalServerErrorException({
error: ErrorCodes.ALREADY_EXISTS,
});
}
const salt = await bcrypt.genSalt(this.configService.get('SALT_ROUNDS'));
const hash = await bcrypt.hash(createUserDto.password, salt);
await tx.user.create({
Expand Down
11 changes: 0 additions & 11 deletions src/user/users.module.ts

This file was deleted.

0 comments on commit 451a4a4

Please sign in to comment.