-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
46 additions
and
40 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
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 |
---|---|---|
@@ -1,35 +1,35 @@ | ||
import { IncomingMessage, ServerResponse } from 'http' | ||
import Cookies from 'cookies' | ||
import { randomBytes } from 'crypto' | ||
import * as z from "zod" | ||
import * as z from 'zod' | ||
|
||
// for testing, for production workload use the env var COOKIE_SECRETS | ||
function generateSecret(){ | ||
function generateSecret() { | ||
return randomBytes(32) | ||
.toString('base64') | ||
.slice(0, 32) | ||
} | ||
|
||
export function sessionManager<T>(opts: {domain: string, schema: z.ZodSchema<T>, keys?: string[] }){ | ||
const keys = opts.keys ?? [generateSecret()] | ||
return function session(req: IncomingMessage, res: ServerResponse<IncomingMessage>, thumbprint: string){ | ||
const cookies = new Cookies(req, res, { | ||
secure: true, | ||
keys | ||
}) | ||
const data = cookies.get(`preevy-${thumbprint}`, {signed: true}); | ||
let currentUser = data ? opts.schema.parse(JSON.parse(data)) : undefined | ||
const session = { | ||
get user(){ return currentUser}, | ||
set(user: T){ | ||
currentUser = user | ||
}, | ||
save: ()=> { | ||
cookies.set(`preevy-${thumbprint}`, JSON.stringify(currentUser), {domain: opts.domain, signed: true}) | ||
} | ||
} | ||
return session | ||
export function session<T>(opts: {domain: string; schema: z.ZodSchema<T>; keys?: string[] }) { | ||
const keys = opts.keys ?? [generateSecret()] | ||
return function getSession(req: IncomingMessage, res: ServerResponse<IncomingMessage>, thumbprint: string) { | ||
const cookies = new Cookies(req, res, { | ||
secure: true, | ||
keys, | ||
}) | ||
const data = cookies.get(`preevy-${thumbprint}`, { signed: true }) | ||
let currentUser = data ? opts.schema.parse(JSON.parse(data)) : undefined | ||
|
||
return { | ||
get user() { return currentUser }, | ||
set(user: T) { | ||
currentUser = user | ||
}, | ||
save: () => { | ||
cookies.set(`preevy-${thumbprint}`, JSON.stringify(currentUser), { domain: opts.domain, signed: true }) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
export type SessionManager<T> = ReturnType<typeof sessionManager<T>> | ||
export type Session<T> = ReturnType<typeof session<T>> |