Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pageMiddleware args #5787

Merged
merged 3 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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