swaf/src/auth/AuthController.ts

35 lines
1.4 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 {REQUIRE_AUTH_MIDDLEWARE, REQUIRE_GUEST_MIDDLEWARE} from "./AuthComponent";
export default abstract class AuthController extends Controller {
public getRoutesPrefix(): string {
return '/auth';
}
public routes() {
this.get('/', this.getAuth, 'auth', REQUIRE_GUEST_MIDDLEWARE);
this.post('/', this.postAuth, 'auth', REQUIRE_GUEST_MIDDLEWARE);
this.get('/check', this.getCheckAuth, 'check_auth');
this.post('/logout', this.postLogout, 'logout', REQUIRE_AUTH_MIDDLEWARE);
}
protected async getAuth(req: Request, res: Response, next: NextFunction): Promise<void> {
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> {
2020-07-25 10:28:50 +02:00
const proof = await req.authGuard.getProofForSession(req.session!);
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
}
}