@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,
40 lines (33 loc) • 1.18 kB
text/typescript
import {
ExecutionContext,
Injectable,
UnauthorizedException,
} from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
import * as macaddress from "macaddress";
()
export class NestAuthJwtGuard extends AuthGuard("jwt") {
// ✅ Custom error handling here
handleRequest(err: any, user: any, info: any, context: ExecutionContext) {
// console.log("err, user, info, context1");
if (err || !user) {
throw new UnauthorizedException("Unauthorized: Invalid or missing token");
}
return user;
}
// ✅ Make sure to return the result of `super.canActivate`
async canActivate(context: ExecutionContext): Promise<boolean> {
const can = (await super.canActivate(context)) as boolean;
if (!can) return false; // short-circuit if base guard fails
const request = context.switchToHttp().getRequest();
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;
}
}