2020-07-14 15:06:30 +02:00
|
|
|
import Controller from "../Controller";
|
|
|
|
import {NextFunction, Request, Response} from "express";
|
2020-09-25 22:03:22 +02:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
public routes(): void {
|
2020-09-25 22:03:22 +02:00
|
|
|
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');
|
2020-09-25 22:03:22 +02:00
|
|
|
this.post('/logout', this.postLogout, 'logout', RequireAuthMiddleware);
|
2020-07-14 15:06:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +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>;
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
protected async postLogout(req: Request, res: Response, _next: NextFunction): Promise<void> {
|
2020-09-25 22:03:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|