-
Is this the intended way to implement the remix-hono middleware? // server.ts
import {
createCookieSessionStorage,
type AppLoadContext,
type Session,
type SessionStorage,
} from "react-router";
import { createHonoServer } from "react-router-hono-server/node";
import { getSession, getSessionStorage, session } from "remix-hono/session";
import { db } from "~/db/index";
declare module "react-router" {
interface AppLoadContext {
sessionStorage: SessionStorage;
session: Session;
db: typeof db;
}
}
export default await createHonoServer({
configure: (server) => {
server.use(
session({
autoCommit: true,
createSessionStorage() {
const sessionStorage = createCookieSessionStorage({
cookie: {
name: "__session",
httpOnly: true,
path: "/",
sameSite: "lax",
secrets: [process.env.SESSION_SECRET!],
secure: process.env.NODE_ENV === "production",
},
});
return {
...sessionStorage,
async commitSession(session) {
return sessionStorage.commitSession(session, {
maxAge: 60 * 60 * 24 * 30, // 30 days
});
},
};
},
})
);
},
getLoadContext(c): AppLoadContext {
const sessionStorage = getSessionStorage(c);
const session = getSession(c);
return { ...c.env, sessionStorage, session, db };
},
}); // routes/index.tsx
import { Form, redirect } from "react-router";
import { authenticator } from "~/auth/auth.server";
import type { Route } from "./+types/home";
export default function Screen() {
return (
<Form method="post">
<input type="email" name="email" required />
<input
type="password"
name="password"
autoComplete="current-password"
required
/>
<button>Sign In</button>
</Form>
);
}
export async function action({ request, context }: Route.ActionArgs) {
let user = await authenticator.authenticate("user-pass", request);
context.session.set("user", user);
throw redirect("/", {
headers: {
"Set-Cookie": await context.sessionStorage.commitSession(context.session),
},
});
}
export async function loader({ context }: Route.LoaderArgs) {
let user = context.session.get("user");
if (user) throw redirect("/dashboard");
return null;
} I mean, it kinda works 😬 |
Beta Was this translation helpful? Give feedback.
Answered by
rphlmr
Dec 21, 2024
Replies: 1 comment
-
It looks good to me! It is not necessary to set the headers in your action response since Remix Hono will auto commit. ex: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
diurivj
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks good to me!
It is not necessary to set the headers in your action response since Remix Hono will auto commit.
ex:
https://github.com/rphlmr/drizzle-lab/tree/main/apps/drizzle-run/app/server
https://github.com/rphlmr/drizzle-lab/blob/642ab4603ab545e09d1d299de6f68b6d5907d778/apps/drizzle-run/app/routes/auth.callback/route.tsx#L53