Skip to content

Commit

Permalink
[WIP] withAuth example
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed May 5, 2021
1 parent cf59312 commit 582f22e
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-stingrays-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/example-next-auth': major
---

Initial version of the `withAuth` example.
1 change: 1 addition & 0 deletions examples/with-auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @keystone-next/example-with-auth
47 changes: 47 additions & 0 deletions examples/with-auth/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { config } from '@keystone-next/keystone/schema';
import { statelessSessions } from '@keystone-next/keystone/session';
import { createAuth } from '@keystone-next/auth';
import { lists } from './schema';

// createAuth configures signin functionality based on the config below. Note this only implements
// authentication, i.e signing in as an item using identity and secret fields in a list. Session
// management and access control are controlled independently in the main keystone config.
const { withAuth } = createAuth({
// This is the list that contains items people can sign in as
listKey: 'Person',
// The identity field is typically a username or email address
identityField: 'email',
// The secret field must be a password type field
secretField: 'password',

// initFirstItem turns on the "First User" experience, which prompts you to create a new user
// when there are no items in the list yet
initFirstItem: {
// These fields are collected in the "Create First User" form
fields: ['name', 'email', 'password'],
},
});

// Stateless sessions will store the listKey and itemId of the signed-in user in a cookie.
// This session object will be made availble on the context object used in hooks, access-control,
// resolvers, etc.
const session = statelessSessions({
// The maxAge option controls how long session cookies are valid for before they expire
maxAge: 60 * 60 * 24 * 30, // 30 days
// The session secret is used to encrypt cookie data (should be an environment variable)
secret: '-- EXAMPLE COOKIE SECRET; CHANGE ME --',
});

// We wrap our config using the withAuth function. This will inject all
// the extra config required to add support for authentication in our system.
export default withAuth(
config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
},
lists,
// We add our session configuration to the system here.
session,
})
);
23 changes: 23 additions & 0 deletions examples/with-auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@keystone-next/example-with-auth",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"dev": "keystone-next dev",
"start": "keystone-next start",
"build": "keystone-next build"
},
"dependencies": {
"@keystone-next/auth": "^23.0.0",
"@keystone-next/fields": "^8.0.0",
"@keystone-next/keystone": "^17.0.0"
},
"devDependencies": {
"typescript": "^4.2.4"
},
"engines": {
"node": ">=v12.13.1"
},
"repository": "https://github.com/keystonejs/keystone/tree/master/examples/with-auth"
}
Loading

0 comments on commit 582f22e

Please sign in to comment.