nestjs_backend/src/auth/auth.service.ts
2022-08-17 16:57:03 +09:00

28 lines
857 B
TypeScript

import { Injectable } from '@nestjs/common'
import { UserService } from 'src/user/user.service'
import { JwtService } from '@nestjs/jwt'
@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의 모든 정보를 포함한다.
return result
}
return null
}
async login(user: any) {
const payload = { id: user.id, username: user.username, role: user.role }
return { access_token: this.jwtService.sign(payload) }
}
}