Skip to Page NavigationSkip to Page NavigationSkip to Content

Lesson 4: Auth & Sessions

Learn how to add passwords, session data and authentication to your Keystone app.

Where we left off

In the last lesson we setup a publishing workflow for blog posts and ended up with a keystone.js file that looks like:

//keystone.ts
import { list, config } from '@keystone-6/core'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { text, timestamp, select, relationship } from '@keystone-6/core/fields'
const lists = {
User: list({
fields: {
name: text({ validation: { isRequired: true } }),
email: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
posts: relationship({ ref: 'Post.author', many: true }),
},
}),
Post: list({
fields: {
title: text(),
publishedAt: timestamp(),
status: select({
options: [
{ label: 'Published', value: 'published' },
{ label: 'Draft', value: 'draft' },
],
defaultValue: 'draft',
ui: { displayMode: 'segmented-control' },
}),
author: relationship({ ref: 'User.posts' }),
},
}),
}
export default config({
db: {
provider: 'sqlite',
prismaClientOptions: () => ({
adapter: new PrismaBetterSqlite3({ url: 'file:./keystone.db' }),
}),
},
lists,
})

We're now going to add auth to our app so that different types of users have access to different types of things. While Keystone has very granular permissions controls, which you can read about here, this lesson will stay focused on securing our Admin UI behind a password.

Add the Password field

Keystone's password field adheres to typical password security recommendations like hashing the password in the database, and masking the password for Admin UI input fields.

The password field and authentication helpers are provided by Keystone’s auth package. Install it before adding the field:

npm install @keystone-6/auth

Let's add a password field to our User list so users can authenticate with Keystone:

import { list, config } from '@keystone-6/core';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import { text, timestamp, select, relationship } from '@keystone-6/core/fields';
import { password } from '@keystone-6/auth';
const lists = {
User: list({
fields: {
name: text({ validation: { isRequired: true } }),
email: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
posts: relationship({ ref: 'Post.author', many: true }),
password: password({ validation: { isRequired: true } })
},
}),
};

That's all we need to store secure passwords in our database!

Add Authentication

Create a new file in the root of our project to write our auth config in:

touch auth.ts

And add the following code:

// auth.ts
import { createAuth, jwtSessions } from '@keystone-6/auth'
// When deploying generate a random 32-byte secret once and
// reuse it across instances and restarts so existing sessions stay valid.
const sessionSecret = crypto.getRandomValues(new Uint8Array(32))
const sessionMaxAge = 60 * 60 * 24 // 24 hours
const sessionStrategy = jwtSessions({
secret: sessionSecret,
maxAge: sessionMaxAge,
})
const { withAuth } = createAuth({
listKey: 'User',
identityField: 'email',
passwordField: 'password',
sessionStrategy,
getAuthenticatedItemId: context => context.session?.user.id,
})
export { withAuth, sessionStrategy }

This code says:

  • The User list is the list that auth should be applied to
  • email and password will be the fields used to log a user in
  • sessionStrategy keeps the user signed in between refreshes with a signed cookie. sessionSecret is generated at startup, and sessionMaxAge sets the session lifetime to 24 hours.

Import Auth & Sessions to Keystone config

Back over in our keystone file, we want to import our withAuth function and sessionStrategy.

withAuth will wrap our default export and modify it as a last step in setting up our config. getSession calls the strategy and hydrates the signed-in user.

Finally, we need to add an isAccessAllowed function to our export so that only users with a valid session can see Admin UI:

//keystone.ts
import { list, config } from '@keystone-6/core';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import { text, timestamp, select, relationship } from '@keystone-6/core/fields';
import { password } from '@keystone-6/auth';
import { withAuth, sessionStrategy } from './auth';
const lists = {
};
export default config(
withAuth({
db: {
provider: 'sqlite',
prismaClientOptions: () => ({
adapter: new PrismaBetterSqlite3({ url: 'file:./keystone.db' }),
}),
},
lists,
async getSession({ context }) {
const data = await sessionStrategy.get({ context });
if (!data) return;
const user = await context.sudo().db.User.findOne({ where: { id: data.sub } });
return user ? { user } : undefined;
},
ui: {
isAccessAllowed: (context) => !!context.session?.user,
},
})
);

Adding an initial user

With our new set-up, we'll be locked out of Admin UI if the database has no users. We can use db.onConnect to create an initial user with a random password and log the credentials when Keystone starts.

The creation runs in the background so it does not delay startup. This is convenient for development; production systems should use a secure provisioning process instead.

What we have now

// auth.ts
import { createAuth, jwtSessions } from '@keystone-6/auth'
// When deploying generate a random 32-byte secret once and
// reuse it across instances and restarts so existing sessions stay valid.
const sessionSecret = crypto.getRandomValues(new Uint8Array(32))
const sessionMaxAge = 60 * 60 * 24 // 24 hours
const sessionStrategy = jwtSessions({
secret: sessionSecret,
maxAge: sessionMaxAge,
})
const { withAuth } = createAuth({
listKey: 'User',
identityField: 'email',
passwordField: 'password',
sessionStrategy,
getAuthenticatedItemId: context => context.session?.user.id,
})
export { withAuth, sessionStrategy }
//keystone.ts
import { list, config } from '@keystone-6/core'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { text, timestamp, select, relationship } from '@keystone-6/core/fields'
import { password } from '@keystone-6/auth'
import { withAuth, sessionStrategy } from './auth'
const lists = {
User: list({
fields: {
name: text({ validation: { isRequired: true } }),
email: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
posts: relationship({ ref: 'Post.author', many: true }),
password: password({ validation: { isRequired: true } }),
},
}),
Post: list({
fields: {
title: text(),
publishedAt: timestamp(),
status: select({
options: [
{ label: 'Published', value: 'published' },
{ label: 'Draft', value: 'draft' },
],
defaultValue: 'draft',
ui: { displayMode: 'segmented-control' },
}),
author: relationship({ ref: 'User.posts' }),
},
}),
}
export default config(
withAuth({
db: {
provider: 'sqlite',
prismaClientOptions: () => ({
adapter: new PrismaBetterSqlite3({ url: 'file:./keystone.db' }),
}),
async onConnect(context) {
// this creates an initial user if none exist so you can log in for development
// WARNING: do not use this in production
;(async () => {
const sudoContext = context.sudo()
if ((await sudoContext.db.User.count()) !== 0) return
const password = crypto.getRandomValues(new Uint8Array(16)).toHex()
await sudoContext.db.User.createOne({
data: { name: 'admin', email: 'admin@example.com', password },
})
console.log(`Created initial user: admin@example.com / ${password}`)
})().catch(error => console.error('Failed to create initial user:', error))
},
},
lists,
async getSession({ context }) {
const data = await sessionStrategy.get({ context })
if (!data) return
const user = await context.sudo().db.User.findOne({ where: { id: data.sub } })
return user ? { user } : undefined
},
ui: {
isAccessAllowed: context => !!context.session?.user,
},
})
)

Next lesson