feat: add jwt

This commit is contained in:
2026-02-22 17:16:57 +03:30
parent 20c89c472c
commit 72a001a150
8 changed files with 183 additions and 9 deletions
+27 -3
View File
@@ -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(