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

fix: throw if no account exists #102

Merged
merged 4 commits into from
Nov 1, 2023
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
17 changes: 9 additions & 8 deletions src/components/AddDevice/AddDevice.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createKey, isPassKeyAvailable } from '@near-js/biometric-ed25519/lib';
import BN from 'bn.js';
import { sendSignInLinkToEmail } from 'firebase/auth';
import { fetchSignInMethodsForEmail, sendSignInLinkToEmail } from 'firebase/auth';
import React, { useCallback, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { useNavigate, useSearchParams } from 'react-router-dom';
Expand All @@ -10,7 +10,7 @@ import { Button } from '../../lib/Button';
import FirestoreController from '../../lib/firestoreController';
import { openToast } from '../../lib/Toast';
import { useAuthState } from '../../lib/useAuthState';
import { decodeIfTruthy, inIframe } from '../../utils';
import { decodeIfTruthy, inIframe, redirectWithError } from '../../utils';
import { basePath } from '../../utils/config';
import { checkFirestoreReady, firebaseAuth } from '../../utils/firebase';
import { isValidEmail } from '../../utils/form-validation';
Expand Down Expand Up @@ -138,6 +138,10 @@ function SignInPage() {
const methodNames = searchParams.get('methodNames');

try {
const result = await fetchSignInMethodsForEmail(firebaseAuth, data.email);
if (!result.length) {
throw new Error('Account not found, please create an account and try again');
}
const { publicKey: publicKeyFak, email, privateKey } = await handleCreateAccount({
accountId: null,
email: data.email,
Expand All @@ -162,6 +166,7 @@ function SignInPage() {
navigate(`/verify-email?${newSearchParams.toString()}#${hashParams.toString()}`);
} catch (error: any) {
console.log(error);
redirectWithError({ success_url, failure_url, error });

if (typeof error?.message === 'string') {
openToast({
Expand Down Expand Up @@ -265,14 +270,10 @@ function SignInPage() {
});
}).catch((error) => {
console.log('error', error);
const { message } = error;
const parsedUrl = new URL(failure_url || success_url || window.location.origin + (basePath ? `/${basePath}` : ''));
parsedUrl.searchParams.set('code', error.code);
parsedUrl.searchParams.set('reason', message);
window.location.replace(parsedUrl.href);
redirectWithError({ success_url, failure_url, error });
openToast({
type: 'ERROR',
title: message,
title: error.message,
});
});
} else if (email && !authenticated) {
Expand Down
13 changes: 6 additions & 7 deletions src/components/AuthCallback/AuthCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styled from 'styled-components';
import FastAuthController from '../../lib/controller';
import FirestoreController from '../../lib/firestoreController';
import { openToast } from '../../lib/Toast';
import { decodeIfTruthy, inIframe } from '../../utils';
import { decodeIfTruthy, inIframe, redirectWithError } from '../../utils';
import { basePath, network, networkId } from '../../utils/config';
import { checkFirestoreReady, firebaseAuth } from '../../utils/firebase';
import {
Expand Down Expand Up @@ -135,6 +135,9 @@ export const onSignIn = async ({
throw new Error('Unable to retrieve account Id');
});

if (!accountIds.length) {
throw new Error('Account not found, please create an account and try again');
}
// TODO: If we want to remove old LAK automatically, use below code and add deleteKeyActions to signAndSendActionsWithRecoveryKey
// const existingDevice = await window.firestoreController.getDeviceCollection(publicKeyFak);
// // delete old lak key attached to webAuthN public Key
Expand Down Expand Up @@ -283,14 +286,10 @@ function AuthCallbackPage() {
}
}).catch((e) => {
console.log('error:', e);
const { message } = e;
const parsedUrl = new URL(failure_url || success_url || window.location.origin);
parsedUrl.searchParams.set('code', e.code);
parsedUrl.searchParams.set('reason', message);
window.location.replace(parsedUrl.href);
redirectWithError({ success_url, failure_url, error: e });
openToast({
type: 'ERROR',
title: message,
title: e.message,
});
});
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/components/CreateAccount/CreateAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import FormContainer from './styles/FormContainer';
import InputContainer from './styles/InputContainer';
import { Button } from '../../lib/Button';
import { openToast } from '../../lib/Toast';
import { inIframe } from '../../utils';
import { inIframe, redirectWithError } from '../../utils';
import { network } from '../../utils/config';
import {
accountAddressPatternNoSubaccount, emailPattern, getEmailId, isValidEmail
Expand Down Expand Up @@ -87,6 +87,7 @@ function CreateAccount() {
navigate(`/verify-email?${newSearchParams.toString()}#${hashParams.toString()}`);
} catch (error: any) {
console.log('error', error);
redirectWithError({ success_url, failure_url, error });
// currently running handleCreateAccount() will throw an error as:
// error DOMException: The following credential operations can only occur in a document which is same-origin with all of its ancestors: storage/retrieval of 'PasswordCredential' and 'FederatedCredential', storage of 'PublicKeyCredential'.

Expand Down
5 changes: 2 additions & 3 deletions src/components/Sign/Sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ArrowUpSvg from '../../Images/arrow-up';
import InternetSvg from '../../Images/Internet';
import { Button } from '../../lib/Button';
import { useAuthState } from '../../lib/useAuthState';
import { redirectWithError } from '../../utils';
import { basePath, network } from '../../utils/config';
import TableContent from '../TableContent/TableContent';

Expand Down Expand Up @@ -181,9 +182,7 @@ function Sign() {
signedTransactions.push(base64);
} catch (err) {
const failure_url = searchParams.get('failure_url');
const parsedUrl = new URL(failure_url || success_url || window.location.origin + (basePath ? `/${basePath}` : ''));
parsedUrl.searchParams.set('message', err);
window.location.replace(parsedUrl.href);
redirectWithError({ success_url, failure_url, error: err });
window.parent.postMessage({ signedDelegates: '', error: err.message }, '*');
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/VerifyEmail/verify-email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import styled from 'styled-components';
import EmailSvg from './icons/EmailSvg';
import { Button } from '../../lib/Button';
import { openToast } from '../../lib/Toast';
import { redirectWithError } from '../../utils';
import { basePath } from '../../utils/config';
import { firebaseAuth } from '../../utils/firebase';

Expand Down Expand Up @@ -112,6 +113,7 @@ function VerifyEmailPage() {
});
} catch (error: any) {
console.log(error);
redirectWithError({ success_url, failure_url, error });

if (typeof error?.message === 'string') {
openToast({
Expand Down
13 changes: 13 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { basePath } from './config';

/* eslint-disable import/prefer-default-export */
export function inIframe() {
try {
Expand All @@ -14,3 +16,14 @@ export const decodeIfTruthy = (paramVal) => {

return paramVal;
};

export const redirectWithError = ({
failure_url,
success_url,
error
}: { failure_url: string; success_url: string; error: Error }): void => {
const { message } = error;
const parsedUrl = new URL(failure_url || success_url || window.location.origin + (basePath ? `/${basePath}` : ''));
parsedUrl.searchParams.set('reason', message);
window.location.replace(parsedUrl.href);
};