Skip to content

Commit

Permalink
feat: adds loging and custom error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyNico43 committed Apr 8, 2024
1 parent 95185a6 commit 54781db
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 173 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all"
"trailingComma": "all",
"semi": true
}
7 changes: 7 additions & 0 deletions enums/error-codes.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum ErrorCodes {
API_ERROR = 'API_ERROR',
NOT_ALLOWED = 'NOT_ALLOWED',
NOT_FOUND = 'NOT_FOUND',
ALREADY_EXISTS = 'ALREADY_EXISTS',
WRONG_CREDENTIALS = 'WRONG_CREDENTIALS',
}
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"eslint": "8.42.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-unused-imports": "^3.1.0",
"reflect-metadata": "0.2.0",
"rxjs": "7.8.1",
"source-map-support": "0.5.21",
Expand Down
2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { UsersModule } from './users/users.module';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './user/users.module';

@Module({
imports: [UsersModule, AuthModule],
Expand Down
6 changes: 3 additions & 3 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common';
import { Logger, Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { JWT_SECRET } from '../constants/constants';
import { UsersModule } from '../users/users.module';
import { UsersModule } from '../user/users.module';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

Expand All @@ -15,7 +15,7 @@ import { AuthService } from './auth.service';
}),
],
controllers: [AuthController],
providers: [AuthService],
providers: [AuthService, Logger],
exports: [AuthService],
})
export class AuthModule {}
27 changes: 24 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcrypt';
import { UsersService } from '../users/users.service';
import { ErrorCodes } from 'enums/error-codes.enum';
import { UsersService } from '../user/users.service';

@Injectable()
export class AuthService {
Expand All @@ -10,10 +15,26 @@ export class AuthService {
private jwtservice: JwtService,
) {}

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

async login(email: string, pass: string): Promise<{ accessToken: string }> {
this.logger.log(`Attempting login for user with email: ${email}`);
const user = await this.usersService.findUserByEmail(email);
if (!user) {
this.logger.error(
`Login for user with email: ${email} failed, not found in database`,
);
throw new InternalServerErrorException({
error: ErrorCodes.WRONG_CREDENTIALS,
});
}
if (!(await bcrypt.compare(pass, user.password))) {
throw new UnauthorizedException();
this.logger.error(
`Login for user with email: ${email} failed, invalid password`,
);
throw new InternalServerErrorException({
error: ErrorCodes.WRONG_CREDENTIALS,
});
}
const payload = { sub: user.id, email: user.email };
const accessToken = await this.jwtservice.signAsync(payload);
Expand Down
File renamed without changes.
File renamed without changes.
81 changes: 81 additions & 0 deletions src/user/users.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}

@Post('signup')
create(@Body() createUserDto: CreateUserDto): Promise<
Prisma.UserGetPayload<{
select: {
id: true;
createdAt: true;
updatedAt: true;
email: true;
password: false;
};
}>
> {
return this.usersService.createUser(createUserDto);
}

@Get(':id')
findOne(@Param('id') id: string): Promise<
Prisma.UserGetPayload<{
select: {
id: true;
createdAt: true;
updatedAt: true;
email: true;
password: false;
};
}>
> {
return this.usersService.findUserById(id);
}

@Put(':id')
update(
@Param('id') id: string,
@Body() updateUserDto: UpdateUserDto,
): Promise<
Prisma.UserGetPayload<{
select: {
id: true;
createdAt: true;
updatedAt: true;
email: true;
password: false;
};
}>
> {
return this.usersService.updateUser(id, updateUserDto);
}

@Delete(':id')
remove(@Param('id') id: string): Promise<
Prisma.UserGetPayload<{
select: {
id: true;
createdAt: true;
updatedAt: true;
email: true;
password: false;
};
}>
> {
return this.usersService.deleteUser(id);
}
}
File renamed without changes.
Loading

0 comments on commit 54781db

Please sign in to comment.