-
Notifications
You must be signed in to change notification settings - Fork 279
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
Introduce injected session
util in user-code
#214
Changes from all commits
537d40e
fca9488
4e55389
2cec0e5
356129d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,57 @@ | ||
import { | ||
type AppLoadContext, | ||
createCookieSessionStorage, | ||
type SessionStorage, | ||
type Session, | ||
} from '@shopify/hydrogen-remix'; | ||
|
||
let sessionStorage: SessionStorage; | ||
/** | ||
* This is a custom session implementation for your Hydrogen shop. | ||
* Feel free to customize it to your needs, add helper methods, or | ||
* swap out the cookie-based implementation with something else! | ||
*/ | ||
export class HydrogenSession { | ||
constructor( | ||
private sessionStorage: SessionStorage, | ||
private session: Session, | ||
) {} | ||
|
||
static async init(request: Request, secrets: string[]) { | ||
const storage = createCookieSessionStorage({ | ||
cookie: { | ||
name: 'session', | ||
httpOnly: true, | ||
path: '/', | ||
sameSite: 'lax', | ||
secrets, | ||
}, | ||
}); | ||
|
||
const session = await storage.getSession(request.headers.get('Cookie')); | ||
|
||
return new this(storage, session); | ||
} | ||
|
||
get(key: string) { | ||
return this.session.get(key); | ||
} | ||
|
||
destroy() { | ||
return this.sessionStorage.destroySession(this.session); | ||
} | ||
|
||
export async function getSession( | ||
request: Request, | ||
context: AppLoadContext & {env?: Record<string, string>}, | ||
) { | ||
if (!context.env?.ENCRYPTION_KEY) { | ||
throw new Error('ENCRYPTION_KEY environment variable is not set'); | ||
flash(key: string, value: any) { | ||
this.session.flash(key, value); | ||
} | ||
|
||
sessionStorage ??= createCookieSessionStorage({ | ||
cookie: { | ||
name: 'session', | ||
httpOnly: true, | ||
path: '/', | ||
sameSite: 'lax', | ||
secrets: [context.env.ENCRYPTION_KEY], | ||
}, | ||
}); | ||
|
||
const session = await sessionStorage.getSession( | ||
request.headers.get('Cookie'), | ||
); | ||
|
||
return { | ||
async get(key: string): Promise<any> { | ||
return await session.get(key); | ||
}, | ||
|
||
set(key: string, value: any): void { | ||
session.set(key, value); | ||
}, | ||
|
||
flash(key: string, value: any): void { | ||
session.flash(key, value); | ||
}, | ||
|
||
unset(key: string): void { | ||
session.unset(key); | ||
}, | ||
|
||
async commit(): Promise<string> { | ||
return await sessionStorage.commitSession(session); | ||
}, | ||
}; | ||
unset(key: string) { | ||
this.session.unset(key); | ||
} | ||
|
||
set(key: string, value: any) { | ||
this.session.set(key, value); | ||
} | ||
|
||
commit() { | ||
return this.sessionStorage.commitSession(this.session); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ import {Layout} from '~/components'; | |
import {getLayoutData, getCountries} from '~/data'; | ||
import {GenericError} from './components/GenericError'; | ||
import {NotFound} from './components/NotFound'; | ||
import {getSession} from './lib/session.server'; | ||
import {Seo, Debugger} from './lib/seo'; | ||
|
||
import styles from './styles/app.css'; | ||
|
@@ -59,9 +58,8 @@ export const meta: MetaFunction = () => ({ | |
viewport: 'width=device-width,initial-scale=1', | ||
}); | ||
|
||
export async function loader({request, context, params}: LoaderArgs) { | ||
const session = await getSession(request, context); | ||
const cartId = await session.get('cartId'); | ||
export async function loader({context, params}: LoaderArgs) { | ||
const cartId = await context.session.get('cartId'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the session property be directly on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this is unrelated to other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree session is unrelated, but maybe a use-case for an API that is like |
||
|
||
return defer({ | ||
layout: await getLayoutData(context, params), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda funky: Because we define
storefront
etc inHydrogenContext
from the package, and then augmentAppLoadContext
in local types, these two sets need to be merged 🤔 Any better way to do this?