Skip to content

Commit

Permalink
Fix pageMiddleware args (#5787)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed May 26, 2021
1 parent a7b05ed commit bb4f4ac
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .changeset/wet-islands-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@keystone-next/auth': major
'@keystone-next/keystone': major
'@keystone-next/types': patch
---

Replaced `req, session, createContext` args to `config.ui.pageMiddleware` with a `context` arg.
14 changes: 5 additions & 9 deletions packages-next/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,9 @@ export function createAuth<GeneratedListTypes extends BaseGeneratedListTypes>({
* - to the init page when initFirstItem is configured, and there are no user in the database
* - to the signin page when no valid session is present
*/
const pageMiddleware: AdminUIConfig['pageMiddleware'] = async ({
req,
isValidSession,
createContext,
session,
}) => {
const pathname = url.parse(req.url!).pathname!;
const pageMiddleware: AdminUIConfig['pageMiddleware'] = async ({ context, isValidSession }) => {
const { req, session } = context;
const pathname = url.parse(req!.url!).pathname!;

if (isValidSession) {
if (pathname === '/signin' || (initFirstItem && pathname === '/init')) {
Expand All @@ -107,7 +103,7 @@ export function createAuth<GeneratedListTypes extends BaseGeneratedListTypes>({
}

if (!session && initFirstItem) {
const count = await createContext({}).sudo().lists[listKey].count({});
const count = await context.sudo().lists[listKey].count({});
if (count === 0) {
if (pathname !== '/init') {
return { kind: 'redirect', to: '/init' };
Expand All @@ -117,7 +113,7 @@ export function createAuth<GeneratedListTypes extends BaseGeneratedListTypes>({
}

if (!session && pathname !== '/signin') {
return { kind: 'redirect', to: `/signin?from=${encodeURIComponent(req.url!)}` };
return { kind: 'redirect', to: `/signin?from=${encodeURIComponent(req!.url!)}` };
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ export const createAdminUIServer = async (
sessionContext: sessionStrategy
? await createSessionContext(sessionStrategy, req, res, createContext)
: undefined,
req,
});
const isValidSession = ui?.isAccessAllowed
? await ui.isAccessAllowed(context)
: sessionStrategy
? context.session !== undefined
: true;
const maybeRedirect = await ui?.pageMiddleware?.({
req,
session: context.session,
isValidSession,
createContext,
});
const maybeRedirect = await ui?.pageMiddleware?.({ context, isValidSession });
if (maybeRedirect) {
res.redirect(maybeRedirect.to);
return;
Expand Down
6 changes: 1 addition & 5 deletions packages-next/types/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { IncomingMessage } from 'http';
import { CorsOptions } from 'cors';
import type { GraphQLSchema } from 'graphql';
import type { Config } from 'apollo-server-express';

import type { ImageMode, FileMode, KeystoneContext } from '..';

import { CreateContext } from '../core';
import type { BaseKeystone } from '../base';
import { SessionStrategy } from '../session';
import type { MaybePromise } from '../utils';
Expand Down Expand Up @@ -104,10 +102,8 @@ export type AdminUIConfig = {
// path?: string;
getAdditionalFiles?: ((config: KeystoneConfig) => MaybePromise<AdminFileToWrite[]>)[];
pageMiddleware?: (args: {
req: IncomingMessage;
session: any;
context: KeystoneContext;
isValidSession: boolean;
createContext: CreateContext;
}) => MaybePromise<{ kind: 'redirect'; to: string } | void>;
};

Expand Down

1 comment on commit bb4f4ac

@vercel
Copy link

@vercel vercel bot commented on bb4f4ac May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.