hoangvvo / next-session

Simple promise-based session middleware for Next.js, micro, Express, and more
https://www.npmjs.com/package/next-session
MIT License
352 stars 24 forks source link

BREAKING: Add function to retrieve session from next cookies #375

Closed SleeplessOne1917 closed 1 year ago

SleeplessOne1917 commented 1 year ago

With Next 13, you can no longer rely on req and res being available if you're making a server component in the experimental app directory. To support this, I changed the session function to return an object with 2 functions instead of just a function. getSession is the function that it originally returned which accepts the request and response objects. The other, getSessionFromCookies, is made with Next 13's cookies function in mind. I expect the next getSessionFromCookies function to be used in a similar way in server components:

import { getSessionFromCookies } from "./lib/session";
import { cookies } from "next/headers";

const ServerComponent = () => {
    const session = getSessionFromCookies(cookies());
    // do stuff with session
   return <div></div>;
};

It should be noted that the session retrieved this way will be "readonly" in the sense that changes to it cannot be committed to the store.

changeset-bot[bot] commented 1 year ago

⚠️ No Changeset found

Latest commit: 93af093cba76cc153b0e2045bfba74d17798633c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

codecov[bot] commented 1 year ago

Codecov Report

Merging #375 (93af093) into master (5615e7d) will increase coverage by 0.03%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #375      +/-   ##
==========================================
+ Coverage   99.17%   99.20%   +0.03%     
==========================================
  Files           6        6              
  Lines         121      126       +5     
  Branches       37       41       +4     
==========================================
+ Hits          120      125       +5     
  Misses          1        1              
Impacted Files Coverage Δ
src/types.ts 0.00% <ø> (ø)
src/session.ts 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more