parent
4a1cc577b5
commit
193727b93f
2 changed files with 44 additions and 1 deletions
@ -0,0 +1,37 @@ |
||||
import { Handler, Request, Response } from 'express'; |
||||
|
||||
import { stdout } from './shell'; |
||||
|
||||
export const assertAuthentication: (options?: { |
||||
failureRedirect?: string; |
||||
failureReturnTo?: boolean | string; |
||||
}) => Handler = ({ failureRedirect, failureReturnTo } = {}) => { |
||||
const redirectOnFailure: (response: Response) => void = failureRedirect |
||||
? (response) => response.redirect(failureRedirect) |
||||
: (response) => response.status(404).send(); |
||||
|
||||
let getSessionReturnTo: ((request: Request) => string) | undefined; |
||||
|
||||
if (failureReturnTo === true) { |
||||
getSessionReturnTo = ({ originalUrl, url }) => originalUrl || url; |
||||
} else if (typeof failureReturnTo === 'string') { |
||||
getSessionReturnTo = () => failureReturnTo; |
||||
} |
||||
|
||||
return (request, response, next) => { |
||||
const { originalUrl, session } = request; |
||||
const { passport } = session; |
||||
|
||||
if (!passport?.user) { |
||||
session.returnTo = getSessionReturnTo?.call(null, request); |
||||
|
||||
stdout( |
||||
`Unauthenticated access to ${originalUrl}; set return to ${session.returnTo}`, |
||||
); |
||||
|
||||
return redirectOnFailure?.call(null, response); |
||||
} |
||||
|
||||
next(); |
||||
}; |
||||
}; |
Loading…
Reference in new issue