2021-07-18 21:42:36 +02:00
|
|
|
import { Response } from 'express';
|
2022-09-13 09:59:34 +02:00
|
|
|
import { promisify } from 'util';
|
2022-11-23 14:58:02 +01:00
|
|
|
import { IUnleashConfig } from '../types';
|
2021-07-18 21:42:36 +02:00
|
|
|
import Controller from './controller';
|
|
|
|
import { IAuthRequest } from './unleash-types';
|
2022-09-23 14:19:17 +02:00
|
|
|
import { IUnleashServices } from '../types';
|
|
|
|
import SessionService from '../services/session-service';
|
2021-07-18 21:42:36 +02:00
|
|
|
|
|
|
|
class LogoutController extends Controller {
|
2022-06-03 11:50:58 +02:00
|
|
|
private clearSiteDataOnLogout: boolean;
|
|
|
|
|
|
|
|
private cookieName: string;
|
|
|
|
|
2021-07-18 21:42:36 +02:00
|
|
|
private baseUri: string;
|
|
|
|
|
2022-09-23 14:19:17 +02:00
|
|
|
private sessionService: SessionService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{ sessionService }: Pick<IUnleashServices, 'sessionService'>,
|
|
|
|
) {
|
2021-07-18 21:42:36 +02:00
|
|
|
super(config);
|
2022-09-23 14:19:17 +02:00
|
|
|
this.sessionService = sessionService;
|
2021-07-18 21:42:36 +02:00
|
|
|
this.baseUri = config.server.baseUriPath;
|
2022-06-03 11:50:58 +02:00
|
|
|
this.clearSiteDataOnLogout = config.session.clearSiteDataOnLogout;
|
|
|
|
this.cookieName = config.session.cookieName;
|
2021-07-18 21:42:36 +02:00
|
|
|
this.get('/', this.logout);
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout(req: IAuthRequest, res: Response): Promise<void> {
|
|
|
|
if (req.session) {
|
|
|
|
// Allow SSO to register custom logout logic.
|
|
|
|
if (req.session.logoutUrl) {
|
|
|
|
res.redirect(req.session.logoutUrl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.logout) {
|
2022-09-13 09:59:34 +02:00
|
|
|
if (this.isReqLogoutWithoutCallback(req.logout)) {
|
|
|
|
// passport < 0.6.0
|
|
|
|
req.logout();
|
|
|
|
} else {
|
|
|
|
// for passport >= 0.6.0, a callback function is expected as first argument.
|
|
|
|
// to reuse controller error handling, function is turned into a promise
|
|
|
|
const logoutAsyncFn = promisify(req.logout).bind(req);
|
|
|
|
await logoutAsyncFn();
|
|
|
|
}
|
2021-07-18 21:42:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:59:34 +02:00
|
|
|
if (req.session) {
|
2022-09-23 14:19:17 +02:00
|
|
|
if (req.session.user?.id) {
|
|
|
|
await this.sessionService.deleteSessionsForUser(
|
|
|
|
req.session.user.id,
|
|
|
|
);
|
|
|
|
}
|
2022-09-13 09:59:34 +02:00
|
|
|
req.session.destroy();
|
|
|
|
}
|
2022-06-03 11:50:58 +02:00
|
|
|
res.clearCookie(this.cookieName);
|
|
|
|
|
|
|
|
if (this.clearSiteDataOnLogout) {
|
|
|
|
res.set('Clear-Site-Data', '"cookies", "storage"');
|
|
|
|
}
|
2022-09-23 14:19:17 +02:00
|
|
|
if (req.user?.id) {
|
|
|
|
await this.sessionService.deleteSessionsForUser(req.user.id);
|
|
|
|
}
|
2021-07-18 21:42:36 +02:00
|
|
|
res.redirect(`${this.baseUri}/`);
|
|
|
|
}
|
2022-09-13 09:59:34 +02:00
|
|
|
|
|
|
|
private isReqLogoutWithoutCallback(
|
|
|
|
logout: IAuthRequest['logout'],
|
|
|
|
): logout is () => void {
|
|
|
|
return logout.length === 0;
|
|
|
|
}
|
2021-07-18 21:42:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default LogoutController;
|