From cf17c1e0b22376597b9853c1ac404487836a610f Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Sat, 10 Feb 2024 13:14:56 +0400 Subject: [PATCH 1/6] Move FirestoreController initialization to App.tsx --- packages/near-fast-auth-signer/src/App.tsx | 6 +++- .../src/components/AddDevice/AddDevice.tsx | 4 --- .../components/AuthCallback/AuthCallback.tsx | 6 ---- .../src/components/Devices/Devices.tsx | 31 +++++++++---------- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/packages/near-fast-auth-signer/src/App.tsx b/packages/near-fast-auth-signer/src/App.tsx index e4efe331..70e3bbed 100644 --- a/packages/near-fast-auth-signer/src/App.tsx +++ b/packages/near-fast-auth-signer/src/App.tsx @@ -16,6 +16,7 @@ import VerifyEmailPage from './components/VerifyEmail/verify-email'; import FastAuthController from './lib/controller'; import './styles/theme.css'; import './styles/globals.css'; +import FirestoreController from './lib/firestoreController'; import GlobalStyle from './styles/index'; import { basePath, networkId } from './utils/config'; @@ -59,7 +60,10 @@ export default function App() { log('faLog'); log2('faLogzzzzz'); - // @ts-ignore + if (!window.firestoreController) { + window.firestoreController = new FirestoreController(); + } + return ( <> 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 22d8e1b8..57752409 100644 --- a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx +++ b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx @@ -12,7 +12,6 @@ import styled from 'styled-components'; import * as yup from 'yup'; import { Button } from '../../lib/Button'; -import FirestoreController from '../../lib/firestoreController'; import Input from '../../lib/Input/Input'; import { openToast } from '../../lib/Toast'; import { useAuthState } from '../../lib/useAuthState'; @@ -94,9 +93,6 @@ function SignInPage() { const skipGetKey = decodeIfTruthy(searchParams.get('skipGetKey')); const { authenticated } = useAuthState(skipGetKey); const [renderRedirectButton, setRenderRedirectButton] = useState(''); - if (!window.firestoreController) { - window.firestoreController = new FirestoreController(); - } const addDevice = useCallback(async (data: any) => { if (!data.email) return; diff --git a/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx b/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx index 8a4653d2..96c87f85 100644 --- a/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx +++ b/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx @@ -8,7 +8,6 @@ import styled from 'styled-components'; import { createNEARAccount, fetchAccountIds } from '../../api'; import FastAuthController from '../../lib/controller'; -import FirestoreController from '../../lib/firestoreController'; import { decodeIfTruthy, inIframe, isUrlNotJavascriptProtocol, redirectWithError } from '../../utils'; @@ -207,10 +206,6 @@ function AuthCallbackPage() { window.location.replace(parsedUrl.href); } - if (!window.firestoreController) { - window.firestoreController = new FirestoreController(); - } - setStatusMessage('Verifying email...'); try { @@ -241,7 +236,6 @@ function AuthCallbackPage() { await window.fastAuthController.claimOidcToken(accessToken); const oidcKeypair = await window.fastAuthController.getKey(`oidc_keypair_${accessToken}`); - window.firestoreController = new FirestoreController(); window.firestoreController.updateUser({ userUid: user.uid, oidcToken: accessToken, diff --git a/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx b/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx index 88e08387..e1c41040 100644 --- a/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx +++ b/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx @@ -1,9 +1,8 @@ import { captureException } from '@sentry/react'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { styled } from 'styled-components'; -import FirestoreController from '../../lib/firestoreController'; import { decodeIfTruthy, inIframe } from '../../utils'; import { basePath } from '../../utils/config'; import { onSignIn } from '../AuthCallback/AuthCallback'; @@ -48,7 +47,6 @@ function Devices() { const [isAddingKey, setIsAddingKey] = useState(false); const [isVerifyEmailRequired, setVerifyEmailRequired] = useState(false); const [deleteCollections, setDeleteCollections] = useState([]); - const controller = useMemo(() => new FirestoreController(), []); const [searchParams] = useSearchParams(); const navigate = useNavigate(); const public_key_lak = decodeIfTruthy(searchParams.get('public_key_lak')) || decodeIfTruthy(searchParams.get('public_key')); @@ -64,12 +62,12 @@ function Devices() { useEffect(() => { const getCollection = async () => { - const deviceCollections = await controller.listDevices(); + const deviceCollections = await window.firestoreController.listDevices(); setIsLoaded(false); setCollections(deviceCollections); }; - const getKeypairOrLogout = () => window.fastAuthController.findInKeyStores(`oidc_keypair_${controller.getUserOidcToken()}`).then((keypair) => { + const getKeypairOrLogout = () => window.fastAuthController.findInKeyStores(`oidc_keypair_${window.firestoreController.getUserOidcToken()}`).then((keypair) => { if (keypair) { getCollection(); } else { @@ -80,18 +78,19 @@ function Devices() { } }); setIsLoaded(true); - if (controller.getUserOidcToken()) { + if (window.firestoreController.getUserOidcToken()) { getKeypairOrLogout(); } else { - (new Promise((resolve) => { setTimeout(resolve, 5000); })).then(controller.getUserOidcToken).then((token) => { - if (!token) { - setVerifyEmailRequired(true); - } else { - getKeypairOrLogout(); - } - }); + (new Promise((resolve) => { setTimeout(resolve, 5000); })) + .then(window.firestoreController.getUserOidcToken).then((token) => { + if (!token) { + setVerifyEmailRequired(true); + } else { + getKeypairOrLogout(); + } + }); } - }, [controller]); + }, []); const redirectToSignin = () => { if (inIframe()) { @@ -115,7 +114,7 @@ function Devices() { }; }); - return controller.deleteDeviceCollections(list) + return window.firestoreController.deleteDeviceCollections(list) .then(async () => { setisDeleted(false); setCollections(collections.filter((collection) => (!deleteCollections.includes(collection.id)))); @@ -128,7 +127,7 @@ function Devices() { const email = window.localStorage.getItem('emailForSignIn'); const methodNames = decodeIfTruthy(searchParams.get('methodNames')); const success_url = decodeIfTruthy(searchParams.get('success_url')); - const oidcToken = await controller.getUserOidcToken(); + const oidcToken = await window.firestoreController.getUserOidcToken(); await onSignIn({ accessToken: oidcToken, publicKeyFak, From b51e9598dd12989fc7f7f435f7cd4134fea20133 Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Sun, 11 Feb 2024 15:10:47 +0400 Subject: [PATCH 2/6] Reset status on setUser --- .../src/components/AddDevice/AddDevice.tsx | 7 ---- .../components/AuthCallback/AuthCallback.tsx | 4 -- .../src/components/Devices/Devices.tsx | 31 ++++++++------- .../src/lib/firestoreController.ts | 39 +++++++++++-------- 4 files changed, 40 insertions(+), 41 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 57752409..cf95acda 100644 --- a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx +++ b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx @@ -205,13 +205,6 @@ function SignInPage() { return null; } - // Add device - window.firestoreController.updateUser({ - userUid: user.uid, - // User type is missing accessToken but it exist - oidcToken, - }); - // Since FAK is already added, we only add LAK return window.firestoreController.addDeviceCollection({ fakPublicKey: null, diff --git a/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx b/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx index 96c87f85..b0e23480 100644 --- a/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx +++ b/packages/near-fast-auth-signer/src/components/AuthCallback/AuthCallback.tsx @@ -236,10 +236,6 @@ function AuthCallbackPage() { await window.fastAuthController.claimOidcToken(accessToken); const oidcKeypair = await window.fastAuthController.getKey(`oidc_keypair_${accessToken}`); - window.firestoreController.updateUser({ - userUid: user.uid, - oidcToken: accessToken, - }); const callback = isRecovery ? onSignIn : onCreateAccount; await callback({ diff --git a/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx b/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx index e1c41040..c8fcaf05 100644 --- a/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx +++ b/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx @@ -67,7 +67,7 @@ function Devices() { setCollections(deviceCollections); }; - const getKeypairOrLogout = () => window.fastAuthController.findInKeyStores(`oidc_keypair_${window.firestoreController.getUserOidcToken()}`).then((keypair) => { + const getKeypairOrLogout = async () => window.fastAuthController.findInKeyStores(`oidc_keypair_${await window.firestoreController.getUserOidcToken()}`).then((keypair) => { if (keypair) { getCollection(); } else { @@ -78,18 +78,23 @@ function Devices() { } }); setIsLoaded(true); - if (window.firestoreController.getUserOidcToken()) { - getKeypairOrLogout(); - } else { - (new Promise((resolve) => { setTimeout(resolve, 5000); })) - .then(window.firestoreController.getUserOidcToken).then((token) => { - if (!token) { - setVerifyEmailRequired(true); - } else { - getKeypairOrLogout(); - } - }); - } + + const verifyUserAuthenticationStatus = async () => { + if (await window.firestoreController.getUserOidcToken()) { + getKeypairOrLogout(); + } else { + (new Promise((resolve) => { setTimeout(resolve, 5000); })) + .then(window.firestoreController.getUserOidcToken).then((token) => { + if (!token) { + setVerifyEmailRequired(true); + } else { + getKeypairOrLogout(); + } + }); + } + }; + + verifyUserAuthenticationStatus(); }, []); const redirectToSignin = () => { diff --git a/packages/near-fast-auth-signer/src/lib/firestoreController.ts b/packages/near-fast-auth-signer/src/lib/firestoreController.ts index 3ff31c0a..680eadce 100644 --- a/packages/near-fast-auth-signer/src/lib/firestoreController.ts +++ b/packages/near-fast-auth-signer/src/lib/firestoreController.ts @@ -1,5 +1,4 @@ import { captureException } from '@sentry/react'; -import { User } from 'firebase/auth'; import { getFirestore, Firestore, collection, setDoc, getDoc, getDocs, query, doc, CollectionReference, deleteDoc, @@ -7,7 +6,7 @@ import { import UAParser from 'ua-parser-js'; import { fetchAccountIds } from '../api'; -import { checkFirestoreReady, firebaseApp, firebaseAuth } from '../utils/firebase'; +import { firebaseApp, firebaseAuth } from '../utils/firebase'; import { getDeleteKeysAction } from '../utils/mpc-service'; import { Device } from '../utils/types'; @@ -20,19 +19,26 @@ class FirestoreController { constructor() { this.firestore = getFirestore(firebaseApp); + this.setUser(); + } - firebaseAuth.onIdTokenChanged(async (user: User) => { - if (!user) { - return; - } + async setUser() { + this.userUid = undefined; + this.oidcToken = undefined; + + const user = firebaseAuth.currentUser; + if (user) { + console.log({ + uid: user.uid, + token: await user.getIdToken() + }); this.userUid = user.uid; this.oidcToken = await user.getIdToken(); - }); - - checkFirestoreReady(); + } } async getAccountIdFromOidcToken() { + await this.setUser(); const recoveryPK = await window.fastAuthController.getUserCredential(this.oidcToken); const accountIds = await fetchAccountIds(recoveryPK); @@ -56,6 +62,7 @@ class FirestoreController { accountId?: string; }) { try { + await this.setUser(); const parser = new UAParser(); const device = parser.getDevice(); const os = parser.getOS(); @@ -104,6 +111,7 @@ class FirestoreController { } async listDevices() { + await this.setUser(); const q = query(collection(this.firestore, `/users/${this.userUid}/devices`) as CollectionReference); const querySnapshot = await getDocs(q); const collections = []; @@ -154,6 +162,7 @@ class FirestoreController { } async deleteDeviceCollections(list) { + await this.setUser(); const recoveryPK = await window.fastAuthController.getUserCredential(this.oidcToken); const accountIds = await fetchAccountIds(recoveryPK); @@ -192,6 +201,7 @@ class FirestoreController { } async getDeviceCollection(fakPublicKey) { + await this.setUser(); const docRef = doc(this.firestore, 'users', this.userUid, 'devices', fakPublicKey); const docSnap = await getDoc(docRef); @@ -201,16 +211,11 @@ class FirestoreController { return null; } - updateUser = async ({ - userUid, - oidcToken, - }) => { - this.userUid = userUid; - this.oidcToken = oidcToken; + getUserOidcToken = async () => { + await this.setUser(); + return this.oidcToken; }; - getUserOidcToken = () => this.oidcToken; - async addAccountIdPublicKey(publicKey: string, accountId: string) { await setDoc(doc(this.firestore, 'publicKeys', publicKey), { accountId, From 9fc8c40561f16f2e2b55f5d6c11a28093d0bd2d1 Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Sun, 11 Feb 2024 15:11:49 +0400 Subject: [PATCH 3/6] Remove debug code --- packages/near-fast-auth-signer/src/lib/firestoreController.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/near-fast-auth-signer/src/lib/firestoreController.ts b/packages/near-fast-auth-signer/src/lib/firestoreController.ts index 680eadce..b647d252 100644 --- a/packages/near-fast-auth-signer/src/lib/firestoreController.ts +++ b/packages/near-fast-auth-signer/src/lib/firestoreController.ts @@ -28,10 +28,6 @@ class FirestoreController { const user = firebaseAuth.currentUser; if (user) { - console.log({ - uid: user.uid, - token: await user.getIdToken() - }); this.userUid = user.uid; this.oidcToken = await user.getIdToken(); } From fd8cb41b027cecafa5ab7a1fc467836aa7eb4cc7 Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Sun, 11 Feb 2024 22:40:01 +0400 Subject: [PATCH 4/6] Simplify code --- .../src/components/Devices/Devices.tsx | 29 +++++----- .../src/lib/firestoreController.ts | 58 ++++++------------- 2 files changed, 34 insertions(+), 53 deletions(-) diff --git a/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx b/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx index c8fcaf05..4580b982 100644 --- a/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx +++ b/packages/near-fast-auth-signer/src/components/Devices/Devices.tsx @@ -3,6 +3,7 @@ import React, { useEffect, useState } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { styled } from 'styled-components'; +import FirestoreController from '../../lib/firestoreController'; import { decodeIfTruthy, inIframe } from '../../utils'; import { basePath } from '../../utils/config'; import { onSignIn } from '../AuthCallback/AuthCallback'; @@ -67,24 +68,26 @@ function Devices() { setCollections(deviceCollections); }; - const getKeypairOrLogout = async () => window.fastAuthController.findInKeyStores(`oidc_keypair_${await window.firestoreController.getUserOidcToken()}`).then((keypair) => { - if (keypair) { - getCollection(); - } else { - window.fastAuthController.clearUser().then(() => { - setVerifyEmailRequired(true); - setIsLoaded(false); - }); - } - }); + const getKeypairOrLogout = async () => window.fastAuthController + .findInKeyStores(`oidc_keypair_${await FirestoreController.getUserOidcToken()}`) + .then((keypair) => { + if (keypair) { + getCollection(); + } else { + window.fastAuthController.clearUser().then(() => { + setVerifyEmailRequired(true); + setIsLoaded(false); + }); + } + }); setIsLoaded(true); const verifyUserAuthenticationStatus = async () => { - if (await window.firestoreController.getUserOidcToken()) { + if (await FirestoreController.getUserOidcToken()) { getKeypairOrLogout(); } else { (new Promise((resolve) => { setTimeout(resolve, 5000); })) - .then(window.firestoreController.getUserOidcToken).then((token) => { + .then(FirestoreController.getUserOidcToken).then((token) => { if (!token) { setVerifyEmailRequired(true); } else { @@ -132,7 +135,7 @@ function Devices() { const email = window.localStorage.getItem('emailForSignIn'); const methodNames = decodeIfTruthy(searchParams.get('methodNames')); const success_url = decodeIfTruthy(searchParams.get('success_url')); - const oidcToken = await window.firestoreController.getUserOidcToken(); + const oidcToken = await FirestoreController.getUserOidcToken(); await onSignIn({ accessToken: oidcToken, publicKeyFak, diff --git a/packages/near-fast-auth-signer/src/lib/firestoreController.ts b/packages/near-fast-auth-signer/src/lib/firestoreController.ts index b647d252..91cc7e57 100644 --- a/packages/near-fast-auth-signer/src/lib/firestoreController.ts +++ b/packages/near-fast-auth-signer/src/lib/firestoreController.ts @@ -13,29 +13,12 @@ import { Device } from '../utils/types'; class FirestoreController { private firestore: Firestore; - private userUid: string; - - private oidcToken: string; - constructor() { this.firestore = getFirestore(firebaseApp); - this.setUser(); } - async setUser() { - this.userUid = undefined; - this.oidcToken = undefined; - - const user = firebaseAuth.currentUser; - if (user) { - this.userUid = user.uid; - this.oidcToken = await user.getIdToken(); - } - } - - async getAccountIdFromOidcToken() { - await this.setUser(); - const recoveryPK = await window.fastAuthController.getUserCredential(this.oidcToken); + static async getAccountIdFromOidcToken() { + const recoveryPK = await window.fastAuthController.getUserCredential(await firebaseAuth.currentUser?.getIdToken()); const accountIds = await fetchAccountIds(recoveryPK); if (!accountIds.length) { @@ -58,7 +41,6 @@ class FirestoreController { accountId?: string; }) { try { - await this.setUser(); const parser = new UAParser(); const device = parser.getDevice(); const os = parser.getOS(); @@ -72,12 +54,12 @@ class FirestoreController { } if (fakPublicKey) { - const fakDoc = setDoc(doc(this.firestore, `/users/${this.userUid}/devices`, fakPublicKey), { + const fakDoc = setDoc(doc(this.firestore, `/users/${firebaseAuth.currentUser?.uid}/devices`, fakPublicKey), { device: `${device.vendor} ${device.model}`, os: `${os.name} ${os.version}`, browser: `${browser.name} ${browser.version}`, publicKeys: [fakPublicKey], - uid: this.userUid, + uid: firebaseAuth.currentUser?.uid, gateway: gateway || 'Unknown Gateway', dateTime, keyType: 'fak', @@ -86,12 +68,12 @@ class FirestoreController { } if (lakPublicKey) { - const lakDoc = setDoc(doc(this.firestore, `/users/${this.userUid}/devices`, lakPublicKey), { + const lakDoc = setDoc(doc(this.firestore, `/users/${firebaseAuth.currentUser?.uid}/devices`, lakPublicKey), { device: `${device.vendor} ${device.model}`, os: `${os.name} ${os.version}`, browser: `${browser.name} ${browser.version}`, publicKeys: [lakPublicKey], - uid: this.userUid, + uid: firebaseAuth.currentUser?.uid, gateway: gateway || 'Unknown Gateway', dateTime, keyType: 'lak', @@ -107,8 +89,7 @@ class FirestoreController { } async listDevices() { - await this.setUser(); - const q = query(collection(this.firestore, `/users/${this.userUid}/devices`) as CollectionReference); + const q = query(collection(this.firestore, `/users/${firebaseAuth.currentUser?.uid}/devices`) as CollectionReference); const querySnapshot = await getDocs(q); const collections = []; @@ -123,18 +104,18 @@ class FirestoreController { }); }); - const existingKeyPair = window.fastAuthController.findInKeyStores(`oidc_keypair_${this.oidcToken}`); + const existingKeyPair = window.fastAuthController.findInKeyStores(`oidc_keypair_${await firebaseAuth.currentUser?.getIdToken()}`); if (!existingKeyPair) { - await (window as any).fastAuthController.claimOidcToken(this.oidcToken); + await (window as any).fastAuthController.claimOidcToken(await firebaseAuth.currentUser?.getIdToken()); } if (!window.fastAuthController.getAccountId()) { - const accountId = await this.getAccountIdFromOidcToken(); + const accountId = await FirestoreController.getAccountIdFromOidcToken(); window.fastAuthController.setAccountId(accountId); } const accessKeysWithoutRecoveryKey = await window.fastAuthController - .getAllAccessKeysExceptRecoveryKey(this.oidcToken); + .getAllAccessKeysExceptRecoveryKey(await firebaseAuth.currentUser?.getIdToken()); // TODO: from the list, exclude record that has same key from recovery service return accessKeysWithoutRecoveryKey.reduce((list, key) => { @@ -158,8 +139,7 @@ class FirestoreController { } async deleteDeviceCollections(list) { - await this.setUser(); - const recoveryPK = await window.fastAuthController.getUserCredential(this.oidcToken); + const recoveryPK = await window.fastAuthController.getUserCredential(await firebaseAuth.currentUser?.getIdToken()); const accountIds = await fetchAccountIds(recoveryPK); // delete firebase records @@ -170,7 +150,7 @@ class FirestoreController { if (firestoreIds.length) { // delete all records except the one that has LAK const deletePromises = firestoreIds.flatMap((id) => [ - deleteDoc(doc(this.firestore, `/users/${this.userUid}/devices`, id)), + deleteDoc(doc(this.firestore, `/users/${firebaseAuth.currentUser?.uid}/devices`, id)), deleteDoc(doc(this.firestore, '/publicKeys', id)) ]); await Promise.allSettled(deletePromises); @@ -185,7 +165,7 @@ class FirestoreController { const publicKeys = list.reduce((acc, curr) => acc.concat(curr.publicKeys), []); const deleteAction = getDeleteKeysAction(publicKeys); await (window as any).fastAuthController.signAndSendActionsWithRecoveryKey({ - oidcToken: this.oidcToken, + oidcToken: await firebaseAuth.currentUser?.getIdToken(), accountId: accountIds[0], recoveryPK, actions: deleteAction @@ -197,8 +177,7 @@ class FirestoreController { } async getDeviceCollection(fakPublicKey) { - await this.setUser(); - const docRef = doc(this.firestore, 'users', this.userUid, 'devices', fakPublicKey); + const docRef = doc(this.firestore, 'users', firebaseAuth.currentUser?.uid, 'devices', fakPublicKey); const docSnap = await getDoc(docRef); if (docSnap.exists()) { @@ -207,10 +186,9 @@ class FirestoreController { return null; } - getUserOidcToken = async () => { - await this.setUser(); - return this.oidcToken; - }; + static async getUserOidcToken() { + return firebaseAuth.currentUser?.getIdToken(); + } async addAccountIdPublicKey(publicKey: string, accountId: string) { await setDoc(doc(this.firestore, 'publicKeys', publicKey), { From 6b00fab35451ee22379c045f8ecd1bb25059479b Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Mon, 12 Feb 2024 10:22:26 +0400 Subject: [PATCH 5/6] Improve typescript --- packages/near-fast-auth-signer/src/lib/controller.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/near-fast-auth-signer/src/lib/controller.ts b/packages/near-fast-auth-signer/src/lib/controller.ts index 9e8e55f0..05529e8b 100644 --- a/packages/near-fast-auth-signer/src/lib/controller.ts +++ b/packages/near-fast-auth-signer/src/lib/controller.ts @@ -284,8 +284,7 @@ class FastAuthController { } } - async getUserCredential(oidcToken) { - // @ts-ignore + async getUserCredential(oidcToken: string) { const GET_USER_SALT = CLAIM + 2; const keypair = await this.getKey(`oidc_keypair_${oidcToken}`) || await this.getLocalStoreKey(`oidc_keypair_${oidcToken}`); From a8d4974c26691722d21ad8d2c5cc08050e02e6da Mon Sep 17 00:00:00 2001 From: Felipe Pessina Date: Mon, 12 Feb 2024 11:46:24 +0400 Subject: [PATCH 6/6] Remove all usages of user.accessToken --- .../src/components/AddDevice/AddDevice.tsx | 6 +++--- packages/near-fast-auth-signer/src/lib/controller.ts | 1 - .../near-fast-auth-signer/src/lib/firestoreController.ts | 2 +- 3 files changed, 4 insertions(+), 5 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 cf95acda..96723af5 100644 --- a/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx +++ b/packages/near-fast-auth-signer/src/components/AddDevice/AddDevice.tsx @@ -12,6 +12,7 @@ import styled from 'styled-components'; import * as yup from 'yup'; import { Button } from '../../lib/Button'; +import FirestoreController from '../../lib/firestoreController'; import Input from '../../lib/Input/Input'; import { openToast } from '../../lib/Toast'; import { useAuthState } from '../../lib/useAuthState'; @@ -173,9 +174,8 @@ function SignInPage() { : null; const existingDeviceLakKey = existingDevice?.publicKeys?.filter((key) => key !== publicKeyFak)[0]; - // @ts-ignore - const oidcToken = user.accessToken; - const recoveryPK = await window.fastAuthController.getUserCredential(oidcToken); + const recoveryPK = await window.fastAuthController + .getUserCredential(await FirestoreController.getUserOidcToken()); // if given lak key is already attached to webAuthN public key, no need to add it again const noNeedToAddKey = existingDeviceLakKey === public_key; diff --git a/packages/near-fast-auth-signer/src/lib/controller.ts b/packages/near-fast-auth-signer/src/lib/controller.ts index 05529e8b..889e0cd4 100644 --- a/packages/near-fast-auth-signer/src/lib/controller.ts +++ b/packages/near-fast-auth-signer/src/lib/controller.ts @@ -174,7 +174,6 @@ class FastAuthController { }); } catch { // fallback, non webAuthN supported browser - // @ts-ignore const oidcToken = await firebaseAuth.currentUser.getIdToken(); const recoveryPK = await this.getUserCredential(oidcToken); // make sure to handle failure, (eg token expired) if fail, redirect to failure_url diff --git a/packages/near-fast-auth-signer/src/lib/firestoreController.ts b/packages/near-fast-auth-signer/src/lib/firestoreController.ts index 91cc7e57..f20696cd 100644 --- a/packages/near-fast-auth-signer/src/lib/firestoreController.ts +++ b/packages/near-fast-auth-signer/src/lib/firestoreController.ts @@ -106,7 +106,7 @@ class FirestoreController { const existingKeyPair = window.fastAuthController.findInKeyStores(`oidc_keypair_${await firebaseAuth.currentUser?.getIdToken()}`); if (!existingKeyPair) { - await (window as any).fastAuthController.claimOidcToken(await firebaseAuth.currentUser?.getIdToken()); + await window.fastAuthController.claimOidcToken(await firebaseAuth.currentUser?.getIdToken()); } if (!window.fastAuthController.getAccountId()) {