38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
/* eslint-disable prettier/prettier */
|
|
import { Injectable } from '@nestjs/common'
|
|
import { JwtService } from '@nestjs/jwt'
|
|
import { UserService } from '../user/user.service'
|
|
|
|
@Injectable()
|
|
export class AuthService {
|
|
constructor(
|
|
private userService: UserService,
|
|
private jwtService: JwtService
|
|
) {}
|
|
|
|
//app.controller.ts에서 @UseGuards(AuthGuard('local'))용
|
|
async validateUser(email: string, password: string): Promise<any> {
|
|
const user = await this.userService.fetchOneByEmail(email)
|
|
if (user && user.password === password) {
|
|
const { password, ...result } = user
|
|
// result는 password 를 제외한 user의 모든 정보를 포함한다.
|
|
//console.log(result)
|
|
console.log(password)
|
|
return result
|
|
}
|
|
return null
|
|
}
|
|
|
|
async login(user: any) {
|
|
//console.log(user)
|
|
const payload = {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
roles: [user.role]
|
|
}
|
|
// console.log(payload)
|
|
return { access_token: this.jwtService.sign(payload) }
|
|
}
|
|
}
|