From ca9c114067e22b1168010d2cceff4351fa25d53b Mon Sep 17 00:00:00 2001 From: Joep Meindertsma Date: Tue, 3 Jan 2023 12:46:18 +0100 Subject: [PATCH] Add-pubkey modal, refactor register comps #238 --- .../src/components/Dialog/useDialog.tsx | 1 - .../src/components/RegisterSignIn.tsx | 196 +++++++++++++----- data-browser/src/views/ErrorPage.tsx | 5 + lib/src/authentication.ts | 37 +++- 4 files changed, 189 insertions(+), 50 deletions(-) diff --git a/data-browser/src/components/Dialog/useDialog.tsx b/data-browser/src/components/Dialog/useDialog.tsx index bdc1c098b..476dfd316 100644 --- a/data-browser/src/components/Dialog/useDialog.tsx +++ b/data-browser/src/components/Dialog/useDialog.tsx @@ -23,7 +23,6 @@ export const useDialog = (): UseDialogReturnType => { }, []); const close = useCallback(() => { - console.log('close', close); setShowDialog(false); }, []); diff --git a/data-browser/src/components/RegisterSignIn.tsx b/data-browser/src/components/RegisterSignIn.tsx index 34da36fae..9201e3afc 100644 --- a/data-browser/src/components/RegisterSignIn.tsx +++ b/data-browser/src/components/RegisterSignIn.tsx @@ -8,7 +8,13 @@ import { import React, { FormEvent, useCallback, useEffect, useState } from 'react'; import { useSettings } from '../helpers/AppSettings'; import { Button } from './Button'; -import { nameRegex, register, useServerURL, useStore } from '@tomic/react'; +import { + addPublicKey, + nameRegex, + register as createRegistration, + useServerURL, + useStore, +} from '@tomic/react'; import Field from './forms/Field'; import { InputWrapper, InputStyled } from './forms/InputStyles'; import { Row } from './Row'; @@ -20,6 +26,16 @@ interface RegisterSignInProps { redirect?: string; } +/** What is currently showing */ +enum PageStateOpts { + none, + signIn, + register, + reset, + mailSentRegistration, + mailSentAddPubkey, +} + /** * Two buttons: Register / Sign in. * Opens a Dialog / Modal with the appropriate form. @@ -29,7 +45,8 @@ export function RegisterSignIn({ }: React.PropsWithChildren): JSX.Element { const { dialogProps, show, close } = useDialog(); const { agent } = useSettings(); - const [isRegistering, setRegister] = useState(true); + const [pageState, setPageState] = useState(PageStateOpts.none); + const [email, setEmail] = useState(''); if (agent) { return <>{children}; @@ -39,7 +56,7 @@ export function RegisterSignIn({ - {isRegistering ? : } + {pageState === PageStateOpts.register && ( + + )} + {pageState === PageStateOpts.signIn && ( + + )} + {pageState === PageStateOpts.reset && ( + + )} + {pageState === PageStateOpts.mailSentRegistration && ( + + )} + {pageState === PageStateOpts.mailSentAddPubkey && ( + + )} ); } -function Register({ close }) { +function Reset({ email, setEmail, setPageState }) { + const store = useStore(); + const [err, setErr] = useState(undefined); + + const handleRequestReset = useCallback(async () => { + try { + await addPublicKey(store, email); + setPageState(PageStateOpts.mailSentAddPubkey); + } catch (e) { + setErr(e); + } + }, [email]); + + return ( + <> + +

Reset your PassKey

+
+ +

+ { + "Lost it? No worries, we'll send a link that let's you create a new one." + } +

+ { + setErr(undefined); + setEmail(e); + }} + /> + {err && {err.message}} +
+ + + + + ); +} + +function MailSentConfirm({ email, close, message }) { + return ( + <> + +

Go to your email inbox

+
+ +

+ {"We've sent a confirmation link to "} + {email} + {'.'} +

+

{message}

+
+ + + + + ); +} + +function Register({ setPageState, email, setEmail }) { const [name, setName] = useState(''); - const [email, setEmail] = useState(''); const [serverUrlStr] = useServerURL(); const [nameErr, setErr] = useState(undefined); const store = useStore(); - const [mailSent, setMailSent] = useState(false); const serverUrl = new URL(serverUrlStr); serverUrl.host = `${name}.${serverUrl.host}`; @@ -93,8 +199,8 @@ function Register({ close }) { } try { - await register(store, name, email); - setMailSent(true); + await createRegistration(store, name, email); + setPageState(PageStateOpts.mailSentRegistration); } catch (er) { setErr(er); } @@ -102,27 +208,6 @@ function Register({ close }) { [name, email], ); - if (mailSent) { - return ( - <> - -

Go to your email inbox

-
- -

- {"We've sent a confirmation link to "} - {email} - {'.'} -

-

Your account will be created when you open that link.

-
- - - - - ); - } - return ( <> @@ -147,48 +232,63 @@ function Register({ close }) { /> - - - { - setEmail(e.target.value); - }} - /> - - + {name && nameErr && {nameErr.message}} + ); } -function SignIn() { +function SignIn({ setPageState }) { return ( <> -

Sign in

+

Sign in

-

Lost your passphrase?

+ +
); } + +function EmailField({ setEmail, email }) { + return ( + + + { + setEmail(e.target.value); + }} + /> + + + ); +} diff --git a/data-browser/src/views/ErrorPage.tsx b/data-browser/src/views/ErrorPage.tsx index 644ecc19a..15dec5d9a 100644 --- a/data-browser/src/views/ErrorPage.tsx +++ b/data-browser/src/views/ErrorPage.tsx @@ -33,6 +33,11 @@ function ErrorPage({ resource }: ResourcePageProps): JSX.Element {

Unauthorized

{agent ? ( <> +

+ { + "You don't have access to this. Try asking for access, or sign in with a different account." + } +