feat: add jwt
This commit is contained in:
+2
-1
@@ -10,6 +10,7 @@ import { AuthModule } from './modules/auth/auth.module';
|
||||
import { OtpModule } from './modules/otp/otp.module';
|
||||
import { HashingModule } from './common/modules/hashing/hashing.module';
|
||||
import path from 'path';
|
||||
import jwtConfig from './config/jwt.config';
|
||||
|
||||
const ENV = process.env.NODE_ENV;
|
||||
|
||||
@@ -34,7 +35,7 @@ const ENV = process.env.NODE_ENV;
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
envFilePath: !ENV ? '.env' : `.env.${ENV}`,
|
||||
load: [appConfig, databaseConfig],
|
||||
load: [appConfig, databaseConfig, jwtConfig],
|
||||
validationSchema: environmentValidation,
|
||||
}),
|
||||
|
||||
|
||||
@@ -11,4 +11,10 @@ export default Joi.object({
|
||||
DATABASE_NAME: Joi.string().required(),
|
||||
DATABASE_AUTOLOAD: Joi.boolean().default('true'),
|
||||
DATABASE_SYNC: Joi.boolean().default('true'),
|
||||
JWT_ACCESS_SECRET: Joi.string().required(),
|
||||
JWT_ACCESS_TTL: Joi.number().required(),
|
||||
JWT_REFRESH_SECRET: Joi.string().required(),
|
||||
JWT_REFRESH_TTL: Joi.number().required(),
|
||||
JWT_ISSUER: Joi.string().required(),
|
||||
JWT_AUDIENCE: Joi.string().required(),
|
||||
});
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export default registerAs('jwt', () => ({
|
||||
access: {
|
||||
secret: process.env.JWT_ACCESS_SECRET,
|
||||
expiresIn: parseInt(process.env.JWT_ACCESS_TTL || '3600'),
|
||||
},
|
||||
refresh: {
|
||||
secret: process.env.JWT_REFRESH_SECRET,
|
||||
expiresIn: parseInt(process.env.JWT_REFRESH_TTL || '864000'),
|
||||
},
|
||||
issuer: process.env.JWT_ISSUER,
|
||||
audience: process.env.JWT_AUDIENCE,
|
||||
}));
|
||||
@@ -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