This commit is contained in:
2025-05-12 11:28:18 +07:00
parent 56df7185f3
commit 6e9bfa4b82
20 changed files with 863 additions and 86 deletions

View File

@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import User from './user.entity';
import CreateUserDto from './dto/createUser.dto';
import * as bcrypt from 'bcrypt';
@Injectable()
export class UsersService {
@@ -22,9 +23,31 @@ export class UsersService {
);
}
async getById(id: number) {
const user = await this.usersRepository.findOne({ where: { id } });
if (user) {
return user;
}
throw new HttpException('User with this id does not exist', HttpStatus.NOT_FOUND);
}
async create(userData: CreateUserDto) {
const newUser = this.usersRepository.create(userData);
await this.usersRepository.save(newUser);
return newUser;
}
async setCurrentRefreshToken(refreshToken: string, userId: number) {
const currentHashedRefreshToken = await bcrypt.hash(refreshToken, 10);
await this.usersRepository.update(userId, {
currentHashedRefreshToken,
});
}
async removeRefreshToken(userId: number) {
return this.usersRepository.update(userId, {
currentHashedRefreshToken: null,
});
}
}