Skip to content

Commit

Permalink
Only return withAuth from createAuth (#4986)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 3, 2021
1 parent 098ce08 commit 03abbab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-lobsters-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/auth': major
---

Simplified `createAuth()` to only return `{ withAuth }`.
35 changes: 17 additions & 18 deletions packages-next/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
BaseGeneratedListTypes,
KeystoneConfig,
KeystoneContext,
AdminUIConfig,
} from '@keystone-next/types';
import { password, timestamp } from '@keystone-next/fields';

import { AuthConfig, Auth, AuthGqlNames } from './types';
import { AuthConfig, AuthGqlNames } from './types';
import { getSchemaExtension } from './schema';
import { signinTemplate } from './templates/signin';
import { initTemplate } from './templates/init';
Expand All @@ -24,7 +25,7 @@ export function createAuth<GeneratedListTypes extends BaseGeneratedListTypes>({
identityField,
magicAuthLink,
passwordResetLink,
}: AuthConfig<GeneratedListTypes>): Auth {
}: AuthConfig<GeneratedListTypes>) {
// The protectIdentities flag is currently under review to see whether it should be
// part of the createAuth API (in which case its use cases need to be documented and tested)
// or whether always being true is what we want, in which case we can refactor our code
Expand Down Expand Up @@ -86,7 +87,7 @@ 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 adminPageMiddleware: Auth['ui']['pageMiddleware'] = async ({
const adminPageMiddleware: AdminUIConfig['pageMiddleware'] = async ({
req,
isValidSession,
createContext,
Expand Down Expand Up @@ -133,7 +134,7 @@ export function createAuth<GeneratedListTypes extends BaseGeneratedListTypes>({
*
* The signin page is always included, and the init page is included when initFirstItem is set
*/
const additionalFiles: Auth['ui']['getAdditionalFiles'] = () => {
const additionalFiles = () => {
let filesToWrite: AdminFileToWrite[] = [
{
mode: 'write',
Expand Down Expand Up @@ -320,21 +321,19 @@ export function createAuth<GeneratedListTypes extends BaseGeneratedListTypes>({
};
};

/**
* Alongside withAuth (recommended) all the config is returned so you can extend or replace
* the default implementation with your own custom functionality, and integrate the result into
* your keystone config by hand.
*/
return {
ui: {
enableSessionItem: true,
pageMiddleware: adminPageMiddleware,
publicPages: publicAuthPages,
getAdditionalFiles: additionalFiles,
},
fields: additionalListFields,
extendGraphqlSchema,
validateConfig,
withAuth,
// In the future we may want to return the following so that developers can
// roll their own. This is pending a review of the use cases this might be
// appropriate for, along with documentation and testing.
// ui: {
// enableSessionItem: true,
// pageMiddleware: adminPageMiddleware,
// publicPages: publicAuthPages,
// getAdditionalFiles: additionalFiles,
// },
// fields: additionalListFields,
// extendGraphqlSchema,
// validateConfig,
};
}
20 changes: 1 addition & 19 deletions packages-next/auth/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
BaseGeneratedListTypes,
AdminUIConfig,
KeystoneConfig,
KeystoneContext,
} from '@keystone-next/types';
import { BaseGeneratedListTypes, KeystoneContext } from '@keystone-next/types';

export type AuthGqlNames = {
CreateInitialInput: string;
Expand Down Expand Up @@ -62,19 +57,6 @@ export type AuthConfig<GeneratedListTypes extends BaseGeneratedListTypes> = {
};
};

export type Auth = {
ui: {
enableSessionItem: NonNullable<AdminUIConfig['enableSessionItem']>;
publicPages: NonNullable<AdminUIConfig['publicPages']>;
pageMiddleware: NonNullable<AdminUIConfig['pageMiddleware']>;
getAdditionalFiles: NonNullable<AdminUIConfig['getAdditionalFiles']>[number];
};
extendGraphqlSchema: NonNullable<KeystoneConfig['extendGraphqlSchema']>;
fields: { [prop: string]: any };
validateConfig: (keystoneConfig: KeystoneConfig) => void;
withAuth: (config: KeystoneConfig) => KeystoneConfig;
};

export type AuthTokenRequestErrorCode = 'IDENTITY_NOT_FOUND' | 'MULTIPLE_IDENTITY_MATCHES';

export type PasswordAuthErrorCode =
Expand Down

1 comment on commit 03abbab

@vercel
Copy link

@vercel vercel bot commented on 03abbab Mar 3, 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.