Skip to content

Commit

Permalink
Create auth.service.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 9, 2024
1 parent 9476e9b commit 0579081
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions projects/pi-nexus-iam/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User } from '../models/user.model';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import * as bcrypt from 'bcrypt';
import { Role } from '../enums/role.enum';
import { AccessToken } from '../interfaces/access-token.interface';
import { RefreshToken } from '../interfaces/refresh-token.interface';

@Injectable()
export class AuthService {
constructor(
@InjectModel('User') private readonly userModel: Model<User>,
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
) {}

async register(user: User): Promise<User> {
const hashedPassword = await bcrypt.hash(user.password, 10);
user.password = hashedPassword;
return this.userModel.create(user);
}

async login(email: string, password: string): Promise<AccessToken> {
const user = await this.userModel.findOne({ email });
if (!user) {
throw new Error('Invalid email or password');
}
const isValid = await bcrypt.compare(password, user.password);
if (!isValid) {
throw new Error('Invalid email or password');
}
const accessToken = this.generateAccessToken(user);
const refreshToken = this.generateRefreshToken(user);
return { accessToken, refreshToken };
}

async refreshToken(refreshToken: string): Promise<AccessToken> {
const user = await this.userModel.findOne({ refreshToken });
if (!user) {
throw new Error('Invalid refresh token');
}
const accessToken = this.generateAccessToken(user);
return { accessToken };
}

async logout(refreshToken: string): Promise<void> {
await this.userModel.updateOne({ refreshToken }, { $set: { refreshToken: null } });
}

private generateAccessToken(user: User): string {
const payload = { sub: user.id, email: user.email, roles: user.roles };
return this.jwtService.sign(payload, {
secret: this.configService.get('JWT_SECRET'),
expiresIn: '1h',
});
}

private generateRefreshToken(user: User): string {
const payload = { sub: user.id, email: user.email };
return this.jwtService.sign(payload, {
secret: this.configService.get('JWT_SECRET'),
expiresIn: '7d',
});
}
}

0 comments on commit 0579081

Please sign in to comment.