Skip to content

Commit

Permalink
remove authenticatedItem from @keystone-6/core/admin-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Aug 20, 2024
1 parent e74d573 commit 08b4ce2
Show file tree
Hide file tree
Showing 25 changed files with 157 additions and 101 deletions.
5 changes: 5 additions & 0 deletions .changeset/admin-ui-navigation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": major
---

Removes `authenticatedItem` from `@keystone-6/core/admin-ui/components` exports
5 changes: 5 additions & 0 deletions .changeset/remove-end-session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": major
---

Removes the `EndSession` GraphQL mutation addition when `context.session.end` is defined, extend this yourself if required
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { ListNavItems, NavigationContainer, NavItem } from '@keystone-6/core/adm

import type { NavigationProps } from '@keystone-6/core/admin-ui/components'

export function CustomNavigation ({ lists, authenticatedItem }: NavigationProps) {
export function CustomNavigation ({ lists }: NavigationProps) {
return (
<NavigationContainer authenticatedItem={authenticatedItem}>
<NavItem href="/">Dashboard</NavItem>
<NavigationContainer>
<NavItem href='/'>Dashboard</NavItem>
<ListNavItems lists={lists} />
<NavItem href="https://keystonejs.com">Keystone Docs</NavItem>
<NavItem href='https://keystonejs.com'>Keystone Docs</NavItem>
</NavigationContainer>
)
}
1 change: 0 additions & 1 deletion examples/custom-session-jwt/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ type Mutation {
updateUsers(data: [UserUpdateArgs!]!): [User]
deleteUser(where: UserWhereUniqueInput!): User
deleteUsers(where: [UserWhereUniqueInput!]!): [User]
endSession: Boolean!
}

type Query {
Expand Down
1 change: 0 additions & 1 deletion examples/custom-session-next-auth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ type Mutation {
updateAuthors(data: [AuthorUpdateArgs!]!): [Author]
deleteAuthor(where: AuthorWhereUniqueInput!): Author
deleteAuthors(where: [AuthorWhereUniqueInput!]!): [Author]
endSession: Boolean!
}

type Query {
Expand Down
1 change: 0 additions & 1 deletion examples/custom-session-passport/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ type Mutation {
updateAuthors(data: [AuthorUpdateArgs!]!): [Author]
deleteAuthor(where: AuthorWhereUniqueInput!): Author
deleteAuthors(where: [AuthorWhereUniqueInput!]!): [Author]
endSession: Boolean!
}

type Query {
Expand Down
1 change: 0 additions & 1 deletion examples/custom-session/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ type Mutation {
updateUsers(data: [UserUpdateArgs!]!): [User]
deleteUser(where: UserWhereUniqueInput!): User
deleteUsers(where: [UserWhereUniqueInput!]!): [User]
endSession: Boolean!
}

type Query {
Expand Down
1 change: 0 additions & 1 deletion examples/usecase-blog-moderated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ type Mutation {
updateUsers(data: [UserUpdateArgs!]!): [User]
deleteUser(where: UserWhereUniqueInput!): User
deleteUsers(where: [UserWhereUniqueInput!]!): [User]
endSession: Boolean!
}

type Query {
Expand Down
4 changes: 4 additions & 0 deletions packages/auth/components/Navigation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/keystone-6-auth-components-Navigation.cjs.js",
"module": "dist/keystone-6-auth-components-Navigation.esm.js"
}
5 changes: 5 additions & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"module": "./pages/SigninPage/dist/keystone-6-auth-pages-SigninPage.esm.js",
"default": "./pages/SigninPage/dist/keystone-6-auth-pages-SigninPage.cjs.js"
},
"./components/Navigation": {
"module": "./components/Navigation/dist/keystone-6-auth-components-Navigation.esm.js",
"default": "./components/Navigation/dist/keystone-6-auth-components-Navigation.cjs.js"
},
"./package.json": "./package.json"
},
"dependencies": {
Expand All @@ -41,6 +45,7 @@
"preconstruct": {
"entrypoints": [
"index.ts",
"components/Navigation.tsx",
"pages/*.tsx"
]
},
Expand Down
43 changes: 43 additions & 0 deletions packages/auth/src/components/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'

import {
ListNavItems,
NavigationContainer,
NavItem
} from '@keystone-6/core/admin-ui/components'
import type { NavigationProps } from '@keystone-6/core/admin-ui/components'
import SignoutButton from './SignoutButton'

function Footer ({ authItem }: { authItem: AuthenticatedItem | undefined }) {
return (
<HStack paddingX="large" gap="regular">
<AuthItem item={authItem} />
<DeveloperResources />
</HStack>
)
}

function AuthItem ({ item }: { item: AuthenticatedItem | undefined }) {
if (!item || item.state !== 'authenticated') {
return null
}

return (
<TooltipTrigger>
<SignoutButton flex />
<Tooltip>
<Text>Signed in as <strong>{item.label}</strong></Text>
</Tooltip>
</TooltipTrigger>
)
}

export default function Navigation ({ lists }: NavigationProps) {
return (
<NavigationContainer>
<NavItem href='/'>Dashboard</NavItem>
<ListNavItems lists={lists} />
<SignoutButton />
</NavigationContainer>
)
}
9 changes: 7 additions & 2 deletions packages/auth/src/components/SigninContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

import { type ReactNode } from 'react'

import { jsx, Box, Center, useTheme } from '@keystone-ui/core'
import {
jsx,
Box,
Center,
useTheme
} from '@keystone-ui/core'
import { Head } from '@keystone-6/core/admin-ui/router'

type SigninContainerProps = {
children: ReactNode
title?: string
}

export const SigninContainer = ({ children, title }: SigninContainerProps) => {
export function SigninContainer ({ children, title }: SigninContainerProps) {
const { colors, shadow } = useTheme()
return (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ActionButton, type ActionButtonProps } from '@keystar/ui/button'
import {
type ActionButtonProps
ActionButton,
} from '@keystar/ui/button'
import { useEffect } from 'react'

import { useMutation, gql } from '../apollo'
import { useMutation, gql } from '@keystone-6/core/admin-ui/apollo'

const END_SESSION = gql`
mutation EndSession {
Expand Down
7 changes: 7 additions & 0 deletions packages/auth/src/gql/getBaseAuthSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export function getBaseAuthSchema<I extends string, S extends string> ({
}),
},
mutation: {
endSession: graphql.field({
type: graphql.nonNull(graphql.Boolean),
async resolve (rootVal, args, context) {
await context.sessionStrategy?.end({ context })
return true
},
}),
[gqlNames.authenticateItemWithPassword]: graphql.field({
type: AuthenticationResult,
args: {
Expand Down
10 changes: 8 additions & 2 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { password, timestamp } from '@keystone-6/core/fields'

import type { AuthConfig, AuthGqlNames } from './types'
import { getSchemaExtension } from './schema'
import { signinTemplate } from './templates/signin'
import { initTemplate } from './templates/init'
import configTemplate from './templates/config'
import signinTemplate from './templates/signin'
import initTemplate from './templates/init'

export type AuthSession = {
listKey: string // TODO: use ListTypeInfo
Expand Down Expand Up @@ -106,6 +107,11 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo> ({
src: signinTemplate({ gqlNames, identityField, secretField }),
outputPath: 'pages/signin.js',
},
{
mode: 'write',
src: configTemplate(),
outputPath: 'config.ts',
},
]
if (initFirstItem) {
filesToWrite.push({
Expand Down
8 changes: 4 additions & 4 deletions packages/auth/src/pages/InitPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { jsx, H1, Stack, Inline, VisuallyHidden } from '@keystone-ui/core'
import { Button } from '@keystone-ui/button'
import { Checkbox, TextInput } from '@keystone-ui/fields'
import { type FieldMeta } from '@keystone-6/core/types'
import isDeepEqual from 'fast-deep-equal'

import { gql, useMutation } from '@keystone-6/core/admin-ui/apollo'
import { useReinitContext, useKeystone } from '@keystone-6/core/admin-ui/context'
import { useKeystone } from '@keystone-6/core/admin-ui/context'
import { useRouter, Link } from '@keystone-6/core/admin-ui/router'
import { GraphQLErrorNotice } from '@keystone-6/core/admin-ui/components'
import {
Fields,
serializeValueToObjByFieldKey,
itemValueToGraphQL,
getDefaultItemValue,
useInvalidFields,
} from '@keystone-6/core/admin-ui/utils'
import { guessEmailFromValue, validEmail } from '../lib/emailHeuristics'
Expand Down Expand Up @@ -88,7 +88,7 @@ function Welcome ({ value, onContinue }: { value: any, onContinue: () => void })
>
<H1>Welcome</H1>
<Stack across gap="small">
<IconTwitter
<IconTwitter
href="https://twitter.com/keystonejs"
target="_blank"
title="Twitter Logo"
Expand Down
8 changes: 7 additions & 1 deletion packages/auth/src/pages/SigninPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/** @jsxRuntime classic */
/** @jsx jsx */

import { useState, Fragment, type FormEvent, useRef, useEffect } from 'react'
import {
type FormEvent,
Fragment,
useEffect,
useRef,
useState,
} from 'react'

import { jsx, H1, Stack, VisuallyHidden } from '@keystone-ui/core'
import { Button } from '@keystone-ui/button'
Expand Down
11 changes: 11 additions & 0 deletions packages/auth/src/templates/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function () {
// -- TEMPLATE START
return `import { type AdminConfig } from '@keystone-6/core/types'
import Navigation from '@keystone-6/auth/components/Navigation'
export const components: AdminConfig['components'] = {
Navigation
}
`
// -- TEMPLATE END
}
21 changes: 10 additions & 11 deletions packages/auth/src/templates/init.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import type { BaseListTypeInfo } from '@keystone-6/core/types'
import type { AuthConfig } from '../types'

type InitTemplateArgs = {
export default function ({
listKey,
initFirstItem
}: {
listKey: string
initFirstItem: NonNullable<AuthConfig<BaseListTypeInfo>['initFirstItem']>
}

export const initTemplate = ({ listKey, initFirstItem }: InitTemplateArgs) => {
}) {
// -- TEMPLATE START
return `import { getInitPage } from '@keystone-6/auth/pages/InitPage';
const fieldPaths = ${JSON.stringify(initFirstItem.fields)};
return `import { getInitPage } from '@keystone-6/auth/pages/InitPage'
export default getInitPage(${JSON.stringify({
listKey,
fieldPaths: initFirstItem.fields,
enableWelcome: !initFirstItem.skipKeystoneWelcome,
})});
listKey,
fieldPaths: initFirstItem.fields,
enableWelcome: !initFirstItem.skipKeystoneWelcome,
})})
`
// -- TEMPLATE END
}
14 changes: 7 additions & 7 deletions packages/auth/src/templates/signin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type AuthGqlNames } from '../types'

export const signinTemplate = ({
export default ({
gqlNames,
identityField,
secretField,
Expand All @@ -13,12 +13,12 @@ export const signinTemplate = ({
return `import { getSigninPage } from '@keystone-6/auth/pages/SigninPage'
export default getSigninPage(${JSON.stringify({
identityField: identityField,
secretField: secretField,
mutationName: gqlNames.authenticateItemWithPassword,
successTypename: gqlNames.ItemAuthenticationWithPasswordSuccess,
failureTypename: gqlNames.ItemAuthenticationWithPasswordFailure,
})});
identityField: identityField,
secretField: secretField,
mutationName: gqlNames.authenticateItemWithPassword,
successTypename: gqlNames.ItemAuthenticationWithPasswordSuccess,
failureTypename: gqlNames.ItemAuthenticationWithPasswordFailure,
})})
`
// -- TEMPLATE END
}
Loading

0 comments on commit 08b4ce2

Please sign in to comment.