swaf/src/auth/AuthController.ts

36 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-07-14 15:06:30 +02:00
import Controller from "../Controller";
import {NextFunction, Request, Response} from "express";
import {AuthMiddleware, RequireAuthMiddleware, RequireGuestMiddleware} from "./AuthComponent";
2020-07-14 15:06:30 +02:00
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);
2020-07-14 15:06:30 +02:00
this.get('/check', this.getCheckAuth, 'check_auth');
this.post('/logout', this.postLogout, 'logout', RequireAuthMiddleware);
2020-07-14 15:06:30 +02:00
}
protected async getAuth(req: Request, res: Response, _next: NextFunction): Promise<void> {
2020-07-14 15:06:30 +02:00
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<void>;
protected abstract async getCheckAuth(req: Request, res: Response, next: NextFunction): Promise<void>;
protected async postLogout(req: Request, res: Response, _next: NextFunction): Promise<void> {
const proof = await req.as(AuthMiddleware).getAuthGuard().getProof(req);
2020-07-25 10:28:50 +02:00
await proof?.revoke();
2020-07-14 15:06:30 +02:00
req.flash('success', 'Successfully logged out.');
2020-07-15 11:42:49 +02:00
res.redirect(req.query.redirect_uri?.toString() || '/');
2020-07-14 15:06:30 +02:00
}
}