fix(striker-ui-api): set query param returnTo in assert authentication

main
Tsu-ba-me 2 years ago
parent 5a51f9a31a
commit b72283a06c
  1. 54
      striker-ui-api/src/lib/assertAuthentication.ts
  2. 7
      striker-ui-api/src/routes/static.ts

@ -2,10 +2,19 @@ import { Handler } from 'express';
import { stdout } from './shell'; import { stdout } from './shell';
type HandlerParameters = Parameters<Handler>;
type AssertAuthenticationFailFunction = (
returnTo?: string,
...args: HandlerParameters
) => void;
type AssertAuthenticationSucceedFunction = (...args: HandlerParameters) => void;
type AssertAuthenticationOptions = { type AssertAuthenticationOptions = {
fail?: string | ((...args: Parameters<Handler>) => void); fail?: string | AssertAuthenticationFailFunction;
failReturnTo?: boolean | string; failReturnTo?: boolean | string;
succeed?: string | ((...args: Parameters<Handler>) => void); succeed?: string | AssertAuthenticationSucceedFunction;
}; };
type AssertAuthenticationFunction = ( type AssertAuthenticationFunction = (
@ -13,42 +22,47 @@ type AssertAuthenticationFunction = (
) => Handler; ) => Handler;
export const assertAuthentication: AssertAuthenticationFunction = ({ export const assertAuthentication: AssertAuthenticationFunction = ({
fail: initFail = (request, response) => response.status(404).send(), fail: initFail = (rt, rq, response) => response.status(404).send(),
failReturnTo, failReturnTo,
succeed: initSucceed = (request, response, next) => next(), succeed: initSucceed = (request, response, next) => next(),
}: AssertAuthenticationOptions = {}) => { }: AssertAuthenticationOptions = {}) => {
const fail: (...args: Parameters<Handler>) => void = let getReturnTo: ((...args: HandlerParameters) => string) | undefined;
if (failReturnTo === true) {
getReturnTo = ({ path }) => path;
} else if (typeof failReturnTo === 'string') {
getReturnTo = () => failReturnTo;
}
const fail: AssertAuthenticationFailFunction =
typeof initFail === 'string' typeof initFail === 'string'
? (request, response) => response.redirect(initFail) ? (returnTo, rq, response) =>
response.redirect(returnTo ? `${initFail}?rt=${returnTo}` : initFail)
: initFail; : initFail;
const succeed: (...args: Parameters<Handler>) => void = const succeed: AssertAuthenticationSucceedFunction =
typeof initSucceed === 'string' typeof initSucceed === 'string'
? (request, response) => response.redirect(initSucceed) ? (request, response) => response.redirect(initSucceed)
: initSucceed; : initSucceed;
let getReturnTo: ((...args: Parameters<Handler>) => string) | undefined;
if (failReturnTo === true) {
getReturnTo = ({ originalUrl, url }) => originalUrl || url;
} else if (typeof failReturnTo === 'string') {
getReturnTo = () => failReturnTo;
}
return (...args) => { return (...args) => {
const { 0: request } = args; const { 0: request } = args;
const { originalUrl, session } = request; const { path, session } = request;
const { passport } = session; const { passport } = session;
if (passport?.user) return succeed(...args); if (passport?.user) return succeed(...args);
session.returnTo = getReturnTo?.call(null, ...args); const rt = getReturnTo?.call(null, ...args);
stdout(`Unauthenticated access to ${path}`);
if (rt) {
stdout(`Set session.returnTo=${rt}`);
stdout( session.returnTo = rt;
`Unauthenticated access to ${originalUrl}; set return to ${session.returnTo}`, }
);
return fail(...args); return fail(rt, ...args);
}; };
}; };

@ -19,9 +19,10 @@ router.use(
if (/^[/]login/.test(originalUrl)) { if (/^[/]login/.test(originalUrl)) {
stdout(`Static:login requested`); stdout(`Static:login requested`);
return assertAuthentication({ fail: (rq, rs, nx) => nx(), succeed: '/' })( return assertAuthentication({
...args, fail: (rt, rq, rs, nx) => nx(),
); succeed: '/',
})(...args);
} }
const parts = originalUrl.replace(/[/]$/, '').split('/'); const parts = originalUrl.replace(/[/]$/, '').split('/');

Loading…
Cancel
Save