Skip to content

Commit

Permalink
Fixes redirect to signin page in @keystone-6/auth when using `baseP…
Browse files Browse the repository at this point in the history
…ath` (#8641)

Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
  • Loading branch information
Josh Calder and dcousens authored Jun 19, 2023
1 parent 6b1a64a commit 19bb460
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .changeset/no-redirect.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@keystone-6/core': patch
'@keystone-6/auth': patch
---

Removes `?from` redirect from `/signin` page to prevent open redirection.
6 changes: 6 additions & 0 deletions .changeset/twelve-toes-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystone-6/core': patch
'@keystone-6/auth': patch
---

Adds `basePath` with a default of `'/'` to `pageMiddleware` to support redirects when using `ui.basePath`
20 changes: 8 additions & 12 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo>({
return filesToWrite;
};

/**
* publicAuthPages
*
* Must be added to the ui.publicPages config
*/
const authPublicPages = ['/signin'];

/**
* extendGraphqlSchema
*
Expand Down Expand Up @@ -214,28 +207,30 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo>({
async function authMiddleware<TypeInfo extends BaseKeystoneTypeInfo>({
context,
wasAccessAllowed,
basePath,
}: {
context: KeystoneContext<TypeInfo>;
wasAccessAllowed: boolean;
basePath: string;
}): Promise<{ kind: 'redirect'; to: string } | void> {
const { req } = context;
const { pathname } = new URL(req!.url!, 'http://_');

// redirect to init if initFirstItem conditions are met
if (pathname !== '/init' && (await hasInitFirstItemConditions(context))) {
return { kind: 'redirect', to: '/init' };
if (pathname !== `${basePath}/init` && (await hasInitFirstItemConditions(context))) {
return { kind: 'redirect', to: `${basePath}/init` };
}

// redirect to / if attempting to /init and initFirstItem conditions are not met
if (pathname === '/init' && !(await hasInitFirstItemConditions(context))) {
return { kind: 'redirect', to: '/' };
if (pathname === `${basePath}/init` && !(await hasInitFirstItemConditions(context))) {
return { kind: 'redirect', to: basePath };
}

// don't redirect if we have access
if (wasAccessAllowed) return;

// otherwise, redirect to signin
return { kind: 'redirect', to: '/signin' };
return { kind: 'redirect', to: `${basePath}/signin` };
}

function defaultIsAccessAllowed({ session, sessionStrategy }: KeystoneContext) {
Expand Down Expand Up @@ -263,6 +258,7 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo>({
pageMiddleware,
publicPages = [],
} = ui || {};
const authPublicPages = [`${ui?.basePath ?? ''}/signin`];
ui = {
...ui,
publicPages: [...publicPages, ...authPublicPages],
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/lib/server/createAdminUIMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ export function createAdminUIMiddlewareWithNextApp(
const handle = nextApp.getRequestHandler();

const {
ui: { isAccessAllowed = defaultIsAccessAllowed, pageMiddleware, publicPages = [] } = {},
ui: {
isAccessAllowed = defaultIsAccessAllowed,
pageMiddleware,
publicPages = [],
basePath = '',
} = {},
} = config;

if (basePath.endsWith('/')) throw new TypeError('basePath must not end with a trailing slash');

return async (req: express.Request, res: express.Response) => {
const { pathname } = url.parse(req.url);

if (pathname?.startsWith('/_next') || pathname?.startsWith('/__next')) {
if (pathname?.startsWith(`${basePath}/_next`) || pathname?.startsWith(`${basePath}/__next`)) {
return handle(req, res);
}

Expand All @@ -38,6 +45,7 @@ export function createAdminUIMiddlewareWithNextApp(
const shouldRedirect = await pageMiddleware?.({
context,
wasAccessAllowed,
basePath,
});

if (shouldRedirect) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export type AdminUIConfig<TypeInfo extends BaseKeystoneTypeInfo> = {
pageMiddleware?: (args: {
context: KeystoneContext<TypeInfo>;
wasAccessAllowed: boolean;
basePath: string;
}) => MaybePromise<{ kind: 'redirect'; to: string } | void>;
};

Expand Down

0 comments on commit 19bb460

Please sign in to comment.