@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,
29 lines (26 loc) • 823 B
text/typescript
import { ExtractJwt, Strategy } from "passport-jwt";
import { PassportStrategy } from "@nestjs/passport";
import { Inject, Injectable } from "@nestjs/common";
import { JwtPayload } from "jsonwebtoken";
import * as macaddress from "macaddress";
()
export class NestAuthJwtStrategy extends PassportStrategy(Strategy) {
constructor(("JWT_SECRET") jwtSecret: string) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: jwtSecret,
});
}
async validate(payload: JwtPayload) {
return {
userId: payload.sub,
macId: payload.macId ?? (await macaddress.one()),
email: payload.email,
role: payload.role,
name: payload.name,
username: payload.username,
pic: payload.pic,
};
}
}