From a85ccb57a0b6dc49d069674f2f2efcc59cdb956d Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Mon, 1 Jan 2024 10:58:55 -0300 Subject: [PATCH 1/3] Replace JSON RPC call on username validation --- .../src/components/AddDevice/AddDevice.tsx | 6 +-- .../CreateAccount/CreateAccount.tsx | 52 ++----------------- .../src/lib/controller.ts | 15 ++++-- .../src/lib/firestoreController.ts | 3 ++ 4 files changed, 20 insertions(+), 56 deletions(-) diff --git a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx index 5d3e9fa3..31cedde0 100644 --- a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx +++ b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx @@ -204,7 +204,7 @@ function SignInPage() { methodNames, allowance: new BN('250000000000000'), publicKey: public_key, - }).then((res) => res && res.json()).then((res) => { + }).then((res) => res && res.json()).then(async (res) => { const failure = res['Receipts Outcome'].find(({ outcome: { status } }) => Object.keys(status).some((k) => k === 'Failure'))?.outcome?.status?.Failure; if (failure?.ActionError?.kind?.LackBalanceForState) { navigate(`/devices?${searchParams.toString()}`); @@ -215,9 +215,7 @@ function SignInPage() { const user = firebaseAuth.currentUser; window.firestoreController.updateUser({ userUid: user.uid, - // User type is missing accessToken but it exist - // @ts-ignore - oidcToken: user.accessToken, + oidcToken: await user.getIdToken(), }); // Since FAK is already added, we only add LAK diff --git a/packages/near-fast-auth-signer/src/components/CreateAccount/CreateAccount.tsx b/packages/near-fast-auth-signer/src/components/CreateAccount/CreateAccount.tsx index 31678d6b..6e4c544e 100644 --- a/packages/near-fast-auth-signer/src/components/CreateAccount/CreateAccount.tsx +++ b/packages/near-fast-auth-signer/src/components/CreateAccount/CreateAccount.tsx @@ -10,7 +10,6 @@ import FormContainer from './styles/FormContainer'; import { BadgeProps } from '../../lib/Badge/Badge'; import { Button } from '../../lib/Button'; import Input from '../../lib/Input/Input'; -import { openToast } from '../../lib/Toast'; import { inIframe, redirectWithError } from '../../utils'; import { network } from '../../utils/config'; import { @@ -36,44 +35,6 @@ const StyledContainer = styled.div` const emailProviders = ['gmail', 'yahoo', 'outlook']; -const checkIsAccountAvailable = async (desiredUsername: string): Promise => { - try { - const response = await fetch(network.nodeUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - jsonrpc: '2.0', - id: 'dontcare', - method: 'query', - params: { - request_type: 'view_account', - finality: 'final', - account_id: `${desiredUsername}.${network.fastAuth.accountIdSuffix}`, - }, - }), - }); - const data = await response.json(); - if (data?.error?.cause?.name === 'UNKNOWN_ACCOUNT') { - return true; - } - - if (data?.result?.code_hash) { - return false; - } - - return false; - } catch (error: any) { - console.log(error); - openToast({ - title: error.message, - type: 'ERROR' - }); - return false; - } -}; - const schema = yup.object().shape({ email: yup .string() @@ -88,14 +49,11 @@ const schema = yup.object().shape({ .test( 'is-account-available', async (username, context) => { - if (username) { - const isAvailable = await checkIsAccountAvailable(username); - if (!isAvailable) { - return context.createError({ - message: `${username}.${network.fastAuth.accountIdSuffix} is taken, try something else.`, - path: context.path - }); - } + if (username && await window.fastAuthController.accountExist(username)) { + return context.createError({ + message: `${username}.${network.fastAuth.accountIdSuffix} is taken, try something else.`, + path: context.path + }); } return true; diff --git a/packages/near-fast-auth-signer/src/lib/controller.ts b/packages/near-fast-auth-signer/src/lib/controller.ts index 56414044..cbb20913 100644 --- a/packages/near-fast-auth-signer/src/lib/controller.ts +++ b/packages/near-fast-auth-signer/src/lib/controller.ts @@ -136,12 +136,17 @@ class FastAuthController { return this.accountId; } - async getAccounts() { - if (this.accountId) { - return [this.accountId]; - } + async accountExist(accountId: string): Promise { + try { + const accountState = await new Account(this.connection, accountId).state(); + return !!accountState; + } catch (error) { + if (error?.type === 'AccountDoesNotExist') { + return false; + } - return []; + return true; + } } async signDelegateAction({ receiverId, actions, signerId }) { diff --git a/packages/near-fast-auth-signer/src/lib/firestoreController.ts b/packages/near-fast-auth-signer/src/lib/firestoreController.ts index 45d63daa..c0a3a140 100644 --- a/packages/near-fast-auth-signer/src/lib/firestoreController.ts +++ b/packages/near-fast-auth-signer/src/lib/firestoreController.ts @@ -210,6 +210,9 @@ class FirestoreController { updateUser = async ({ userUid, oidcToken, + }: { + userUid: string; + oidcToken: string; }) => { this.userUid = userUid; this.oidcToken = oidcToken; From f0246249ea21985bf02ca2c6fb74109feee6cbab Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Mon, 1 Jan 2024 16:47:30 -0300 Subject: [PATCH 2/3] Return as soon as the authenticated state is defined --- .../src/components/AuthIndicator/AuthIndicator.tsx | 2 +- packages/near-fast-auth-signer/src/lib/useAuthState.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/near-fast-auth-signer/src/components/AuthIndicator/AuthIndicator.tsx b/packages/near-fast-auth-signer/src/components/AuthIndicator/AuthIndicator.tsx index f96b4459..dedfbe72 100644 --- a/packages/near-fast-auth-signer/src/components/AuthIndicator/AuthIndicator.tsx +++ b/packages/near-fast-auth-signer/src/components/AuthIndicator/AuthIndicator.tsx @@ -12,7 +12,7 @@ function AuthIndicator() { if (authenticated !== 'loading' && authenticated === false) { navigate('/login'); } - }, [authenticated]); + }, [authenticated, navigate]); return ( diff --git a/packages/near-fast-auth-signer/src/lib/useAuthState.ts b/packages/near-fast-auth-signer/src/lib/useAuthState.ts index 56611fb1..b626fe10 100644 --- a/packages/near-fast-auth-signer/src/lib/useAuthState.ts +++ b/packages/near-fast-auth-signer/src/lib/useAuthState.ts @@ -47,6 +47,7 @@ export const useAuthState = (skipGetKeys = false): AuthState => { if (accountsList.length === 0) { setAuthenticated(false); + return; } window.fastAuthController = new FastAuthController({ From 1bb6f36fa7f1b0a8b1420b7c01b87c5de961a02f Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Wed, 10 Jan 2024 10:54:55 -0300 Subject: [PATCH 3/3] Fix merge conflicts --- .../src/components/AddDevice/AddDevice.tsx | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx index c63a8c83..5c3963fc 100644 --- a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx +++ b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx @@ -180,8 +180,7 @@ function SignInPage() { : null; const existingDeviceLakKey = existingDevice?.publicKeys?.filter((key) => key !== publicKeyFak)[0]; - // @ts-ignore - const oidcToken = user.accessToken; + const oidcToken = await user.getIdToken(); const recoveryPK = await window.fastAuthController.getUserCredential(oidcToken); // if given lak key is already attached to webAuthN public key, no need to add it again @@ -215,8 +214,6 @@ function SignInPage() { // Add device window.firestoreController.updateUser({ userUid: user.uid, - oidcToken: await user.getIdToken(), - // User type is missing accessToken but it exist oidcToken, });