update
This commit is contained in:
@@ -8,6 +8,9 @@ import { JwtService } from '@nestjs/jwt';
|
||||
import { AdminService } from '../../admin/admin.service';
|
||||
import { AuthEntityType } from 'src/modules/auth/guards/auth.guard';
|
||||
import { TokensService } from './tokens.service';
|
||||
import { RestaurantsService } from 'src/modules/restaurants/restaurants.service';
|
||||
import { RestRepository } from 'src/modules/restaurants/repositories/user.repository';
|
||||
import { RestMessage } from 'src/common/enums/message.enum';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@@ -18,13 +21,14 @@ export class AuthService {
|
||||
private readonly jwtService: JwtService,
|
||||
private readonly adminService: AdminService,
|
||||
private readonly tokensService: TokensService,
|
||||
private readonly restRepository: RestRepository,
|
||||
) {}
|
||||
|
||||
async requestOtp(dto: RequestOtpDto) {
|
||||
const { phone, slug } = dto;
|
||||
const code = this.generateOtpCode();
|
||||
|
||||
await this.cacheService.set(`otp:${phone}:${slug}`, code, 160);
|
||||
await this.cacheService.set(`otp:${slug}:${phone}`, code, 160);
|
||||
|
||||
await this.smsService.sendOtp(phone, code);
|
||||
|
||||
@@ -48,17 +52,20 @@ export class AuthService {
|
||||
return { success: true, message: ' OTP sent successfully' };
|
||||
}
|
||||
|
||||
async verifyOtp(phone: string, code: string) {
|
||||
const cachedCode = await this.cacheService.get(`otp:${phone}`);
|
||||
async verifyOtp(phone: string, slug: string, code: string) {
|
||||
const cachedCode = await this.cacheService.get(`otp:${slug}:${phone}`);
|
||||
if (!cachedCode) throw new BadRequestException('OTP expired or not found');
|
||||
if (cachedCode !== code) throw new BadRequestException('Invalid OTP');
|
||||
|
||||
await this.cacheService.del(`otp:${phone}`);
|
||||
await this.cacheService.del(`otp:${slug}:${phone}`);
|
||||
|
||||
const user = await this.userService.findOrCreateByPhone(phone);
|
||||
|
||||
const rest = await this.restRepository.findOne({ slug });
|
||||
if (!rest) {
|
||||
throw new BadRequestException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
// const accessToken = await this.issueToken(user.id, AuthEntityType.USER);
|
||||
const tokens = await this.tokensService.generateTokens(user);
|
||||
const tokens = await this.tokensService.generateTokens(user, rest);
|
||||
|
||||
return { success: true, message: 'User registered successfully!', tokens };
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import { AuthMessage, UserMessage } from '../../../common/enums/message.enum';
|
||||
import { RefreshToken } from '../../users/entities/refresh-token.entity';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
import { ITokenPayload } from '../interfaces/IToken-payload';
|
||||
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
|
||||
|
||||
@Injectable()
|
||||
export class TokensService {
|
||||
@@ -20,10 +21,11 @@ export class TokensService {
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async generateTokens(user: User, em?: EntityManager) {
|
||||
async generateTokens(user: User, rest: Restaurant, em?: EntityManager) {
|
||||
return this.generateAccessAndRefreshToken(
|
||||
{
|
||||
id: user.id,
|
||||
restId: rest.id,
|
||||
},
|
||||
em,
|
||||
);
|
||||
@@ -33,7 +35,11 @@ export class TokensService {
|
||||
const accessExpire = this.configService.getOrThrow<number>('ACCESS_TOKEN_EXPIRE');
|
||||
const refreshExpire = this.configService.getOrThrow<number>('REFRESH_TOKEN_EXPIRE');
|
||||
|
||||
const accessToken = await this.jwtService.signAsync(payload, { expiresIn: `${accessExpire}m` });
|
||||
const tokenPayload: ITokenPayload = {
|
||||
id: payload.id,
|
||||
restId: payload.restId,
|
||||
};
|
||||
const accessToken = await this.jwtService.signAsync(tokenPayload, { expiresIn: `${accessExpire}m` });
|
||||
const refreshToken = this.generateRefreshToken();
|
||||
|
||||
await this.storeRefreshToken(payload.id, refreshToken, em);
|
||||
|
||||
Reference in New Issue
Block a user