Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fcaps committed Nov 20, 2023
1 parent de1acc9 commit e8286e2
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions routes/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,22 @@ exports.flashMessage = function(req, res, next) {
}
};

exports.isAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
return next()
}
exports.isAuthenticated = (redirectAfterLogin = null) => {
return (req, res, next) => {
if (req.isAuthenticated()) {
return next()
}

if (req.xhr || req.headers.accept.indexOf('json') > -1) {
return res.status(401).json({error: 'Unauthorized'})
}

if (req.session) {
req.session.redirectUrl = redirectAfterLogin || req.headers.referer || req.originalUrl || req.url;
}

if (req.xhr || req.headers.accept.indexOf('json') > -1) {
return res.status(401).json({error: 'Unauthorized'})
return res.redirect('/login')
}

return res.redirect('/login')
}


Expand Down

0 comments on commit e8286e2

Please sign in to comment.