mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 12:33:17 +00:00
add auth
This commit is contained in:
87
src/modules/users/services/users.service.ts
Normal file
87
src/modules/users/services/users.service.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import {
|
||||
CreateUserInput,
|
||||
EntityWithPaginationType,
|
||||
User,
|
||||
UserViewType,
|
||||
} from '../../../types/types';
|
||||
import { addHours } from 'date-fns';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { UsersRepository } from '../infrastructure/users.repository';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private usersRepository: UsersRepository) {}
|
||||
|
||||
async getUsers(page: number, pageSize: number, searchNameTerm: string) {
|
||||
return await this.usersRepository.getUsers(page, pageSize, searchNameTerm);
|
||||
}
|
||||
|
||||
async getUserById(id: string) {
|
||||
return await this.usersRepository.findUserById(id);
|
||||
}
|
||||
|
||||
async createUser(
|
||||
name: string,
|
||||
password: string,
|
||||
email: string,
|
||||
): Promise<UserViewType | null> {
|
||||
const passwordHash = await this._generateHash(password);
|
||||
const newUser: CreateUserInput = {
|
||||
name: name || email.split('@')[0],
|
||||
email: email,
|
||||
password: passwordHash,
|
||||
verificationToken: uuidv4(),
|
||||
verificationTokenExpiry: addHours(new Date(), 24),
|
||||
isEmailVerified: false,
|
||||
};
|
||||
const createdUser = await this.usersRepository.createUser(newUser);
|
||||
if (!createdUser) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
id: createdUser.id,
|
||||
name: createdUser.name,
|
||||
email: createdUser.email,
|
||||
};
|
||||
}
|
||||
|
||||
async deleteUserById(id: string): Promise<boolean> {
|
||||
return await this.usersRepository.deleteUserById(id);
|
||||
}
|
||||
|
||||
async addRevokedToken(token: string) {
|
||||
const secretKey = process.env.JWT_SECRET_KEY;
|
||||
if (!secretKey) throw new Error('JWT_SECRET_KEY is not defined');
|
||||
|
||||
try {
|
||||
const decoded: any = jwt.verify(token, secretKey);
|
||||
return this.usersRepository.revokeToken(decoded.userId, token);
|
||||
} catch (e) {
|
||||
console.log('Decoding error: e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private async _generateHash(password: string) {
|
||||
return await bcrypt.hash(password, 10);
|
||||
}
|
||||
}
|
||||
|
||||
export interface IUsersRepository {
|
||||
getUsers(
|
||||
page: number,
|
||||
pageSize: number,
|
||||
searchNameTerm: string,
|
||||
): Promise<EntityWithPaginationType<UserViewType>>;
|
||||
|
||||
createUser(newUser: CreateUserInput): Promise<User | null>;
|
||||
|
||||
deleteUserById(id: string): Promise<boolean>;
|
||||
|
||||
findUserById(id: string): Promise<User | null>;
|
||||
|
||||
revokeToken(id: string, token: string): Promise<User | null>;
|
||||
}
|
||||
Reference in New Issue
Block a user