-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession.server.ts
More file actions
38 lines (35 loc) · 1.15 KB
/
session.server.ts
File metadata and controls
38 lines (35 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createCookieSessionStorage } from 'react-router'
export const authSessionStorage = createCookieSessionStorage({
cookie: {
name: 'en_session',
sameSite: 'lax', // CSRF protection is advised if changing to 'none'
path: '/',
httpOnly: true,
secrets: process.env.SESSION_SECRET.split(','),
secure: process.env.NODE_ENV === 'production',
},
})
// we have to do this because every time you commit the session you overwrite it
// so we store the expiration time in the cookie and reset it every time we commit
const originalCommitSession = authSessionStorage.commitSession
Object.defineProperty(authSessionStorage, 'commitSession', {
value: async function commitSession(
...args: Parameters<typeof originalCommitSession>
) {
const [session, options] = args
if (options?.expires) {
session.set('expires', options.expires)
}
if (options?.maxAge) {
session.set('expires', new Date(Date.now() + options.maxAge * 1000))
}
const expires = session.has('expires')
? new Date(session.get('expires'))
: undefined
const setCookieHeader = await originalCommitSession(session, {
...options,
expires,
})
return setCookieHeader
},
})