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,

25 lines (23 loc) 786 B
import { ExecutionContext, Injectable, UnauthorizedException, } from "@nestjs/common"; import { AuthGuard } from "@nestjs/passport"; import * as macaddress from "macaddress"; @Injectable() export class NestAuthJwtGuard extends AuthGuard("jwt") { async canActivate(context: ExecutionContext): Promise<boolean> { const request = context.switchToHttp().getRequest(); await super.canActivate(context); const user = request.user; if (!user) { throw new UnauthorizedException("Unauthorized: Invalid token"); } const currentMacId = await macaddress.one(); if (user.macId !== currentMacId) { throw new UnauthorizedException("Unauthorized: Device mismatch"); } return true; } }