Skip to content

Commit

Permalink
create: auth module and functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
adiCacti committed May 16, 2022
1 parent 2f06aa0 commit 41b630c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
12 changes: 7 additions & 5 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Controller, Post } from '@nestjs/common';
import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthDto } from './dto';

@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}

@Post('signup')
signup() {
return this.authService.signup();
signup(@Body() dto: AuthDto) {
return this.authService.signup(dto);
}

@HttpCode(HttpStatus.OK)
@Post('signin')
signin() {
return this.authService.signin();
signin(@Body() dto: AuthDto) {
return this.authService.signin(dto);
}
}
5 changes: 4 additions & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { JwtStrategy } from './strategy';

@Module({
imports: [JwtModule.register({})],
controllers: [AuthController],
providers: [AuthService],
providers: [AuthService, JwtStrategy],
})
export class AuthModule {}
78 changes: 72 additions & 6 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,78 @@
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ForbiddenException, Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { AuthDto } from './dto';
import * as argon from 'argon2';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
import { ConfigService } from '@nestjs/config';

@Injectable({})
@Injectable()
export class AuthService {
signup() {
return { msg: 'I am signed up' };
constructor(
private prisma: PrismaService,
private jwt: JwtService,
private config: ConfigService,
) {}

async signup(dto: AuthDto) {
// generate the password hash
const hash = await argon.hash(dto.password);
try {
// save the new user in the db
const user = await this.prisma.user.create({
data: {
email: dto.email,
hash,
},
});
// return jwt token
return this.signToken(user.id, user.email);
} catch (error) {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === 'P2002') {
throw new ForbiddenException('Credentials Taken');
}
}

throw error;
}
}

async signin(dto: AuthDto) {
// find the user by email
const user = await this.prisma.user.findUnique({
where: {
email: dto.email,
},
});
// if user does not exits throw exception
if (!user) throw new ForbiddenException('Credentials incorrect');
// compare password
const pwMatches = await argon.verify(user.hash, dto.password);
// if password incorrect throw exception
if (!pwMatches) throw new ForbiddenException('Credentials incorrect');
// return jwt token
return this.signToken(user.id, user.email);
}

signin() {
return { msg: 'I am signed in' };
async signToken(
userId: number,
email: string,
): Promise<{ access_token: string }> {
const payload = {
sub: userId,
email,
};

const secret = this.config.get('JWT_SECRET');

const token = await this.jwt.signAsync(payload, {
expiresIn: '15m',
secret: secret,
});

return {
access_token: token,
};
}
}

0 comments on commit 41b630c

Please sign in to comment.