@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,
90 lines (79 loc) • 2.39 kB
text/typescript
import {
Request,
Controller,
Get,
Post,
UseGuards,
Body,
UnauthorizedException,
HttpStatus,
All,
BadRequestException,
UseFilters,
Inject,
} from "@nestjs/common";
import { NestAuthService } from "./nestauth.service";
import { NestAuthGoogleGuard } from "./nestauth-google.guard";
import { NestAuthFacebookGuard } from "./nestauth-facebook.guard";
import { HttpExceptionFilter } from "./http-exception.filter";
export function createDynamicController(
prefix: string,
nestAuthServiceToken: string,
localGuard: any,
) {
(HttpExceptionFilter)
(prefix)
class NestAuthController {
constructor(
(nestAuthServiceToken)
readonly nestAuthService: NestAuthService,
) {}
()
async greetings(): Promise<string> {
return "Welcome to NestAuth. Please check our documentation for more information.";
}
(localGuard)
("login")
async login(() req: any): Promise<any> {
// console.log("nestAuthServiceToken", nestAuthServiceToken);
return this.nestAuthService.login(req.user);
}
("refresh-token")
refreshToken(() params: { refresh_token: string }): Promise<any> {
if (!params.refresh_token) {
throw new BadRequestException("Invalid or expired refresh token");
}
return this.nestAuthService.refreshToken(params.refresh_token);
}
(NestAuthGoogleGuard)
("google")
async googleAuth(() req: any): Promise<any> {}
("google-redirect")
(NestAuthGoogleGuard)
googleAuthRedirect(() req) {
if (!req.user) {
throw new UnauthorizedException("Unable to login with Google");
}
return this.nestAuthService.google(req.user);
}
("/facebook")
(NestAuthFacebookGuard)
async facebookLogin(): Promise<any> {
return HttpStatus.OK;
}
("/facebook-redirect")
(NestAuthFacebookGuard)
async facebookLoginRedirect(() req): Promise<any> {
if (!req.user) {
throw new UnauthorizedException("Unable to login with Facebook");
}
return this.nestAuthService.facebook(req.user);
}
(localGuard)
("logout")
async logout(() req: any) {
return req.logout();
}
}
return NestAuthController;
}