IdentityStep: prevent entry of nsec
This commit is contained in:
parent
3157969645
commit
470511acbb
1 changed files with 27 additions and 23 deletions
|
@ -1,5 +1,5 @@
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
|
||||||
|
|
||||||
import { accountLookup } from 'soapbox/actions/accounts';
|
import { accountLookup } from 'soapbox/actions/accounts';
|
||||||
import { Button, Form, FormGroup, HStack, Input, Stack, Modal } from 'soapbox/components/ui';
|
import { Button, Form, FormGroup, HStack, Input, Stack, Modal } from 'soapbox/components/ui';
|
||||||
|
@ -15,40 +15,44 @@ interface IIdentityStep {
|
||||||
onClose(): void;
|
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<IIdentityStep> = ({ setAccountId, setStep, onClose }) => {
|
const IdentityStep: React.FC<IIdentityStep> = ({ setAccountId, setStep, onClose }) => {
|
||||||
|
const intl = useIntl();
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
const [error, setError] = useState<string>();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [notFound, setNotFound] = useState(false);
|
|
||||||
const [username, setUsername] = useState('');
|
const [username, setUsername] = useState('');
|
||||||
|
|
||||||
const handleChangeUsername: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
const handleChangeUsername: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
||||||
setNotFound(false);
|
setError(undefined);
|
||||||
setUsername(e.target.value);
|
setUsername(e.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
await dispatch(accountLookup(username))
|
if (username.startsWith('nsec1')) {
|
||||||
.then((account) => {
|
setError(intl.formatMessage(messages.nsec));
|
||||||
setAccountId(account.id);
|
setLoading(false);
|
||||||
setStep('account');
|
return;
|
||||||
setNotFound(false);
|
}
|
||||||
setLoading(false);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
if (error.response?.status === 404) {
|
|
||||||
setNotFound(true);
|
|
||||||
}
|
|
||||||
setLoading(false);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const errors: string[] = [];
|
try {
|
||||||
if (notFound) {
|
const account = await dispatch(accountLookup(username));
|
||||||
errors.push('Account not found');
|
setAccountId(account.id);
|
||||||
}
|
setStep('account');
|
||||||
|
} catch (e: any) {
|
||||||
|
if (e.response?.status === 404) {
|
||||||
|
setError(intl.formatMessage(messages.notFound));
|
||||||
|
}
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal title={<FormattedMessage id='nostr_signin.identity.title' defaultMessage='Who are you?' />} onClose={onClose}>
|
<Modal title={<FormattedMessage id='nostr_signin.identity.title' defaultMessage='Who are you?' />} onClose={onClose}>
|
||||||
|
@ -60,7 +64,7 @@ const IdentityStep: React.FC<IIdentityStep> = ({ setAccountId, setStep, onClose
|
||||||
|
|
||||||
<EmojiGraphic emoji='🕵️' />
|
<EmojiGraphic emoji='🕵️' />
|
||||||
|
|
||||||
<FormGroup labelText='Username' errors={errors}>
|
<FormGroup labelText='Username' errors={error ? [error] : []}>
|
||||||
<Input
|
<Input
|
||||||
icon={require('@tabler/icons/at.svg')}
|
icon={require('@tabler/icons/at.svg')}
|
||||||
placeholder='Username or npub'
|
placeholder='Username or npub'
|
||||||
|
@ -76,7 +80,7 @@ const IdentityStep: React.FC<IIdentityStep> = ({ setAccountId, setStep, onClose
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
theme='accent'
|
theme='accent'
|
||||||
type='submit' disabled={!username || loading || notFound}
|
type='submit' disabled={!username || loading || !!error}
|
||||||
onClick={handleSubmit}
|
onClick={handleSubmit}
|
||||||
>
|
>
|
||||||
Next
|
Next
|
||||||
|
|
Loading…
Reference in a new issue