import Controller from "../Controller"; import {NextFunction, Request, Response} from "express"; import {AuthMiddleware, RequireAuthMiddleware, RequireGuestMiddleware} from "./AuthComponent"; export default abstract class AuthController extends Controller { public getRoutesPrefix(): string { return '/auth'; } public routes(): void { this.get('/', this.getAuth, 'auth', RequireGuestMiddleware); this.post('/', this.postAuth, 'auth', RequireGuestMiddleware); this.get('/check', this.getCheckAuth, 'check_auth'); this.post('/logout', this.postLogout, 'logout', RequireAuthMiddleware); } protected async getAuth(req: Request, res: Response, _next: NextFunction): Promise { const registerEmail = req.flash('register_confirm_email'); res.render('auth/auth', { register_confirm_email: registerEmail.length > 0 ? registerEmail[0] : null, }); } protected abstract async postAuth(req: Request, res: Response, next: NextFunction): Promise; protected abstract async getCheckAuth(req: Request, res: Response, next: NextFunction): Promise; protected async postLogout(req: Request, res: Response, _next: NextFunction): Promise { const proof = await req.as(AuthMiddleware).getAuthGuard().getProof(req); await proof?.revoke(); req.flash('success', 'Successfully logged out.'); res.redirect(req.query.redirect_uri?.toString() || '/'); } }