Commit d8a0d9c0 authored by Цой Кирилл's avatar Цой Кирилл

БЭм bitch

parent bf28139e
// import { Body, HttpException, HttpStatus, Injectable, UnauthorizedException } from '@nestjs/common';
// import { UserDto } from '../users/models/user.dto';
// import { UsersService } from '../users/users.service';
// import { JwtService } from '@nestjs/jwt';
// import * as bcrypt from "bcryptjs"
// import { User } from '../users/models/user.entity';
// @Injectable()
// export class AuthService {
// constructor(
// private readonly userService: UsersService,
// private readonly jwtService: JwtService
// ) {}
// async auth(userDto: UserDto){
// const user = await this.validateUser(userDto)
// return this.generateToken(user)
// }
// async register(userDto: UserDto){
// const candidate = await this.userService.getUserByEmail(userDto.email)
// if (candidate) throw new HttpException('User already exists', HttpStatus.BAD_REQUEST)
// const hashPassword = await bcrypt.hash(candidate.password, 5)
// const user = await this.userService.createUser({ ...userDto, password: hashPassword})
// return this.generateToken(user)
// }
// async generateToken(user: User) {
// const payload = {
// email: user.email,
// id: user.id,
// name: user.name
// }
// return {
// token: this.jwtService.sign(payload)
// }
// }
// async validateUser(userDto: UserDto){
// const user = await this.userService.getUserByEmail(userDto.email)
// const passwordValid = await bcrypt.compare(userDto.password, user.password)
// if (user && passwordValid) return user
// throw new UnauthorizedException({ messaage: "Invalid password" })
// }
// }
import { Body, HttpException, HttpStatus, Injectable, UnauthorizedException } from '@nestjs/common'; import { Body, HttpException, HttpStatus, Injectable, UnauthorizedException } from '@nestjs/common';
import { UserDto } from '../users/models/user.dto'; import { UserDto } from '../users/models/user.dto';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
...@@ -23,7 +73,7 @@ export class AuthService { ...@@ -23,7 +73,7 @@ export class AuthService {
const candidate = await this.userService.getUserByEmail(userDto.email) const candidate = await this.userService.getUserByEmail(userDto.email)
if (candidate) throw new HttpException('User already exists', HttpStatus.BAD_REQUEST) if (candidate) throw new HttpException('User already exists', HttpStatus.BAD_REQUEST)
const hashPassword = await bcrypt.hash(candidate.password, 5) const hashPassword = await bcrypt.hash(userDto.password, 5)
const user = await this.userService.createUser({ ...userDto, password: hashPassword}) const user = await this.userService.createUser({ ...userDto, password: hashPassword})
return this.generateToken(user) return this.generateToken(user)
} }
...@@ -42,7 +92,7 @@ export class AuthService { ...@@ -42,7 +92,7 @@ export class AuthService {
async validateUser(userDto: UserDto){ async validateUser(userDto: UserDto){
const user = await this.userService.getUserByEmail(userDto.email) const user = await this.userService.getUserByEmail(userDto.email)
const passwordValid = await bcrypt.compare(userDto.password, user.password) const passwordValid = bcrypt.compare(userDto.password, user.password)
if (user && passwordValid) return user if (user && passwordValid) return user
throw new UnauthorizedException({ messaage: "Invalid password" }) throw new UnauthorizedException({ messaage: "Invalid password" })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment