-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(API): expose ironSession (express), applySession, cookieName req…
…uired New features: - an Express/Connect middleware: import { ironSession } from "next-iron-session" app.use(ironSession(options)) or router.get("/", ironSession(options), (req, res, next) => {}); - import { applySession } from "next-iron-session": await applySession(req, res, options); Examples: - moved to examples/ folder - added an Express example BREAKING CHANGE: - you need to import withIronSession as a named export: before: import withIronSession from "next-iron-session" after: import { withIronSession } from "next-iron-session" - cookieName option is now mandatory (#54) fixes #54 fixes #9 fixes #41
- Loading branch information
Showing
40 changed files
with
1,298 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
example/ | ||
examples/next.js/ | ||
examples/express/node_modules | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Express example application using next-iron-session | ||
|
||
This is a small example application generated with the [Express application generator](https://expressjs.com/en/starter/generator.html). | ||
|
||
The tl;dr; on how to use `next-iron-session` with Express is this: | ||
|
||
```js | ||
import { ironSession } from "next-iron-session"; | ||
|
||
const session = ironSession({ | ||
cookieName: "next-iron-session/examples/express", | ||
password: process.env.SECRET_COOKIE_PASSWORD, | ||
cookieOptions: { | ||
secure: process.env.NODE_ENV === "production" ? true : false, | ||
}, | ||
}); | ||
|
||
router.get("/profile", session, async function (req, res) { | ||
// now you can access all of the req.session.* utilities | ||
if (req.session.get("user") === undefined) { | ||
res.redirect("/restricted"); | ||
return; | ||
} | ||
|
||
res.render("profile", { | ||
title: "Profile", | ||
userId: req.session.get("user").id, | ||
}); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var path = require("path"); | ||
|
||
var createError = require("http-errors"); | ||
var express = require("express"); | ||
var logger = require("morgan"); | ||
|
||
var indexRouter = require("./routes/index"); | ||
var usersRouter = require("./routes/users"); | ||
|
||
var app = express(); | ||
|
||
// view engine setup | ||
app.set("views", path.join(__dirname, "views")); | ||
app.set("view engine", "jade"); | ||
|
||
app.use(logger("dev")); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(express.static(path.join(__dirname, "public"))); | ||
|
||
app.use("/", indexRouter); | ||
app.use("/users", usersRouter); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function (req, res, next) { | ||
next(createError(404)); | ||
}); | ||
|
||
// error handler | ||
app.use(function (err, req, res) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get("env") === "development" ? err : {}; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.render("error"); | ||
}); | ||
|
||
module.exports = app; |
Oops, something went wrong.