46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
/* eslint-disable prettier/prettier */
|
|
import { Injectable } from '@nestjs/common'
|
|
import { JwtService } from '@nestjs/jwt'
|
|
import { User } from '@prisma/client'
|
|
import { UserDTO } from 'src/user/dtos/user.dto'
|
|
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 | null> {
|
|
const user = await this.userService.fetchOne({ email: email })
|
|
if (user && user.password === password) {
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
const { password, ...result } = user
|
|
return result
|
|
}
|
|
return null
|
|
}
|
|
|
|
getTokens(user: User) {
|
|
//console.log(user)
|
|
const access_token_payload = {
|
|
email: user.email,
|
|
name: user.name
|
|
}
|
|
const refresh_token_payload = {}
|
|
return {
|
|
tokens: {
|
|
access_token: this.jwtService.sign(access_token_payload),
|
|
refresh_token: this.jwtService.sign(refresh_token_payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
async register(data: UserDTO): Promise<any | null> {
|
|
const user = await this.userService.add(data)
|
|
if (!user) return null
|
|
return this.getTokens(user)
|
|
}
|
|
}
|