-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from Shopify/jl-refactor-session
Introduce injected `session` util in user-code
- Loading branch information
Showing
21 changed files
with
128 additions
and
128 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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); | ||
} | ||
} |
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
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
Oops, something went wrong.