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

feat(react): make session requireable in useSession #2236

Merged
merged 26 commits into from
Jul 5, 2021

Conversation

balazsorban44
Copy link
Member

@balazsorban44 balazsorban44 commented Jun 23, 2021

Reasoning 💡

A living session could be a requirement for specific pages (like dashboards). If it doesn’t exist, then the user should be redirected to a page that asks them to sign in again.

Sometimes, a user might log out by accident, or by deleting cookies on purpose. If that happens (e.g. on a separate tab), then useSession({ required: true }) should detect the absence of a session cookie and always return a non-nullable Session object type.

When required: true is set, the default behavior will be to redirect the user to the sign-in page. This can be overridden by an action() callback:

const session = useSession({
  required: true,
  action() {
    // ....
  }
})
if (session.status === "Loading") return "Loading or not authenticated..."

// session.data is always defined here.

Checklist 🧢

  • Documentation
  • Tests
  • Ready to be merged

Affected issues 🎟

Fixes #1210
Related: #2269

BREAKING CHANGE:

The useSession hook now returns an object. Here is how to accommodate for this change:

- const [ session, loading ] = useSession()
+ const { data: session, loading } = useSession()

@vercel
Copy link

vercel bot commented Jun 23, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nextauthjs/next-auth/EuDgeRfzdP4vdMCRFMRuL9VomnnV
✅ Preview: https://next-auth-git-feat-use-session-required-nextauthjs.vercel.app

@github-actions github-actions bot added client Client related code core Refers to `@auth/core` test Related to testing TypeScript Issues relating to TypeScript labels Jun 23, 2021
@vercel vercel bot temporarily deployed to Preview June 26, 2021 13:07 Inactive
@vercel vercel bot temporarily deployed to Preview June 26, 2021 13:23 Inactive
@vercel vercel bot temporarily deployed to Preview June 26, 2021 13:37 Inactive
@codecov-commenter
Copy link

codecov-commenter commented Jun 26, 2021

Codecov Report

Merging #2236 (40f9b25) into next (53e5e37) will increase coverage by 0.79%.
The diff coverage is 80.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            next    #2236      +/-   ##
=========================================
+ Coverage   9.94%   10.74%   +0.79%     
=========================================
  Files         83       83              
  Lines       1397     1414      +17     
  Branches     383      393      +10     
=========================================
+ Hits         139      152      +13     
- Misses      1039     1042       +3     
- Partials     219      220       +1     
Impacted Files Coverage Δ
src/server/index.js 0.00% <ø> (ø)
src/server/pages/signin.js 0.00% <ø> (ø)
src/client/react.js 84.27% <80.00%> (-0.94%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 53e5e37...40f9b25. Read the comment docs.

@vercel vercel bot temporarily deployed to Preview June 29, 2021 20:56 Inactive
@balazsorban44 balazsorban44 temporarily deployed to release June 29, 2021 20:58 Inactive
@vercel vercel bot temporarily deployed to Preview June 29, 2021 21:42 Inactive
@github-actions github-actions bot added the hacktoberfest-docs Relates to documentation label Jun 29, 2021
@balazsorban44 balazsorban44 temporarily deployed to release June 29, 2021 21:43 Inactive
Co-authored-by: Kristóf Poduszló <kripod@protonmail.com>
@vercel vercel bot temporarily deployed to Preview July 1, 2021 11:47 Inactive
@balazsorban44 balazsorban44 temporarily deployed to release July 1, 2021 11:48 Inactive
Copy link
Collaborator

@ubbe-xyz ubbe-xyz left a comment

Choose a reason for hiding this comment

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

Wow, very neat 🎸 💯 ✨

I'm leaving quite a lot of suggestions 💬 , in an effort to further polish the API, but I consider it's already very nice 💚

Let's have a call to go through it together 👍🏽

src/client/__tests__/client-provider.test.js Outdated Show resolved Hide resolved
src/client/__tests__/client-provider.test.js Show resolved Hide resolved
src/client/react.js Show resolved Hide resolved
src/client/react.js Show resolved Hide resolved
src/client/react.js Outdated Show resolved Hide resolved
types/internals/react.d.ts Outdated Show resolved Hide resolved
types/tests/react.test.ts Show resolved Hide resolved
www/docs/getting-started/client.md Show resolved Hide resolved
www/docs/getting-started/example.md Outdated Show resolved Hide resolved
www/docs/tutorials/securing-pages-and-api-routes.md Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview July 2, 2021 11:12 Inactive
@balazsorban44 balazsorban44 temporarily deployed to Preview July 2, 2021 11:13 Inactive
Co-authored-by: Lluis Agusti <hi@llu.lu>
@vercel vercel bot temporarily deployed to Preview July 2, 2021 19:21 Inactive
Copy link
Member

@ndom91 ndom91 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@balazsorban44
Copy link
Member Author

@lluia had some requests which first need to be reviewed again, when this PR is updated. 😊

Remove `loading` value from `useSession` in V4 given we already return a `status`value which scales better to identify the different states of the session request.
@vercel vercel bot temporarily deployed to Preview July 4, 2021 19:43 Inactive
• rename `action` to `onFail`
• reword documentation
• document `useSession` return
• reword `<SessionProvider />` usage to state is mandatory
@vercel vercel bot temporarily deployed to Preview July 4, 2021 20:14 Inactive
@ubbe-xyz ubbe-xyz temporarily deployed to Preview July 4, 2021 20:16 Inactive
Rename the handler name on `useSession` when authentication didn't pass to be more accurate.
@vercel vercel bot temporarily deployed to Preview July 5, 2021 09:48 Inactive
@ubbe-xyz ubbe-xyz temporarily deployed to Preview July 5, 2021 09:49 Inactive
@vercel vercel bot temporarily deployed to Preview July 5, 2021 14:01 Inactive
@balazsorban44 balazsorban44 temporarily deployed to Preview July 5, 2021 14:03 Inactive
@balazsorban44 balazsorban44 merged commit a2e5afa into next Jul 5, 2021
@balazsorban44 balazsorban44 deleted the feat/use-session-required branch July 5, 2021 14:03
mnphpexpert added a commit to mnphpexpert/next-auth that referenced this pull request Sep 2, 2024
A living session could be a requirement for specific pages (like dashboards). If it doesn’t exist, the user should be redirected to a page asking them to sign in again.

Sometimes, a user might log out by accident, or by deleting cookies on purpose. If that happens (e.g. on a separate tab), then `useSession({ required: true })` should detect the absence of a session cookie and always return a non-nullable Session object type.

When `required: true` is set, the default behavior will be to redirect the user to the sign-in page. This can be overridden by an `action()` callback:

```js
const session = useSession({
  required: true,
  action() {
    // ....
  }
})
if (session.status === "Loading") return "Loading or not authenticated..."

// session.data is always defined here.
```

Co-authored-by: Kristóf Poduszló <kripod@protonmail.com>
Co-authored-by: Lluis Agusti <hi@llu.lu>

BREAKING CHANGE:

The `useSession` hook now returns an object. Here is how to accommodate for this change:

```diff
- const [ session, loading ] = useSession()
+ const { data: session, status } = useSession()
+ const loading = status === "loading"
```

With the new `status` option, you can test states much more clearly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Client related code core Refers to `@auth/core` hacktoberfest-docs Relates to documentation pages test Related to testing TypeScript Issues relating to TypeScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants