Skip to content

Commit

Permalink
Replace express-session with next-session
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Sep 19, 2021
1 parent 5310c52 commit 230b2a9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
WEB_URI=http://localhost:3000
MONGODB_URI=mongodb+srv://testuser:[email protected]/nextjsmongodbapp
SESSION_SECRET=keyboard cat
CLOUDINARY_URL=cloudinary://741947492169653:vkyuRmZ3EbSULnkfXJdtSqwhURw@dbplcha6k
NODEMAILER_CONFIG={"service":"Yandex","auth":{"user":"[email protected]","pass":"cfpnbpilyraudeaa"}}
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div align="center">

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fhoangvvo%2Fnextjs-mongodb-app&env=MONGODB_URI,CLOUDINARY_URL,NODEMAILER_CONFIG,WEB_URI,SESSION_SECRET&envDescription=Environment%20Variables&envLink=https%3A%2F%2Fgithub.com%2Fhoangvvo%2Fnextjs-mongodb-app%23environmental-variables&demo-title=nextjs-mongodb-app%20demo&demo-description=A%20demo%20deployed%20on%20Vercel&demo-url=https%3A%2F%2Fnextjs-mongodb.vercel.app%2F)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fhoangvvo%2Fnextjs-mongodb-app&env=MONGODB_URI,CLOUDINARY_URL,NODEMAILER_CONFIG,WEB_URI&envDescription=Environment%20Variables&envLink=https%3A%2F%2Fgithub.com%2Fhoangvvo%2Fnextjs-mongodb-app%23environmental-variables&demo-title=nextjs-mongodb-app%20demo&demo-description=A%20demo%20deployed%20on%20Vercel&demo-url=https%3A%2F%2Fnextjs-mongodb.vercel.app%2F)

An [**Next.js**](https://github.com/zeit/next.js/) and [**MongoDB**](https://www.mongodb.com/) web application, designed with simplicity for learning and real-world applicability in mind.

Expand Down Expand Up @@ -90,7 +90,7 @@ This project uses the following dependencies:
- `mongodb` - may be replaced by `mongoose`.
- `passport`, `passport-local` - required after [#39](https://github.com/hoangvvo/nextjs-mongodb-app/pull/39) for authentication
- `next-connect` - recommended if you want to use Express/Connect middleware and easier method routing.
- `express-session`, `connect-mongo` - required for session, may be replaced with other session libraries such as `cookie-session` or `next-iron-session`.
- `next-session`, `connect-mongo` - required for session, may be replaced with other session libraries such as `cookie-session`, `next-iron-session`, or `express-session` (`express-session` is observed not to work properly on Next.js 11+).
- `bcryptjs` - optional, may be replaced with any password-hashing library. `argon2` recommended.
- `validator` - optional but recommended.
- `multer` - may be replaced with any middleware that handles `multipart/form-data`
Expand All @@ -102,7 +102,6 @@ Environmental variables in this project include:

- `MONGODB_URI` The MongoDB Connection String (with credentials and database name)
- `WEB_URI` The _URL_ of your web.
- `SESSION_SECRET` (only if you use `express-session`) The secret to be used in `express-session`.
- `CLOUDINARY_URL` (optional, Cloudinary **only**) Cloudinary environment variable for configuration. See [this](https://cloudinary.com/documentation/node_integration#configuration).
- `NODEMAILER_CONFIG` (optional, if using nodemailer **only**) JSON stringified nodemailer config. See `.env.example`.

Expand Down
8 changes: 4 additions & 4 deletions api-lib/middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { passport } from "@/api-lib/auth";
import { ncOpts } from "@/api-lib/nc";
import nc from "next-connect";
import session from "./session";
import { passport } from '@/api-lib/auth';
import { ncOpts } from '@/api-lib/nc';
import nc from 'next-connect';
import session from './session';

const auth = nc(ncOpts)
.use(session)
Expand Down
9 changes: 2 additions & 7 deletions api-lib/middlewares/session.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import MongoStore from 'connect-mongo';
import expressSession from 'express-session';
import { session as nextSession } from 'next-session';

export default function session(req, res, next) {
const mongoStore = MongoStore.create({
client: req.dbClient,
stringify: false,
});
return expressSession({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: mongoStore,
})(req, res, next);
return nextSession({ store: mongoStore })(req, res, next);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"nanoid": "^3.1.25",
"next": "^11.1.2",
"next-connect": "^0.10.2",
"next-session": "^3.4.1",
"nodemailer": "^6.6.3",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
Expand Down

1 comment on commit 230b2a9

@vercel
Copy link

@vercel vercel bot commented on 230b2a9 Sep 19, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.