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

Add next-auth using a custom session strategy #8582

Merged
merged 21 commits into from
Jun 13, 2023

Conversation

borisno2
Copy link
Member

This new example shows what it might look like to implement next-auth with Keystone without the use of an external package. Given https://github.com/OpenSaasAU/keystone-nextjs-auth is no longer working with later versions of next-auth I wanted to see/get feedback on using next-auth directly instead of potentially rearchitecting the package for the upcoming next-auth@5 release.

@changeset-bot

This comment was marked as resolved.

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 19, 2023

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.

@borisno2 borisno2 force-pushed the custom-session-next-auth-example branch 2 times, most recently from 585b0b6 to afedf9d Compare May 19, 2023 08:53
@borisno2 borisno2 marked this pull request as ready for review May 21, 2023 22:18
@borisno2 borisno2 requested a review from dcousens May 21, 2023 22:18
@borisno2 borisno2 force-pushed the custom-session-next-auth-example branch from 42662fb to 6df3dd6 Compare May 24, 2023 23:19
@borisno2 borisno2 self-assigned this May 26, 2023
@borisno2 borisno2 force-pushed the custom-session-next-auth-example branch 2 times, most recently from 80f95ec to 82c4f2e Compare May 29, 2023 00:19
@dcousens dcousens force-pushed the custom-session-next-auth-example branch from c59e7e5 to 5e6c3ee Compare May 31, 2023 05:34
Copy link
Member

@dcousens dcousens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

I think we should show how you might use the session returned, for example with GitHub my session object was the following:

session: {
  user: {
    name: 'Daniel Cousens',
    email: undefined,
    image: 'https://avatars.githubusercontent.com/u/413395?v=4'
  },
  expires: '2023-06-30T00:12:34.567Z'
}

@gautamsi
Copy link
Member

gautamsi commented Jun 1, 2023

would it make sense to merge this session info with actual user in keystone and present compatible keystone session? This session is also useful and may represent fresh from provider without matching user in database then we can redirect them to complete signup page and either link account or complete signup, for example having user provide missing email.

@borisno2
Copy link
Member Author

borisno2 commented Jun 5, 2023

Thanks for the feedback @dcousens and @gautamsi
I have just pushed two commits:

  • The first (Add user email and id to session) adds the user's email and subject id to the next-auth session
  • The Second (Add User to DB on Signin) checks the database for the user and creates a new Author if they are not found, then adds the itemId to the Keystone session. (This is what @opensaasau/keystone-nextjs-auth currently does)

The reason I started with just a hasSessionis I didn't want to overcomplicate what the minimum requirement is to get next-auth working with Keystone. However maybe given how getContext needs to be imported, this might be useful in the example.

@borisno2 borisno2 force-pushed the custom-session-next-auth-example branch from 33f4ad6 to 3f293b0 Compare June 5, 2023 08:01
@borisno2 borisno2 requested a review from dcousens June 5, 2023 08:05
@borisno2 borisno2 force-pushed the custom-session-next-auth-example branch from 38abc43 to 685b03a Compare June 8, 2023 05:28
@dcousens dcousens force-pushed the custom-session-next-auth-example branch from 2d3527d to 15772f2 Compare June 8, 2023 07:51
@dcousens dcousens force-pushed the custom-session-next-auth-example branch from ab04361 to 023a316 Compare June 8, 2023 07:56
@dcousens dcousens merged commit 70c6c16 into main Jun 13, 2023
57 of 58 checks passed
@dcousens dcousens deleted the custom-session-next-auth-example branch June 13, 2023 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants