From 5a01e09fa5e2f87ae56ed255bb993c8f31b6fc3c Mon Sep 17 00:00:00 2001 From: Yun Date: Thu, 30 May 2024 09:22:16 -1000 Subject: [PATCH] Login optimizations (#321) * Login optimizations * Remove storage access for cookies --- .../src/components/Auth/Authenticate.tsx | 8 +-- .../keychain/src/components/Auth/Login.tsx | 54 +++++++++---------- .../keychain/src/components/Auth/Signup.tsx | 9 +++- .../keychain/src/components/Auth/utils.ts | 15 ------ packages/keychain/src/pages/cookie.tsx | 23 -------- packages/keychain/src/pages/index.tsx | 1 + packages/keychain/src/utils/controller.ts | 23 ++++---- 7 files changed, 44 insertions(+), 89 deletions(-) delete mode 100644 packages/keychain/src/pages/cookie.tsx diff --git a/packages/keychain/src/components/Auth/Authenticate.tsx b/packages/keychain/src/components/Auth/Authenticate.tsx index 3d3b3b5f2..9815d3c67 100644 --- a/packages/keychain/src/components/Auth/Authenticate.tsx +++ b/packages/keychain/src/components/Auth/Authenticate.tsx @@ -5,7 +5,6 @@ import { doSignup } from "hooks/account"; import { Container } from "../Container"; import { PortalBanner } from "components/PortalBanner"; import { PortalFooter } from "components/PortalFooter"; -import { requestStorageDropCookie } from "./utils"; type AuthAction = "signup" | "login"; @@ -24,8 +23,6 @@ export function Authenticate({ const onAuth = useCallback(async () => { setIsLoading(true); - await requestStorageDropCookie(); - try { switch (action) { case "signup": @@ -76,10 +73,7 @@ export function Authenticate({ return ( <> - + diff --git a/packages/keychain/src/components/Auth/Signup.tsx b/packages/keychain/src/components/Auth/Signup.tsx index 40717886e..59c7afee1 100644 --- a/packages/keychain/src/components/Auth/Signup.tsx +++ b/packages/keychain/src/components/Auth/Signup.tsx @@ -8,7 +8,11 @@ import { useFormikContext, } from "formik"; import { constants } from "starknet"; -import { PORTAL_FOOTER_MIN_HEIGHT, PortalBanner, PortalFooter } from "components"; +import { + PORTAL_FOOTER_MIN_HEIGHT, + PortalBanner, + PortalFooter, +} from "components"; import { useCallback, useEffect, useState } from "react"; import { DeployAccountDocument, useAccountQuery } from "generated/graphql"; import Controller from "utils/controller"; @@ -57,7 +61,7 @@ export function Signup({ doSignup(decodeURIComponent(values.username)) .catch((e) => { - setError(e) + setError(e); }) .finally(() => setIsLoading(false)); }, []); @@ -138,6 +142,7 @@ function Form({ chainId: "starknet:SN_SEPOLIA", }); await controller.account(constants.StarknetChainId.SN_SEPOLIA).sync(); + controller.store(); // TODO: Enable once controller is ready for mainnet // controller.account(constants.StarknetChainId.SN_MAIN).status = diff --git a/packages/keychain/src/components/Auth/utils.ts b/packages/keychain/src/components/Auth/utils.ts index 64bd2c557..eb0abed51 100644 --- a/packages/keychain/src/components/Auth/utils.ts +++ b/packages/keychain/src/components/Auth/utils.ts @@ -48,21 +48,6 @@ export function fetchAccount(username: string) { }); } -// Cross-site cookies access will eventually be disabled on all major browsers, use storage access api -// for now, then migrate to CHIPS when golang 1.23 with support for partitioned cookies is released. -// https://developers.google.com/privacy-sandbox/3pcd -export async function requestStorageDropCookie() { - await document.requestStorageAccess(); - - if (process.env.NODE_ENV === "development") { - document.cookie = "visited=true; path=/"; - return; - } - - document.cookie = - "visited=true; domain=.cartridge.gg; path=/; samesite=none; secure"; -} - export function isIframe() { return typeof window !== "undefined" ? window.top !== window.self : false; } diff --git a/packages/keychain/src/pages/cookie.tsx b/packages/keychain/src/pages/cookie.tsx deleted file mode 100644 index d188f2fef..000000000 --- a/packages/keychain/src/pages/cookie.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import { Button } from "@chakra-ui/react"; -import { requestStorageDropCookie } from "components/Auth/utils"; -import type { NextPage } from "next"; - -const CookieDropper: NextPage = () => { - return ( - - ); -}; - -export default CookieDropper; diff --git a/packages/keychain/src/pages/index.tsx b/packages/keychain/src/pages/index.tsx index 5cb5efbb1..f79fdda4a 100644 --- a/packages/keychain/src/pages/index.tsx +++ b/packages/keychain/src/pages/index.tsx @@ -298,6 +298,7 @@ const Index: NextPage = () => { /> ) : ( { setPrefilledUsername(username); diff --git a/packages/keychain/src/utils/controller.ts b/packages/keychain/src/utils/controller.ts index 12fcd413b..7f1f62a4e 100644 --- a/packages/keychain/src/utils/controller.ts +++ b/packages/keychain/src/utils/controller.ts @@ -58,13 +58,6 @@ export default class Controller { }, ), ]; - - Storage.set( - selectors[VERSION].admin(this.address, process.env.NEXT_PUBLIC_ADMIN_URL), - {}, - ); - Storage.set(selectors["0.0.1"].active(), address); - this.store(); } async getUser() { @@ -105,11 +98,10 @@ export default class Controller { throw new Error("Account not found"); } - const credentials = await account.cartridge - .createSession(policies, expiresAt) - .catch((e) => { - console.log(e); - }); + const credentials = await account.cartridge.createSession( + policies, + expiresAt, + ); Storage.set(selectors[VERSION].session(this.address, origin, chainId), { policies, @@ -144,6 +136,13 @@ export default class Controller { store() { Storage.set("version", VERSION); + + Storage.set( + selectors[VERSION].admin(this.address, process.env.NEXT_PUBLIC_ADMIN_URL), + {}, + ); + Storage.set(selectors[VERSION].active(), this.address); + return Storage.set(selectors[VERSION].account(this.address), { address: this.address, publicKey: this.publicKey,