From 470511acbb48f9ac5367289b772d4790734c89c7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 19 Feb 2024 16:24:11 -0600 Subject: [PATCH] IdentityStep: prevent entry of nsec --- .../steps/identity-step.tsx | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx b/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx index 91c12711d..64f8a1814 100644 --- a/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx +++ b/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { FormattedMessage } from 'react-intl'; +import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; import { accountLookup } from 'soapbox/actions/accounts'; import { Button, Form, FormGroup, HStack, Input, Stack, Modal } from 'soapbox/components/ui'; @@ -15,40 +15,44 @@ interface IIdentityStep { onClose(): void; } +const messages = defineMessages({ + notFound: { id: 'nostr_signin.identity.not_found', defaultMessage: 'Account not found' }, + nsec: { id: 'nostr_signin.identity.nsec', defaultMessage: 'Enter your public key' }, +}); + const IdentityStep: React.FC = ({ setAccountId, setStep, onClose }) => { + const intl = useIntl(); const dispatch = useAppDispatch(); + const [error, setError] = useState(); const [loading, setLoading] = useState(false); - const [notFound, setNotFound] = useState(false); const [username, setUsername] = useState(''); const handleChangeUsername: React.ChangeEventHandler = (e) => { - setNotFound(false); + setError(undefined); setUsername(e.target.value); }; const handleSubmit = async () => { setLoading(true); - await dispatch(accountLookup(username)) - .then((account) => { - setAccountId(account.id); - setStep('account'); - setNotFound(false); - setLoading(false); - }) - .catch((error) => { - if (error.response?.status === 404) { - setNotFound(true); - } - setLoading(false); - }); - }; + if (username.startsWith('nsec1')) { + setError(intl.formatMessage(messages.nsec)); + setLoading(false); + return; + } - const errors: string[] = []; - if (notFound) { - errors.push('Account not found'); - } + try { + const account = await dispatch(accountLookup(username)); + setAccountId(account.id); + setStep('account'); + } catch (e: any) { + if (e.response?.status === 404) { + setError(intl.formatMessage(messages.notFound)); + } + setLoading(false); + } + }; return ( } onClose={onClose}> @@ -60,7 +64,7 @@ const IdentityStep: React.FC = ({ setAccountId, setStep, onClose - + = ({ setAccountId, setStep, onClose