Skip to content

Commit

Permalink
🐛 Fix cookie props fcked up after each handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Oct 14, 2024
1 parent c661b5e commit 7f0167e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 29 deletions.
26 changes: 23 additions & 3 deletions packages/backend-elysia/src/common/context/session.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -75,7 +93,7 @@ export const sessionContext = new Elysia({
error,
businessJwt,
}) => {
if (!businessAuth) {
if (!businessAuth?.value) {
return error(
"Unauthorized",
"Missing business JWT"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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");
Expand Down
29 changes: 10 additions & 19 deletions packages/backend-elysia/src/domain/auth/routes/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ 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";

/**
* Login from previous authentication
* @constructor
*/
export function Login() {
const { login } = useLogin();
const router = useRouter();
const [, startTransition] = useTransition();
const { login } = useLogin({
onSuccess: () => {
startTransition(() => {
router.push("/wallet");
});
},
});

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<li className={styles.loginItem}>
Expand All @@ -23,9 +29,6 @@ export function LoginItem({
className={styles.loginItem__button}
onClick={async () => {
await login({ lastAuthentication });
startTransition(() => {
router.push("/wallet");
});
}}
>
<span>
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down

0 comments on commit 7f0167e

Please sign in to comment.