Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login optimizations #321

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions packages/keychain/src/components/Auth/Authenticate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -24,8 +23,6 @@ export function Authenticate({
const onAuth = useCallback(async () => {
setIsLoading(true);

await requestStorageDropCookie();

try {
switch (action) {
case "signup":
Expand Down Expand Up @@ -76,10 +73,7 @@ export function Authenticate({
return (
<>
<Container hideAccount>
<PortalBanner
title={title}
description={description}
/>
<PortalBanner title={title} description={description} />

<PortalFooter>
<Button colorScheme="colorful" onClick={onAuth} isLoading={isLoading}>
Expand Down
54 changes: 24 additions & 30 deletions packages/keychain/src/components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { useAnalytics } from "hooks/analytics";
import { fetchAccount, validateUsernameFor } from "./utils";
import { RegistrationLink } from "./RegistrationLink";
import { useControllerTheme } from "hooks/theme";
import { PopupCenter } from "utils/url";
import { doLogin } from "hooks/account";
import { Error as ErrorComp } from "components/Error";

Expand All @@ -29,26 +28,38 @@ export function Login({
const { event: log } = useAnalytics();
const theme = useControllerTheme();
const [isLoading, setIsLoading] = useState(false);
const [expiresAt] = useState<bigint>(3000000000n);
const [error, setError] = useState();

const onSubmit = useCallback(
async (values: FormValues) => {
setIsLoading(true);

let address;
try {
const {
account: {
credentials: {
webauthn: [{ id: credentialId, publicKey }],
},
contractAddress,
const {
account: {
credentials: {
webauthn: [{ id: credentialId, publicKey }],
},
} = await fetchAccount(values.username);
address = contractAddress;
contractAddress: address,
},
} = await fetchAccount(values.username);

try {
const controller = new Controller(address, publicKey, credentialId);

await doLogin(values.username, credentialId, publicKey);
onSuccess(new Controller(address, publicKey, credentialId));
if (isSlot) {
await doLogin(values.username, credentialId, publicKey);
} else {
await controller.approve(
context.origin,
chainId,
expiresAt,
context.policies,
);
}

controller.store();
onSuccess(controller);

log({ type: "webauthn_login", address });
} catch (e) {
Expand Down Expand Up @@ -118,23 +129,6 @@ export function Login({
type="submit"
colorScheme="colorful"
isLoading={isLoading}
onClick={async (ev) => {
// Storage request must be done in onClick rather than onSubmit
document.requestStorageAccess().catch((e) => {
console.error(e);
PopupCenter(
`/authenticate?name=${encodeURIComponent(
props.values.username,
)}&action=login`,
"Cartridge Login",
480,
640,
);

// Prevent onsubmit from firing
ev.preventDefault();
});
}}
>
Log in
</Button>
Expand Down
9 changes: 7 additions & 2 deletions packages/keychain/src/components/Auth/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -57,7 +61,7 @@ export function Signup({

doSignup(decodeURIComponent(values.username))
.catch((e) => {
setError(e)
setError(e);
})
.finally(() => setIsLoading(false));
}, []);
Expand Down Expand Up @@ -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 =
Expand Down
15 changes: 0 additions & 15 deletions packages/keychain/src/components/Auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
23 changes: 0 additions & 23 deletions packages/keychain/src/pages/cookie.tsx

This file was deleted.

1 change: 1 addition & 0 deletions packages/keychain/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ const Index: NextPage = () => {
/>
) : (
<Login
chainId={chainId}
prefilledName={prefilledUsername}
onSignup={(username) => {
setPrefilledUsername(username);
Expand Down
23 changes: 11 additions & 12 deletions packages/keychain/src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading