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

Expose req to genid function similar to express-session #372

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ server.listen(8080);
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| name | The name of the cookie to be read from the request and set to the response. | `sid` |
| store | The session store instance to be used. **Required** to work in production! | `MemoryStore` |
| genid | The function that generates a string for a new session ID. | [`nanoid`](https://github.com/ai/nanoid) |
| genid | The function that exposes the `req` object as a parameter and generates a string for a new session ID. | [`(() => nanoid())`](https://github.com/ai/nanoid) |
| encode | Transforms session ID before setting cookie. It takes the raw session ID and returns the decoded/decrypted session ID. | undefined |
| decode | Transforms session ID back while getting from cookie. It should return the encoded/encrypted session ID | undefined |
| touchAfter | Only touch after an amount of time **(in seconds)** since last access. Disabled by default or if set to `-1`. See [touchAfter](#touchAfter). | `-1` (Disabled) |
Expand Down
4 changes: 2 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function session<T extends SessionRecord = SessionRecord>(options

const name = options.name || "sid";
const store = options.store || new MemoryStore();
const genId = options.genid || nanoid;
const genId = options.genid || (() => nanoid());
const encode = options.encode;
const decode = options.decode;
const touchAfter = options.touchAfter ?? -1;
Expand Down Expand Up @@ -88,7 +88,7 @@ export default function session<T extends SessionRecord = SessionRecord>(options
}
}
} else {
sessionId = genId();
sessionId = genId(req);
session = {
[isNew]: true,
cookie: {
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isDestroyed, isNew, isTouched } from "./symbol";
import { IncomingMessage } from "http";

export type SessionRecord = Record<string, any>

Expand Down Expand Up @@ -40,7 +41,7 @@ export interface SessionStore {
export interface Options {
name?: string;
store?: SessionStore;
genid?: () => string;
genid?: (req: IncomingMessage & { session?: Session }) => string;
encode?: (rawSid: string) => string;
decode?: (encryptedSid: string) => string | null;
touchAfter?: number;
Expand Down