From e9bf0d797418f955b035abb26547e572e628f88e Mon Sep 17 00:00:00 2001 From: Tiscs Date: Mon, 1 Apr 2024 15:03:37 +0800 Subject: [PATCH] Add environment variable for cookie max age and update dependencies --- .env.example | 2 ++ app/clients/grpc.client.ts | 15 +++------------ app/clients/state.server.ts | 7 ++++--- app/root.tsx | 19 ++----------------- app/session.server.ts | 2 +- package.json | 2 ++ pnpm-lock.yaml | 23 +++++++++++++++++++---- 7 files changed, 33 insertions(+), 37 deletions(-) diff --git a/.env.example b/.env.example index 0a754c6..40577c2 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ # Randomly generated secret key for cookies signing REMIX_COOKIE_SECRET="sEcReT" +REMIX_COOKIE_MAXAGE="604800" # 7 days +VITE_GRPC_ENDPOINT="${GOMMERCE_GRPC_ENDPOINT}" GOMMERCE_GRPC_ENDPOINT="http://localhost:50050" GOMMERCE_AUTH_REALM="admin" # Client basic auth token, calculated as btoa(secret_key + ':' + secret_code) diff --git a/app/clients/grpc.client.ts b/app/clients/grpc.client.ts index c68633f..31f1d98 100644 --- a/app/clients/grpc.client.ts +++ b/app/clients/grpc.client.ts @@ -1,16 +1,7 @@ import invariant from "tiny-invariant"; import type { MessageType, Message } from "@bufbuild/protobuf"; -invariant(window.env.GOMMERCE_GRPC_ENDPOINT, "environment variable GOMMERCE_GRPC_ENDPOINT is required."); - -declare global { - interface Window { - env: { - [key: string]: string | undefined; - GOMMERCE_GRPC_ENDPOINT: string; - }; - } -} +invariant(import.meta.env.VITE_GRPC_ENDPOINT, "environment variable VITE_GRPC_ENDPOINT is required."); export async function fetchUnary, O extends Message>( req: Message, @@ -19,7 +10,7 @@ export async function fetchUnary, O extends Message>( mn: string, rs: AbortSignal | null = null, ): Promise { - const resp = await fetch(`${window.env.GOMMERCE_GRPC_ENDPOINT}/${sn}/${mn}`, { + const resp = await fetch(`${import.meta.env.VITE_GRPC_ENDPOINT}/${sn}/${mn}`, { method: "POST", body: req.toJsonString(), headers: { "Content-Type": "application/json" }, @@ -36,7 +27,7 @@ export async function* fetchStream, O extends Message>( rs: AbortSignal | null = null, ): AsyncGenerator { try { - const resp = await fetch(`${window.env.GOMMERCE_GRPC_ENDPOINT}/${sn}/${mn}`, { + const resp = await fetch(`${import.meta.env.VITE_GRPC_ENDPOINT}/${sn}/${mn}`, { method: "POST", body: req.toJsonString(), headers: { "Content-Type": "application/json" }, diff --git a/app/clients/state.server.ts b/app/clients/state.server.ts index 4ddd5ce..7a48587 100644 --- a/app/clients/state.server.ts +++ b/app/clients/state.server.ts @@ -1,6 +1,7 @@ import type { SessionData, SessionStorage, SessionIdStorageStrategy } from "@remix-run/node"; import { createSessionStorage } from "@remix-run/node"; -import { stateStoreServiceClient as stateStore, snowflakeServiceClient as snowflake } from "~/clients/grpc.server"; +import { stateStoreServiceClient as stateStore } from "~/clients/grpc.server"; +import { v4 as uuidv4 } from "uuid"; interface CookieSessionStorageOptions { bucket?: string; @@ -17,7 +18,7 @@ export function createStateSessionStorage( const key = `${bucket}:${id}`; const metadata: { [key: string]: string } = {}; if (expires) { - metadata["ttlInSeconds"] = (expires.getTime() - Date.now()).toString(10); + metadata["ttlInSeconds"] = Math.floor((expires.getTime() - Date.now()) / 1000).toString(10); } await stateStore.setState( { @@ -32,7 +33,7 @@ export function createStateSessionStorage( return createSessionStorage({ cookie: options?.cookie, async createData(data, expires) { - const { value: id } = await snowflake.nextHex({}); + const id = uuidv4(); await upsert(id, JSON.stringify(data || null), expires); return id; }, diff --git a/app/root.tsx b/app/root.tsx index 9450b9c..d6f8c08 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -1,14 +1,6 @@ import type { LinksFunction, LoaderFunctionArgs } from "@remix-run/node"; import { cssBundleHref } from "@remix-run/css-bundle"; -import { - Links, - Meta, - Scripts, - ScrollRestoration, - isRouteErrorResponse, - useLoaderData, - useRouteError, -} from "@remix-run/react"; +import { Links, Meta, Scripts, ScrollRestoration, isRouteErrorResponse, useRouteError } from "@remix-run/react"; import { authorize } from "~/secure.server"; import Outlet from "~/partials/layout"; import styles from "~/styles/global.css?url"; @@ -23,7 +15,6 @@ export async function loader({ request }: LoaderFunctionArgs) { return { user: identity?.user, scope: identity?.scope, - env: { GOMMERCE_GRPC_ENDPOINT: process.env.GOMMERCE_GRPC_ENDPOINT }, }; } @@ -46,13 +37,7 @@ export function Layout({ children }: { children: React.ReactNode }) { } export default function App() { - const { env } = useLoaderData(); - return ( - <> - -