UNPKG

@next-nest-auth/nestauth

Version:

NestAuth is an authentication solution for NestJS applications, designed to handle user login, session management, and token-based authentication (JWT). It integrates seamlessly with Next.js and other frontends to provide a unified authentication system,

66 lines (56 loc) 2.13 kB
import { Inject, Injectable, UnauthorizedException } from "@nestjs/common"; import { FacebookProfileType, GoogleProfileType, JwtPayloadType, NestAuthInterface, } from "./nestauth.interface"; import { JwtService } from "@nestjs/jwt"; @Injectable() export class NestAuthService { constructor( private jwtService: JwtService, @Inject("UserService") private readonly userService: NestAuthInterface, @Inject("JWT_EXPIRES_IN") private readonly jwtExpiresIn: string, @Inject("JWT_REFRESH_TOKEN_EXPIRES_IN") private readonly jwtRefreshTokenExpiresIn: string ) {} async login(user: any): Promise<any> { return { accessToken: this.jwtService.sign(user, { expiresIn: this.jwtExpiresIn || "15m", }), refreshToken: this.jwtService.sign(user, { expiresIn: "7d" }), accessTokenExpiresIn: this.jwtExpiresIn || "15m", refreshTokenExpiresIn: this.jwtRefreshTokenExpiresIn || "7d", }; } async google(user: GoogleProfileType): Promise<any> { const payload: JwtPayloadType = await this.userService.google(user); if (!payload) { throw new UnauthorizedException("Invalid credentials"); } return this.login(payload); } async facebook(user: FacebookProfileType): Promise<any> { const payload: JwtPayloadType = await this.userService.facebook(user); if (!payload) { throw new UnauthorizedException("Invalid credentials"); } return this.login(payload); } async refreshToken(refreshToken: string) { try { const payload = this.jwtService.verify(refreshToken); const user = await this.userService.getUserById(payload.sub); if (!user) { throw new UnauthorizedException( "Invalid or expired refresh token" ); } return this.login(user); } catch (err) { throw new UnauthorizedException("Invalid or expired refresh token"); } } }