2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-08-16 21:03:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-11-30 17:51:53 +00:00
|
|
|
import type { ChangeEvent } from 'react';
|
|
|
|
import React, { useState, useEffect, useCallback, useRef } from 'react';
|
|
|
|
import type { Plugin } from 'intl-tel-input';
|
|
|
|
import intlTelInput from 'intl-tel-input';
|
|
|
|
|
|
|
|
import { strictAssert } from '../util/assert';
|
2023-04-06 21:49:24 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-06-01 17:48:16 +00:00
|
|
|
import { parseNumber } from '../util/libphonenumberUtil';
|
2021-11-30 17:51:53 +00:00
|
|
|
import { getChallengeURL } from '../challenge';
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
function PhoneInput({
|
2021-11-30 17:51:53 +00:00
|
|
|
onValidation,
|
|
|
|
onNumberChange,
|
|
|
|
}: {
|
|
|
|
onValidation: (isValid: boolean) => void;
|
|
|
|
onNumberChange: (number?: string) => void;
|
2022-11-18 00:45:19 +00:00
|
|
|
}): JSX.Element {
|
2021-11-30 17:51:53 +00:00
|
|
|
const [isValid, setIsValid] = useState(false);
|
|
|
|
const pluginRef = useRef<Plugin | undefined>();
|
|
|
|
const elemRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
|
|
|
|
const onRef = useCallback((elem: HTMLInputElement | null) => {
|
|
|
|
elemRef.current = elem;
|
|
|
|
|
|
|
|
if (!elem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginRef.current?.destroy();
|
|
|
|
|
|
|
|
const plugin = intlTelInput(elem);
|
|
|
|
pluginRef.current = plugin;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const validateNumber = useCallback(
|
|
|
|
(number: string) => {
|
|
|
|
const { current: plugin } = pluginRef;
|
|
|
|
if (!plugin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const regionCode = plugin.getSelectedCountryData().iso2;
|
|
|
|
|
2022-06-01 17:48:16 +00:00
|
|
|
const parsedNumber = parseNumber(number, regionCode);
|
2021-11-30 17:51:53 +00:00
|
|
|
|
|
|
|
setIsValid(parsedNumber.isValidNumber);
|
|
|
|
onValidation(parsedNumber.isValidNumber);
|
|
|
|
|
|
|
|
onNumberChange(
|
|
|
|
parsedNumber.isValidNumber ? parsedNumber.e164 : undefined
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[setIsValid, onNumberChange, onValidation]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onChange = useCallback(
|
|
|
|
(_: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
if (elemRef.current) {
|
|
|
|
validateNumber(elemRef.current.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[validateNumber]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onKeyDown = useCallback(
|
|
|
|
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
// Pacify TypeScript and handle events bubbling up
|
|
|
|
if (event.target instanceof HTMLInputElement) {
|
|
|
|
validateNumber(event.target.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[validateNumber]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="phone-input">
|
|
|
|
<div className="phone-input-form">
|
|
|
|
<div className={`number-container ${isValid ? 'valid' : 'invalid'}`}>
|
|
|
|
<input
|
|
|
|
className="number"
|
|
|
|
type="tel"
|
|
|
|
ref={onRef}
|
|
|
|
onChange={onChange}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
placeholder="Phone Number"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|
2021-11-30 17:51:53 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function StandaloneRegistration({
|
2021-11-30 17:51:53 +00:00
|
|
|
onComplete,
|
|
|
|
requestVerification,
|
|
|
|
registerSingleDevice,
|
|
|
|
}: {
|
|
|
|
onComplete: () => void;
|
|
|
|
requestVerification: (
|
|
|
|
type: 'sms' | 'voice',
|
|
|
|
number: string,
|
|
|
|
token: string
|
|
|
|
) => Promise<void>;
|
|
|
|
registerSingleDevice: (number: string, code: string) => Promise<void>;
|
2022-11-18 00:45:19 +00:00
|
|
|
}): JSX.Element {
|
2021-11-30 17:51:53 +00:00
|
|
|
useEffect(() => {
|
2023-01-13 00:24:59 +00:00
|
|
|
window.IPC.readyForUpdates();
|
2021-11-30 17:51:53 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const [isValidNumber, setIsValidNumber] = useState(false);
|
|
|
|
const [isValidCode, setIsValidCode] = useState(false);
|
|
|
|
const [number, setNumber] = useState<string | undefined>(undefined);
|
|
|
|
const [code, setCode] = useState('');
|
|
|
|
const [error, setError] = useState<string | undefined>(undefined);
|
|
|
|
const [status, setStatus] = useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
const onRequestCode = useCallback(
|
|
|
|
async (type: 'sms' | 'voice') => {
|
|
|
|
if (!isValidNumber) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!number) {
|
|
|
|
setIsValidNumber(false);
|
|
|
|
setError(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:49:24 +00:00
|
|
|
const url = getChallengeURL('registration');
|
|
|
|
log.info(`StandaloneRegistration: navigating to ${url}`);
|
|
|
|
document.location.href = url;
|
2022-06-13 21:39:35 +00:00
|
|
|
if (!window.Signal.challengeHandler) {
|
|
|
|
setError('Captcha handler is not ready!');
|
|
|
|
return;
|
|
|
|
}
|
2022-09-15 00:31:37 +00:00
|
|
|
const token = await window.Signal.challengeHandler.requestCaptcha({
|
|
|
|
reason: 'standalone registration',
|
|
|
|
});
|
2021-11-30 17:51:53 +00:00
|
|
|
|
|
|
|
try {
|
2022-12-21 18:41:48 +00:00
|
|
|
void requestVerification(type, number, token);
|
2021-11-30 17:51:53 +00:00
|
|
|
setError(undefined);
|
|
|
|
} catch (err) {
|
|
|
|
setError(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[isValidNumber, setIsValidNumber, setError, requestVerification, number]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onSMSClick = useCallback(
|
|
|
|
(e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void onRequestCode('sms');
|
2021-11-30 17:51:53 +00:00
|
|
|
},
|
|
|
|
[onRequestCode]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onVoiceClick = useCallback(
|
|
|
|
(e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void onRequestCode('voice');
|
2021-11-30 17:51:53 +00:00
|
|
|
},
|
|
|
|
[onRequestCode]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onChangeCode = useCallback(
|
|
|
|
(event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const { value } = event.target;
|
|
|
|
|
|
|
|
setIsValidCode(value.length === 6);
|
|
|
|
setCode(value);
|
|
|
|
},
|
|
|
|
[setIsValidCode, setCode]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onVerifyCode = useCallback(
|
|
|
|
async (event: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
if (!isValidNumber || !isValidCode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:40:50 +00:00
|
|
|
strictAssert(number != null && code.length > 0, 'Missing number or code');
|
2021-11-30 17:51:53 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await registerSingleDevice(number, code);
|
|
|
|
onComplete();
|
|
|
|
} catch (err) {
|
|
|
|
setStatus(err.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[
|
|
|
|
registerSingleDevice,
|
|
|
|
onComplete,
|
|
|
|
number,
|
|
|
|
code,
|
|
|
|
setStatus,
|
|
|
|
isValidNumber,
|
|
|
|
isValidCode,
|
|
|
|
]
|
|
|
|
);
|
2021-06-14 19:01:00 +00:00
|
|
|
|
|
|
|
return (
|
2021-11-30 17:51:53 +00:00
|
|
|
<div className="full-screen-flow">
|
|
|
|
<div className="module-title-bar-drag-area" />
|
|
|
|
|
|
|
|
<div className="step">
|
|
|
|
<div className="inner">
|
|
|
|
<div className="step-body">
|
|
|
|
<div className="banner-image module-splash-screen__logo module-img--128" />
|
|
|
|
<div className="header">Create your Signal Account</div>
|
|
|
|
<div>
|
|
|
|
<div className="phone-input-form">
|
|
|
|
<PhoneInput
|
|
|
|
onValidation={setIsValidNumber}
|
|
|
|
onNumberChange={setNumber}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="clearfix">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="button"
|
|
|
|
disabled={!isValidNumber}
|
|
|
|
onClick={onSMSClick}
|
|
|
|
>
|
|
|
|
Send SMS
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="link"
|
|
|
|
tabIndex={-1}
|
|
|
|
disabled={!isValidNumber}
|
|
|
|
onClick={onVoiceClick}
|
|
|
|
>
|
|
|
|
Call
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<input
|
|
|
|
className={`form-control ${isValidCode ? 'valid' : 'invalid'}`}
|
|
|
|
type="text"
|
2023-04-20 17:03:43 +00:00
|
|
|
dir="auto"
|
2021-11-30 17:51:53 +00:00
|
|
|
pattern="\s*[0-9]{3}-?[0-9]{3}\s*"
|
|
|
|
title="Enter your 6-digit verification code. If you did not receive a code, click Call or Send SMS to request a new one"
|
|
|
|
placeholder="Verification Code"
|
|
|
|
autoComplete="off"
|
|
|
|
value={code}
|
|
|
|
onChange={onChangeCode}
|
|
|
|
/>
|
|
|
|
<div>{error}</div>
|
|
|
|
<div>{status}</div>
|
|
|
|
</div>
|
|
|
|
<div className="nav">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="button"
|
|
|
|
disabled={!isValidNumber || !isValidCode}
|
|
|
|
onClick={onVerifyCode}
|
|
|
|
>
|
|
|
|
Register
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-06-14 19:01:00 +00:00
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|