auth
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user