2022-09-09 07:44:52 +03:00
|
|
|
import { Alert, Button, Input, Space, Spin, Collapse } from 'antd';
|
|
|
|
import React, { FC, useState } from 'react';
|
2023-01-16 09:31:36 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
2022-09-09 07:44:52 +03:00
|
|
|
import styles from './FediAuthModal.module.scss';
|
2023-04-17 00:17:13 +03:00
|
|
|
import { isValidFediverseAccount } from '../../../utils/validators';
|
2022-04-28 09:19:20 +03:00
|
|
|
|
2022-09-09 07:44:52 +03:00
|
|
|
const { Panel } = Collapse;
|
|
|
|
|
2023-01-16 09:31:36 +03:00
|
|
|
// Lazy loaded components
|
|
|
|
|
|
|
|
const CheckCircleOutlined = dynamic(() => import('@ant-design/icons/CheckCircleOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2022-09-09 07:44:52 +03:00
|
|
|
export type FediAuthModalProps = {
|
|
|
|
authenticated: boolean;
|
|
|
|
displayName: string;
|
|
|
|
accessToken: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const FediAuthModal: FC<FediAuthModalProps> = ({
|
|
|
|
authenticated,
|
|
|
|
displayName,
|
|
|
|
accessToken,
|
|
|
|
}) => {
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [valid, setValid] = useState(false);
|
|
|
|
const [account, setAccount] = useState('');
|
|
|
|
const [code, setCode] = useState('');
|
2022-09-09 08:23:43 +03:00
|
|
|
const [verifyingCode, setVerifyingCode] = useState(false);
|
2022-09-09 07:44:52 +03:00
|
|
|
|
|
|
|
const message = !authenticated ? (
|
|
|
|
<span>
|
|
|
|
Receive a direct message on the Fediverse to link your account to{' '}
|
|
|
|
<strong>{displayName}</strong>, or login as a previously linked chat user.
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span>
|
|
|
|
<b>You are already authenticated</b>. However, you can add other domains or log in as a
|
|
|
|
different user.
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
|
|
|
let errorMessageText = errorMessage;
|
|
|
|
if (errorMessageText) {
|
|
|
|
if (errorMessageText.includes('url does not support indieauth')) {
|
|
|
|
errorMessageText = 'The provided URL is either invalid or does not support IndieAuth.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const validate = (acct: string) => {
|
2023-04-17 00:17:13 +03:00
|
|
|
setValid(isValidFediverseAccount(acct));
|
2022-09-09 07:44:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const onInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setAccount(e.target.value);
|
|
|
|
validate(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeRequest = async (url, data) => {
|
|
|
|
const rawResponse = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
});
|
|
|
|
|
|
|
|
const content = await rawResponse.json();
|
|
|
|
if (content.message) {
|
|
|
|
setErrorMessage(content.message);
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const submitCodePressed = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
const url = `/api/auth/fediverse/verify?accessToken=${accessToken}`;
|
|
|
|
const data = { code };
|
|
|
|
|
|
|
|
try {
|
|
|
|
await makeRequest(url, data);
|
|
|
|
|
|
|
|
// Success. Reload the page.
|
|
|
|
window.location.href = '/';
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setErrorMessage(e);
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const submitAccountPressed = async () => {
|
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
const url = `/api/auth/fediverse?accessToken=${accessToken}`;
|
|
|
|
const normalizedAccount = account.replace(/^@+/, '');
|
|
|
|
const data = { account: normalizedAccount };
|
|
|
|
|
|
|
|
try {
|
|
|
|
await makeRequest(url, data);
|
|
|
|
setVerifyingCode(true);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setErrorMessage(e);
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const inputCodeStep = (
|
|
|
|
<div>
|
|
|
|
Paste in the code that was sent to your Fediverse account. If you did not receive a code, make
|
|
|
|
sure you can accept direct messages.
|
|
|
|
<div className={styles.codeInputContainer}>
|
|
|
|
<Input
|
|
|
|
value={code}
|
|
|
|
onChange={e => setCode(e.target.value)}
|
|
|
|
className={styles.codeInput}
|
|
|
|
placeholder="123456"
|
|
|
|
maxLength={6}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
onClick={submitCodePressed}
|
|
|
|
disabled={code.length < 6}
|
|
|
|
className={styles.submitButton}
|
|
|
|
>
|
|
|
|
Verify Code
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
const inputAccountStep = (
|
|
|
|
<>
|
|
|
|
<div>Your Fediverse Account</div>
|
|
|
|
<Input.Search
|
|
|
|
addonBefore="@"
|
|
|
|
onInput={onInput}
|
|
|
|
value={account}
|
|
|
|
placeholder="youraccount@yourserver.com"
|
|
|
|
status={!valid && account.length > 0 ? 'error' : undefined}
|
2022-10-27 00:35:46 +03:00
|
|
|
onSearch={submitAccountPressed}
|
2022-11-29 09:41:33 +03:00
|
|
|
enterButton={
|
|
|
|
<Button type={valid ? 'primary' : 'default'} disabled={!valid || account.length === 0}>
|
|
|
|
<CheckCircleOutlined />
|
|
|
|
</Button>
|
|
|
|
}
|
2022-09-09 07:44:52 +03:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Spin spinning={loading}>
|
|
|
|
<Space direction="vertical">
|
|
|
|
{message}
|
|
|
|
{errorMessageText && (
|
|
|
|
<Alert message="Error" description={errorMessageText} type="error" showIcon />
|
|
|
|
)}
|
|
|
|
{verifyingCode ? inputCodeStep : inputAccountStep}
|
|
|
|
<Collapse ghost>
|
|
|
|
<Panel
|
|
|
|
key="header"
|
|
|
|
header="Learn more about using the Fediverse to authenticate with chat."
|
|
|
|
>
|
2022-12-18 01:05:11 +03:00
|
|
|
<p>
|
|
|
|
You can link your chat identity with your Fediverse identity. Next time you want to
|
|
|
|
use this chat identity you can again go through the Fediverse authentication.
|
|
|
|
</p>
|
2022-09-09 07:44:52 +03:00
|
|
|
</Panel>
|
|
|
|
</Collapse>
|
|
|
|
<div>
|
|
|
|
<strong>Note</strong>: This is for authentication purposes only, and no personal
|
|
|
|
information will be accessed or stored.
|
|
|
|
</div>
|
|
|
|
</Space>
|
|
|
|
</Spin>
|
|
|
|
);
|
|
|
|
};
|