Skip to content

Commit

Permalink
Merge pull request #94 from ijsto/SA/V3/Final-patches-pt1-+-Typings
Browse files Browse the repository at this point in the history
SA/v3/final patches pt1 + typings
  • Loading branch information
ScottAgirs authored Nov 15, 2022
2 parents d614908 + 127b601 commit 352976c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 86 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-bikes-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'keystone-6-oauth': patch
---

Types - wip - optimize
25 changes: 17 additions & 8 deletions packages/keystone-6-oauth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import url from 'url';
import {
AdminFileToWrite,
BaseListTypeInfo,
Expand All @@ -8,27 +7,37 @@ import {
BaseKeystoneTypeInfo,
SessionStrategy,
} from '@keystone-6/core/types';
import { getSession } from 'next-auth/react';
import { getToken, JWT } from 'next-auth/jwt';
import { Provider } from 'next-auth/providers';

import * as cookie from 'cookie';

import { Session } from 'next-auth';
import { getSession } from 'next-auth/react';
import { getToken, JWT } from 'next-auth/jwt';
import { Provider } from 'next-auth/providers';

import { nextConfigTemplate } from './templates/next-config';
// import * as Path from 'path';
import { authTemplate } from './templates/auth';

import { AuthConfig, KeystoneOAuthConfig, AuthSessionStrategy } from './types';
import { getSchemaExtension } from './schema';
import { authTemplate } from './templates/auth';

import { AuthConfig, KeystoneOAuthConfig, AuthSessionStrategy } from './types';

import url from 'url';

/**
* createAuth function
*
* Generates config for Keystone to implement standard auth features.
*/

export type { NextAuthProviders, KeystoneOAuthConfig } from './types';
export type {
NextAuthProviders,
KeystoneOAuthConfig,
KeystoneOAuthCallbacks,
KeystoneOAuthOnSignIn,
KeystoneOAuthOnSignUp
} from './types';

export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
autoCreate,
context,
Expand Down
31 changes: 5 additions & 26 deletions packages/keystone-6-oauth/src/pages/NextAuthPage.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
/* eslint-disable no-param-reassign */
import NextAuth, {
CookiesOptions,
EventCallbacks,
PagesOptions,
} from 'next-auth';
import type { KeystoneContext } from '@keystone-6/core/types';
import { Provider } from 'next-auth/providers';
import { JWTOptions } from 'next-auth/jwt';
import { validateNextAuth } from '../lib/validateNextAuth';

import { NextAuthTemplateProps, OAuthCallbacks } from '../types';
import NextAuth from 'next-auth';

export type CoreNextAuthPageProps = {
cookies?: Partial<CookiesOptions>;
events?: Partial<EventCallbacks>;
jwt?: Partial<JWTOptions>;
pages?: Partial<PagesOptions>;
providers: Provider[];
} & NextAuthTemplateProps & OAuthCallbacks;

export type NextAuthPageProps = CoreNextAuthPageProps & {
context: KeystoneContext;
};
import { validateNextAuth } from '../lib/validateNextAuth';
import { KeystoneOAuth } from '../types';

// eslint-disable-next-line react/function-component-definition
export default function NextAuthPage(props: NextAuthPageProps) {
export default function NextAuthPage(props: KeystoneOAuth) {
const {
autoCreate,
cookies,
Expand Down Expand Up @@ -180,5 +159,5 @@ export default function NextAuthPage(props: NextAuthPageProps) {
});
}

export const getNextAuthPage = (props: NextAuthPageProps) => () =>
export const getNextAuthPage = (props: KeystoneOAuth) => () =>
NextAuthPage({ ...props });
93 changes: 41 additions & 52 deletions packages/keystone-6-oauth/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,71 @@
import type { ServerResponse, IncomingMessage } from 'http';
// TODO: Review this type - why "Cannot find module 'next/server'"?
// @ts-ignore

import type {
CookiesOptions,
EventCallbacks,
PagesOptions,
} from 'next-auth';
import type { Provider } from 'next-auth/providers';
import type { JWTOptions } from 'next-auth/jwt';

import type { NextRequest } from 'next/server';
import { Provider } from 'next-auth/providers';
import { CookiesOptions, PagesOptions } from 'next-auth';
import {

import type {
BaseListTypeInfo,
KeystoneConfig,
CreateContext,
KeystoneContext,
} from '@keystone-6/core/types';

export type OAuthCallbacks = {
// TODO: Review definition of this type
// eslint-disable-next-line no-unused-vars
onSignIn?: (args: {
account: any;
profile: any;
context: KeystoneContext;
user: any;
}) => Promise<void>;
// TODO: Review definition of this type
// eslint-disable-next-line no-unused-vars
onSignUp?: (args: {
account: any;
created?: any;
profile: any;
context: KeystoneContext;
user: any;
}) => Promise<void>;
type NextAuthResponse = IncomingMessage & NextRequest;

export type KeystoneOAuthOnSignIn = {
account: any;
profile: any;
context: KeystoneContext;
user: any;
};
export type KeystoneOAuthOnSignUp = {
account: any;
created?: any;
profile: any;
context: KeystoneContext;
user: any;
};

type NextAuthResponse = IncomingMessage & NextRequest;
export type KeystoneOAuthCallbacks = {
onSignIn?: (args: KeystoneOAuthOnSignIn) => Promise<boolean>;
onSignUp?: (args: KeystoneOAuthOnSignUp) => Promise<boolean>;
};

export type NextAuthTemplateProps = {
autoCreate: boolean;
context?: KeystoneContext;
identityField: string;
listKey: string;
sessionData: string | undefined;
sessionSecret: string;
};

export type OAuthCallbacks = {
// TODO: Review definition of this type
// eslint-disable-next-line no-unused-vars
onSignIn?: (args: {
account: any;
profile: any;
context: KeystoneContext;
user: any;
}) => Promise<void>;
// TODO: Review definition of this type
// eslint-disable-next-line no-unused-vars
onSignUp?: (args: {
account: any;
created?: any;
profile: any;
context: KeystoneContext;
user: any;
}) => Promise<void>;
};
export type KeystoneOAuth = {
cookies?: Partial<CookiesOptions>;
events?: Partial<EventCallbacks>;
jwt?: Partial<JWTOptions>;
pages?: Partial<PagesOptions>;
providers: Provider[];
} & KeystoneOAuthCallbacks & NextAuthTemplateProps;

export declare type AuthSessionStrategy<StoredSessionData> = {
// TODO: Review definition
// eslint-disable-next-line no-unused-vars
start: (args: {
res: ServerResponse;
data: any;
createContext: CreateContext;
}) => Promise<string>;
// TODO: Review definition
// eslint-disable-next-line no-unused-vars
end: (args: {
req: IncomingMessage;
res: ServerResponse;
createContext: CreateContext;
}) => Promise<void>;
// TODO: Review definition
// eslint-disable-next-line no-unused-vars
get: (args: {
req: NextAuthResponse;
createContext: CreateContext;
Expand All @@ -88,15 +76,16 @@ export type NextAuthProviders = Provider[];

type KeystoneOAuthOptions = {
/** KeystoneContext */
context: KeystoneContext;
context?: KeystoneContext;
// Custom pages for different NextAuth events
pages?: Partial<PagesOptions>;
/** Providers for Next Auth */
providers: NextAuthProviders;
};

type NextAuthOptions = {
cookies?: Partial<CookiesOptions>;
} & OAuthCallbacks;
} & KeystoneOAuthCallbacks;

export type KeystoneOAuthConfig = KeystoneConfig &
KeystoneOAuthOptions &
Expand All @@ -117,7 +106,7 @@ export type AuthConfig<GeneratedListTypes extends BaseListTypeInfo> = {
sessionData?: string | undefined;
/** Next-Auth Session Secret */
sessionSecret: string;
} & OAuthCallbacks & KeystoneOAuthOptions;
} & KeystoneOAuthCallbacks & KeystoneOAuthOptions;

export type AuthTokenRequestErrorCode =
| 'IDENTITY_NOT_FOUND'
Expand Down

1 comment on commit 352976c

@vercel
Copy link

@vercel vercel bot commented on 352976c Nov 15, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

keystone-plugins-docs – ./

keystone-oauth.vercel.app
keystone-plugins-docs-git-main-ijsto.vercel.app
keystone-plugins-docs-ijsto.vercel.app

Please sign in to comment.