feat: add jwt
This commit is contained in:
@@ -4,9 +4,26 @@ import { AuthService } from './providers/auth.service';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
import { OtpModule } from '../otp/otp.module';
|
||||
import { HashingModule } from '@/common/modules/hashing/hashing.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule, OtpModule, HashingModule],
|
||||
imports: [
|
||||
UsersModule,
|
||||
OtpModule,
|
||||
HashingModule,
|
||||
JwtModule.registerAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
secret: configService.get<string>('jwt.access.secret'),
|
||||
signOptions: {
|
||||
expiresIn: configService.get<number>('jwt.access.expiresIn'),
|
||||
issuer: configService.get<string>('jwt.issuer'),
|
||||
audience: configService.get<string>('jwt.audience'),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService],
|
||||
})
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { UsersService } from '@/modules/users/providers/users.service';
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { LoginDTO } from '../dtos/login.dto';
|
||||
import { OtpService } from '@/modules/otp/providers/otp.service';
|
||||
import { HashingProvider } from '@/common/modules/hashing/providers/hashing.provider';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import type { ConfigType } from '@nestjs/config';
|
||||
import jwtConfig from '@/config/jwt.config';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@@ -21,6 +24,17 @@ export class AuthService {
|
||||
* Inject Hashing Provider
|
||||
*/
|
||||
private readonly hashingProvider: HashingProvider,
|
||||
|
||||
/**
|
||||
* Inject Jwt Service
|
||||
*/
|
||||
private readonly jwtService: JwtService,
|
||||
|
||||
/**
|
||||
* Inject Jwt Config
|
||||
*/
|
||||
@Inject(jwtConfig.KEY)
|
||||
private readonly jwtConfiguration: ConfigType<typeof jwtConfig>,
|
||||
) {}
|
||||
|
||||
public async logIn(loginDto: LoginDTO) {
|
||||
@@ -52,9 +66,19 @@ export class AuthService {
|
||||
);
|
||||
|
||||
if (isPasswordCorrect) {
|
||||
// TODO: Create JWT
|
||||
const tokenPayload = {
|
||||
sub: user.id,
|
||||
phone: user.phone,
|
||||
};
|
||||
|
||||
return { access: 'ACCESS_TOKEN', refresh: 'REFRESH_TOKEN' };
|
||||
const accessToken = await this.jwtService.signAsync(tokenPayload);
|
||||
|
||||
const refreshToken = await this.jwtService.signAsync(tokenPayload, {
|
||||
secret: this.jwtConfiguration.refresh.secret,
|
||||
expiresIn: this.jwtConfiguration.refresh.expiresIn,
|
||||
});
|
||||
|
||||
return { access: accessToken, refresh: refreshToken };
|
||||
}
|
||||
|
||||
throw new UnauthorizedException(
|
||||
|
||||
@@ -39,7 +39,6 @@ export class User {
|
||||
type: 'varchar',
|
||||
length: 96,
|
||||
nullable: true,
|
||||
select: false,
|
||||
})
|
||||
password?: string;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user