-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace session-store-redis package with example (#7971)
- Loading branch information
Showing
23 changed files
with
545 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@keystone-6/core': major | ||
--- | ||
|
||
Removes `connect` and `disconnect` from `SessionStore` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@keystone-6/core': major | ||
--- | ||
|
||
Removes `disconnect` from `SessionStrategy` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## Feature Example - Redis Session Store | ||
|
||
This project demonstrates how to use create a custom session store with Redis. | ||
It builds on the [Authentication](../with-auth) example. | ||
|
||
## Instructions | ||
|
||
To run this project, clone the Keystone repository locally, run `yarn` at the root of the repository then navigate to this directory and run: | ||
|
||
```shell | ||
yarn dev | ||
``` | ||
|
||
This will start the Admin UI at [localhost:3000](http://localhost:3000). | ||
You can use the Admin UI to create items in your database. | ||
|
||
You can also access a GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql), which allows you to directly run GraphQL queries and mutations. | ||
|
||
## Try it out in CodeSandbox 🧪 | ||
|
||
You can play with this example online in a web browser using the free [codesandbox.io](https://codesandbox.io/) service. To launch this example, open the URL <https://githubbox.com/keystonejs/keystone/tree/main/examples/with-auth>. You can also fork this sandbox to make your own changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { config } from '@keystone-6/core'; | ||
import { storedSessions } from '@keystone-6/core/session'; | ||
import { createAuth } from '@keystone-6/auth'; | ||
import { createClient } from '@redis/client'; | ||
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'], | ||
}, | ||
}); | ||
|
||
const redis = createClient(); | ||
|
||
const session = storedSessions({ | ||
store: ({ maxAge }) => ({ | ||
async get(key) { | ||
let result = await redis.get(key); | ||
if (typeof result === 'string') { | ||
return JSON.parse(result); | ||
} | ||
}, | ||
async set(key, value) { | ||
await redis.setEx(key, maxAge, JSON.stringify(value)); | ||
}, | ||
async delete(key) { | ||
await redis.del(key); | ||
}, | ||
}), | ||
// 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', | ||
async onConnect() { | ||
await redis.connect(); | ||
}, | ||
}, | ||
lists, | ||
// We add our session configuration to the system here. | ||
session, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@keystone-6/redis-session-store-example", | ||
"version": "0.0.5", | ||
"private": true, | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "keystone dev", | ||
"start": "keystone start", | ||
"build": "keystone build" | ||
}, | ||
"dependencies": { | ||
"@keystone-6/auth": "^4.0.1", | ||
"@keystone-6/core": "^2.2.0", | ||
"@redis/client": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "~4.7.4" | ||
}, | ||
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/redis-session-store" | ||
} |
Oops, something went wrong.
a8a5f1f
There was a problem hiding this comment.
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-next-docs – ./
keystone-6.vercel.app
keystone-next-docs-thinkmill.vercel.app
keystone-next-docs-git-main-thinkmill.vercel.app
keystonejs.com
next.keystonejs.com
demo.keystonejs.com
www.keystonejs.com