This is a helper library for the Remix framework. It provides a SessionProxy
class that simplifies commiting and destroying sessions in loaders and actions.
It supports all official Remix adapters that implement the SessionStorage
interface, including @remix-run/node
, @remix-run/cloudflare
, @remix-run/deno
, and @remix-run/architect
npm install remix-session-helper
# or
yarn add remix-session-helper
import { createSessionStorage } from "@remix-run/node";
import { wrapSessionStorage } from "remix-session-helper";
// Optional but recommended
type SessionKey = "userId" | "login-error";
const { getSession } = wrapSessionStorage<SessionKey>(
createSessionStorage({
// ...session storage options
}),
);
export async function action({ request }: ActionArgs) {
const session = await getSession(request);
const formData = await request.formData();
const username = formData.get("username");
const password = formData.get("password");
const user = await verifyLogin(username, password);
if (!user) {
session.flash("login-error", "Invalid username or password");
return session.redirect("/login");
}
session.set("userId", user.id);
return session.redirect("/dashboard");
}
For all methods, if the SessionKey generic is used, the key will be type-checked.
The session ID.
A boolean indicating whether the session has been modified.
Checks if a key exists in the session.
Gets a value from the session.
Gets a string value from the session, if the value is not a string, null
will be returned.
Gets a string value from the session, if the value is not a string, an error will be thrown.
Sets a value in the session.
Unsets a value in the session.
Sets a value in the session and marks it as a flash value. Flash values are automatically unset after the next request.
A wrapper around Remix's json
function that commits the session before returning the response.
A wrapper around Remix's json
function that destroys the session before returning the response.
A wrapper around Remix's redirect
function that commits the session before returning the response.
A wrapper around Remix's redirect
function that destroys the session before returning the response.