Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customisation of the session object through new getSession config #8097

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

borisno2
Copy link
Member

@borisno2 borisno2 commented Nov 17, 2022

This PR continues the context and session work from #8013

The goal of this PR to simplify how keystone handles different session implementations. With that in mind This PR does the following:

Moves statelessSessions and storedSessions functions into the auth package.

These Session Strategies are fairly simple cookie-based session management which do the following

  • start - starts a session by setting a cookie on the server res and in the case of storedSessions add the session data to the store.
  • get - decodes the data from the cookie in req returns data for context.session and
  • end - sends a blank cookie back in the server res
    These functions are based around keystone handling the start and end of a session, however, this is not always the case - for example, SSO or other token-based auth.

By moving this more prescriptive session setup into auth the aim is to allow more complex auth and session management strategies to be implemented

This is a BREAKING change to config and means the following:

  • Change location of import for sessionStrategy
import { statelessSessions } from '@keystone-6/core/session';

becomes:

import { statelessSessions } from '@keystone-6/auth/session';
  • Move session config from main config into createAuth sessionStrategy config and move sessionData into sessionStrategy config - this means the following:
const { withAuth } = createAuth({
 /* ... Existing Auth Config */
sessionData: 'name isAdmin',
});

export default withAuth(
  config({
 /* ... Existing Keystone Config */
    session: statelessSessions({
         maxAge: sessionMaxAge,
         secret: sessionSecret,
       }),
   })
 );
});

Becomes:

const { withAuth } = createAuth({
 /* ... Existing Auth Config */
    sessionStrategy: statelessSessions({
         maxAge: sessionMaxAge,
         secret: sessionSecret,
         data: 'name isAdmin',
       }),
});

export default withAuth(
  config({
 /* ... Existing Keystone Config */
   })
 );
});

Adds new getSession object to config - removing current session and sessionStrategy

With the current session and sessionStrategy implementation moved to auth, Keystone still needs a way to populate the session object on the context. In order to facilitate this a new getSession option is added to config this passes the current context - which if withRequest has been used will contain the current req and res - and accepts a function that returns a valid session, or undefined if the session is not valid. The return of getSession is added to the context as session.

This should allow for easier configuration of more complex session strategies.

@vercel
Copy link

vercel bot commented Nov 17, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated
keystone-next-docs ⬜️ Ignored (Inspect) Visit Preview Mar 2, 2023 at 8:27AM (UTC)

@changeset-bot

This comment was marked as resolved.

@vercel vercel bot temporarily deployed to Preview November 17, 2022 04:22 Inactive
@vercel vercel bot temporarily deployed to Preview November 17, 2022 04:26 Inactive
@vercel vercel bot temporarily deployed to Preview November 17, 2022 04:34 Inactive
@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 17, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a8bfac8:

Sandbox Source
@keystone-6/sandbox Configuration

@vercel vercel bot temporarily deployed to Preview November 17, 2022 06:07 Inactive
@vercel vercel bot temporarily deployed to Preview November 20, 2022 21:44 Inactive
@borisno2 borisno2 self-assigned this Nov 20, 2022
@vercel vercel bot temporarily deployed to Preview November 21, 2022 03:32 Inactive
@dcousens dcousens force-pushed the get-session branch 2 times, most recently from c80622a to de0905c Compare November 22, 2022 07:25
@vercel vercel bot temporarily deployed to Preview November 22, 2022 07:35 Inactive
@vercel vercel bot temporarily deployed to Preview November 23, 2022 23:09 Inactive
@vercel vercel bot temporarily deployed to Preview November 23, 2022 23:46 Inactive
@vercel vercel bot temporarily deployed to Preview November 24, 2022 05:28 Inactive
@vercel vercel bot temporarily deployed to Preview November 27, 2022 21:57 Inactive
@vercel vercel bot temporarily deployed to Preview November 27, 2022 22:29 Inactive
@vercel vercel bot temporarily deployed to Preview November 27, 2022 22:35 Inactive
@vercel vercel bot temporarily deployed to Preview November 29, 2022 03:59 Inactive
@vercel vercel bot temporarily deployed to Preview November 29, 2022 04:30 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants