|
@ -46,14 +46,18 @@ class SessionService extends SiteService { |
|
|
options = Object.assign({ |
|
|
options = Object.assign({ |
|
|
requireLogin: true, |
|
|
requireLogin: true, |
|
|
requireAdmin: false, |
|
|
requireAdmin: false, |
|
|
|
|
|
useRedirect: true, |
|
|
loginUri: '/welcome/login', |
|
|
loginUri: '/welcome/login', |
|
|
}, options); |
|
|
}, options); |
|
|
return async (req, res, next) => { |
|
|
return async (req, res, next) => { |
|
|
if (options.requireLogin && !req.user) { |
|
|
if (options.requireLogin && !req.user) { |
|
|
req.session.loginReturnTo = req.url; |
|
|
if (options.useRedirect) { |
|
|
await this.saveSession(req); |
|
|
req.session.loginReturnTo = req.url; |
|
|
this.log.info('redirecting to login', { returnTo: req.url }); |
|
|
await this.saveSession(req); |
|
|
return res.redirect(options.loginUri); |
|
|
this.log.info('redirecting to login', { returnTo: req.url }); |
|
|
|
|
|
return res.redirect(options.loginUri); |
|
|
|
|
|
} |
|
|
|
|
|
return next(new SiteError(403, 'Must sign in to continue')); |
|
|
} |
|
|
} |
|
|
if (options.requireAdmin && (!req.user || !req.user.flags.isAdmin)) { |
|
|
if (options.requireAdmin && (!req.user || !req.user.flags.isAdmin)) { |
|
|
return next(new SiteError(403, 'Administrator privileges are required')); |
|
|
return next(new SiteError(403, 'Administrator privileges are required')); |
|
|