nest-authify
Version:
Complete authentication and authorization package for NestJS - Monolith and Microservices ready with OAuth, JWT, Redis sessions
252 lines (234 loc) • 6.81 kB
text/typescript
import {
Controller,
Get,
HttpStatus,
Inject,
Res,
UseGuards
} from '@nestjs/common';
import {
ApiExcludeEndpoint,
ApiOperation,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { Response } from 'express';
import { AUTH_SERVICE } from '../constants';
import { CurrentUser } from '../decorators/current-user.decorator';
import { Public } from '../decorators/public.decorator';
import { FacebookAuthGuard } from '../guards/facebook-auth.guard';
import { GithubAuthGuard } from '../guards/github-auth.guard';
import { GoogleAuthGuard } from '../guards/google-auth.guard';
import { LoginResponse } from '../interfaces/auth-options.interface';
import { BaseAuthService } from '../services/base-auth.service';
/**
* Controlador para autenticación OAuth
* Soporta Google, Facebook y GitHub
*/
export class OAuthController {
constructor(
private readonly authService: BaseAuthService,
) { }
// ==================== GOOGLE ====================
/**
* Inicia el flujo de autenticación con Google
*/
async googleAuth(): Promise<void> {
// Guard se encarga de la redirección
}
/**
* Callback de Google OAuth
*/
async googleAuthCallback(
user: any,
res: Response,
): Promise<void> {
const session = await this.authService.createSession(user, {
provider: 'google',
});
// Redirigir al frontend con el token
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000';
res.redirect(
`${frontendUrl}/auth/callback?token=${session.accessToken}&refresh=${session.refreshToken}`,
);
}
/**
* Endpoint alternativo para obtener los tokens después del callback
*/
async googleAuthRedirect( user: any): Promise<LoginResponse> {
const session = await this.authService.createSession(user, {
provider: 'google',
});
return {
...session,
user: this.sanitizeUser(user),
};
}
// ==================== FACEBOOK ====================
/**
* Inicia el flujo de autenticación con Facebook
*/
async facebookAuth(): Promise<void> {
// Guard se encarga de la redirección
}
/**
* Callback de Facebook OAuth
*/
async facebookAuthCallback(
user: any,
res: Response,
): Promise<void> {
const session = await this.authService.createSession(user, {
provider: 'facebook',
});
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000';
res.redirect(
`${frontendUrl}/auth/callback?token=${session.accessToken}&refresh=${session.refreshToken}`,
);
}
/**
* Endpoint alternativo para obtener los tokens después del callback
*/
async facebookAuthRedirect(
user: any,
): Promise<LoginResponse> {
const session = await this.authService.createSession(user, {
provider: 'facebook',
});
return {
...session,
user: this.sanitizeUser(user),
};
}
// ==================== GITHUB ====================
/**
* Inicia el flujo de autenticación con GitHub
*/
async githubAuth(): Promise<void> {
// Guard se encarga de la redirección
}
/**
* Callback de GitHub OAuth
*/
async githubAuthCallback(
user: any,
res: Response,
): Promise<void> {
const session = await this.authService.createSession(user, {
provider: 'github',
});
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000';
res.redirect(
`${frontendUrl}/auth/callback?token=${session.accessToken}&refresh=${session.refreshToken}`,
);
}
/**
* Endpoint alternativo para obtener los tokens después del callback
*/
async githubAuthRedirect( user: any): Promise<LoginResponse> {
const session = await this.authService.createSession(user, {
provider: 'github',
});
return {
...session,
user: this.sanitizeUser(user),
};
}
/**
* Sanitiza el objeto de usuario eliminando campos sensibles
*/
private sanitizeUser(user: any): any {
const { password, ...sanitized } = user;
return sanitized;
}
}