@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,
74 lines (64 loc) • 2.1 kB
text/typescript
import {
Request,
Controller,
Get,
Post,
UseGuards,
Body,
UnauthorizedException,
HttpStatus,
All,
UseFilters,
} from "@nestjs/common";
import { NestAuthService } from "./nestauth.service";
import { NestAuthLocalGuard } from "./nestauth-local.guard";
import { NestAuthGoogleGuard } from "./nestauth-google.guard";
import { NestAuthFacebookGuard } from "./nestauth-facebook.guard";
import { HttpExceptionFilter } from "./http-exception.filter";
(HttpExceptionFilter)
("nestauth")
export class NestAuthController {
constructor(private readonly nestAuthService: NestAuthService) {}
()
async greetings(): Promise<string> {
return "Welcome to NestAuth. Please check our documentation for more information.";
}
(NestAuthLocalGuard)
("login")
async login(() req): Promise<any> {
return this.nestAuthService.login(req.user);
}
("refresh-token")
refreshToken(() params: { refresh_token: string }): Promise<any> {
return this.nestAuthService.refreshToken(params.refresh_token);
}
(NestAuthGoogleGuard)
("google")
async googleAuth(() req): 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);
}
(NestAuthLocalGuard)
("logout")
async logout(() req) {
return req.logout();
}
}