diff --git a/packages/backend-elysia/src/common/context/session.ts b/packages/backend-elysia/src/common/context/session.ts index 07b13950..414d11bd 100644 --- a/packages/backend-elysia/src/common/context/session.ts +++ b/packages/backend-elysia/src/common/context/session.ts @@ -1,9 +1,21 @@ import { jwt, t } from "@backend-utils"; +import { isRunningLocally } from "@frak-labs/app-essentials"; import { Elysia } from "elysia"; import { Config } from "sst/node/config"; +/** + * Some default auth cookies props + */ +const defaultCookiesProps = { + domain: isRunningLocally ? "localhost" : ".frak.id", + sameSite: "none", + maxAge: 60 * 60 * 24 * 7, // 1 week + secure: true, +} as const; + export const sessionContext = new Elysia({ name: "Context.session", + cookie: defaultCookiesProps, }) // Wallet JWT .use( @@ -62,6 +74,12 @@ export const sessionContext = new Elysia({ businessAuth: t.Optional(t.String()), }), }) + .onBeforeHandle(({ cookie: { walletAuth, businessAuth } }) => { + // Set default properties for walletAuth cookie + walletAuth.update(defaultCookiesProps); + // Set default properties for businessAuth cookie + businessAuth.update(defaultCookiesProps); + }) .macro(({ onBeforeHandle }) => ({ authenticated(target?: true | "wallet" | "business" | "wallet-sdk") { if (!target) return; @@ -75,7 +93,7 @@ export const sessionContext = new Elysia({ error, businessJwt, }) => { - if (!businessAuth) { + if (!businessAuth?.value) { return error( "Unauthorized", "Missing business JWT" @@ -136,7 +154,7 @@ export const sessionContext = new Elysia({ error, walletJwt, }) => { - if (!walletAuth) { + if (!walletAuth?.value) { return error(401, "Missing wallet JWT"); } const auth = await walletJwt.verify( @@ -168,8 +186,10 @@ export const walletSessionContext = new Elysia({ }), }) .resolve(async ({ cookie: { walletAuth }, walletJwt }) => { + const value = walletAuth?.value; + if (!value) return {}; return { - walletSession: await walletJwt.verify(walletAuth.value), + walletSession: await walletJwt.verify(value), }; }) .as("plugin"); diff --git a/packages/backend-elysia/src/domain/auth/routes/wallet.ts b/packages/backend-elysia/src/domain/auth/routes/wallet.ts index c80854da..6e880c5e 100644 --- a/packages/backend-elysia/src/domain/auth/routes/wallet.ts +++ b/packages/backend-elysia/src/domain/auth/routes/wallet.ts @@ -5,11 +5,7 @@ import { sessionContext, } from "@backend-common"; import { t } from "@backend-utils"; -import { - WebAuthN, - isRunningLocally, - kernelAddresses, -} from "@frak-labs/app-essentials"; +import { WebAuthN, kernelAddresses } from "@frak-labs/app-essentials"; import { verifyAuthenticationResponse, verifyRegistrationResponse, @@ -30,8 +26,12 @@ export const walletAuthRoutes = new Elysia({ prefix: "/wallet" }) .use(blockchainContext) .use(sessionContext) // Logout - .post("/logout", async ({ cookie: { walletAuth } }) => { - walletAuth.remove(); + .post("/logout", async ({ cookie: { walletAuth, businessAuth } }) => { + walletAuth.update({ + value: "", + maxAge: 0, + }); + businessAuth.remove(); }) // Decode token .get( @@ -44,7 +44,7 @@ export const walletAuthRoutes = new Elysia({ prefix: "/wallet" }) // Decode it const decodedSession = await walletJwt.verify(walletAuth.value); if (!decodedSession) { - console.log("Error decoding session", { decodedSession }); + log.error({ decodedSession }, "Error decoding session"); return error(404, "Invalid wallet session"); } return decodedSession; @@ -174,13 +174,8 @@ export const walletAuthRoutes = new Elysia({ prefix: "/wallet" }) sub: walletAddress, iat: Date.now(), }); - console.log("Setting cookie", { token }); - walletAuth.set({ + walletAuth.update({ value: token, - sameSite: "none", - maxAge: 60 * 60 * 24 * 7, // 1 week - secure: true, - domain: isRunningLocally ? "localhost" : ".frak.id", }); return { @@ -308,12 +303,8 @@ export const walletAuthRoutes = new Elysia({ prefix: "/wallet" }) sub: walletAddress, iat: Date.now(), }); - walletAuth.set({ + walletAuth.update({ value: token, - sameSite: "none", - maxAge: 60 * 60 * 24 * 7, // 1 week - secure: true, - domain: isRunningLocally ? "localhost" : ".frak.id", }); return { diff --git a/packages/wallet/src/module/authentication/component/Login/index.tsx b/packages/wallet/src/module/authentication/component/Login/index.tsx index b11fe5c1..f4df2a77 100644 --- a/packages/wallet/src/module/authentication/component/Login/index.tsx +++ b/packages/wallet/src/module/authentication/component/Login/index.tsx @@ -7,6 +7,8 @@ import { Back } from "@/module/common/component/Back"; import { Grid } from "@/module/common/component/Grid"; import { CloudUpload } from "lucide-react"; import Link from "next/link"; +import { useRouter } from "next/navigation"; +import { useTransition } from "react"; import styles from "./index.module.css"; /** @@ -14,7 +16,15 @@ import styles from "./index.module.css"; * @constructor */ export function Login() { - const { login } = useLogin(); + const router = useRouter(); + const [, startTransition] = useTransition(); + const { login } = useLogin({ + onSuccess: () => { + startTransition(() => { + router.push("/wallet"); + }); + }, + }); return ( <> diff --git a/packages/wallet/src/module/authentication/component/LoginItem/index.tsx b/packages/wallet/src/module/authentication/component/LoginItem/index.tsx index 807ba75a..fc70adf3 100644 --- a/packages/wallet/src/module/authentication/component/LoginItem/index.tsx +++ b/packages/wallet/src/module/authentication/component/LoginItem/index.tsx @@ -14,7 +14,13 @@ export function LoginItem({ }: { lastAuthentication: PreviousAuthenticatorModel }) { const router = useRouter(); const [, startTransition] = useTransition(); - const { login } = useLogin(); + const { login } = useLogin({ + onSuccess: () => { + startTransition(() => { + router.push("/wallet"); + }); + }, + }); return (
  • @@ -23,9 +29,6 @@ export function LoginItem({ className={styles.loginItem__button} onClick={async () => { await login({ lastAuthentication }); - startTransition(() => { - router.push("/wallet"); - }); }} > diff --git a/packages/wallet/src/module/authentication/component/Logout/index.tsx b/packages/wallet/src/module/authentication/component/Logout/index.tsx index 95ffff89..8f55d1b7 100644 --- a/packages/wallet/src/module/authentication/component/Logout/index.tsx +++ b/packages/wallet/src/module/authentication/component/Logout/index.tsx @@ -1,5 +1,5 @@ "use client"; -import { sessionAtom } from "@/module/common/atoms/session"; +import { sdkSessionAtom, sessionAtom } from "@/module/common/atoms/session"; import { Panel } from "@/module/common/component/Panel"; import Row from "@/module/common/component/Row"; import { backendApi } from "@frak-labs/shared/context/server"; @@ -29,6 +29,7 @@ export function Logout() { // Session deletion await backendApi.auth.wallet.logout.post(); jotaiStore.set(sessionAtom, null); + jotaiStore.set(sdkSessionAtom, null); // Query cache queryClient.removeQueries(); // Local storage cleanup diff --git a/packages/wallet/src/module/authentication/component/Register/index.tsx b/packages/wallet/src/module/authentication/component/Register/index.tsx index 5ccf6d00..361e2f75 100644 --- a/packages/wallet/src/module/authentication/component/Register/index.tsx +++ b/packages/wallet/src/module/authentication/component/Register/index.tsx @@ -10,7 +10,11 @@ import styles from "./index.module.css"; export function Register() { const router = useRouter(); - const { register, error, isRegisterInProgress } = useRegister(); + const { register, error, isRegisterInProgress } = useRegister({ + onSuccess: () => { + router.push("/wallet"); + }, + }); const [disabled, setDisabled] = useState(false); /**