You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
517 lines
14 KiB
TypeScript
517 lines
14 KiB
TypeScript
5 years ago
|
import React from 'react';
|
||
|
|
||
5 years ago
|
import {
|
||
|
SessionButton,
|
||
|
SessionButtonColor,
|
||
|
SessionButtonType,
|
||
4 years ago
|
} from '../SessionButton';
|
||
|
import { trigger } from '../../../shims/events';
|
||
|
import { StringUtils, ToastUtils, UserUtils } from '../../../session/utils';
|
||
|
import { ConversationController } from '../../../session/conversations';
|
||
|
import { PasswordUtil } from '../../../util';
|
||
|
import { removeAll } from '../../../data/data';
|
||
|
import { SignUpMode, SignUpTab } from './SignUpTab';
|
||
|
import { SignInMode } from './SignInTab';
|
||
|
import { RegistrationUserDetails } from './RegistrationUserDetails';
|
||
|
import { TermsAndConditions } from './TermsAndConditions';
|
||
|
import { TabLabel, TabType } from './TabLabel';
|
||
5 years ago
|
|
||
4 years ago
|
export const MAX_USERNAME_LENGTH = 20;
|
||
5 years ago
|
|
||
5 years ago
|
interface State {
|
||
5 years ago
|
selectedTab: TabType;
|
||
5 years ago
|
signInMode: SignInMode;
|
||
5 years ago
|
signUpMode: SignUpMode;
|
||
5 years ago
|
secretWords: string | undefined;
|
||
5 years ago
|
displayName: string;
|
||
|
password: string;
|
||
|
validatePassword: string;
|
||
5 years ago
|
passwordErrorString: string;
|
||
|
passwordFieldsMatch: boolean;
|
||
5 years ago
|
recoveryPhrase: string;
|
||
|
generatedRecoveryPhrase: string;
|
||
5 years ago
|
hexGeneratedPubKey: string;
|
||
5 years ago
|
mnemonicError: string | undefined;
|
||
|
displayNameError: string | undefined;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
export class RegistrationTabs extends React.Component<any, State> {
|
||
5 years ago
|
constructor(props: any) {
|
||
|
super(props);
|
||
|
|
||
|
this.onSeedChanged = this.onSeedChanged.bind(this);
|
||
|
this.onDisplayNameChanged = this.onDisplayNameChanged.bind(this);
|
||
|
this.onPasswordChanged = this.onPasswordChanged.bind(this);
|
||
|
this.onPasswordVerifyChanged = this.onPasswordVerifyChanged.bind(this);
|
||
5 years ago
|
this.handlePressEnter = this.handlePressEnter.bind(this);
|
||
|
this.handleContinueYourSessionClick = this.handleContinueYourSessionClick.bind(
|
||
|
this
|
||
|
);
|
||
4 years ago
|
this.onCompleteSignUpClick = this.onCompleteSignUpClick.bind(this);
|
||
5 years ago
|
|
||
|
this.state = {
|
||
4 years ago
|
selectedTab: TabType.SignUp,
|
||
5 years ago
|
signInMode: SignInMode.Default,
|
||
5 years ago
|
signUpMode: SignUpMode.Default,
|
||
5 years ago
|
secretWords: undefined,
|
||
5 years ago
|
displayName: '',
|
||
|
password: '',
|
||
|
validatePassword: '',
|
||
5 years ago
|
passwordErrorString: '',
|
||
|
passwordFieldsMatch: false,
|
||
5 years ago
|
recoveryPhrase: '',
|
||
|
generatedRecoveryPhrase: '',
|
||
5 years ago
|
hexGeneratedPubKey: '',
|
||
5 years ago
|
mnemonicError: undefined,
|
||
|
displayNameError: undefined,
|
||
5 years ago
|
};
|
||
|
}
|
||
|
|
||
5 years ago
|
public componentDidMount() {
|
||
4 years ago
|
void this.generateMnemonicAndKeyPair();
|
||
|
void this.resetRegistration();
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
public render() {
|
||
5 years ago
|
const { selectedTab } = this.state;
|
||
4 years ago
|
// tslint:disable: use-simple-attributes
|
||
5 years ago
|
|
||
|
return (
|
||
5 years ago
|
<div className="session-registration-container">
|
||
|
<div className="session-registration__tab-container">
|
||
4 years ago
|
<TabLabel
|
||
|
type={TabType.SignUp}
|
||
|
isSelected={selectedTab === TabType.SignUp}
|
||
5 years ago
|
onSelect={this.handleTabSelect}
|
||
|
/>
|
||
4 years ago
|
<TabLabel
|
||
5 years ago
|
type={TabType.SignIn}
|
||
4 years ago
|
isSelected={selectedTab === TabType.SignIn}
|
||
5 years ago
|
onSelect={this.handleTabSelect}
|
||
|
/>
|
||
|
</div>
|
||
|
{this.renderSections()}
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
5 years ago
|
private async generateMnemonicAndKeyPair() {
|
||
5 years ago
|
if (this.state.generatedRecoveryPhrase === '') {
|
||
5 years ago
|
const language = 'english';
|
||
4 years ago
|
const mnemonic = await window
|
||
|
.getAccountManager()
|
||
|
.generateMnemonic(language);
|
||
5 years ago
|
|
||
|
let seedHex = window.mnemonic.mn_decode(mnemonic, language);
|
||
|
// handle shorter than 32 bytes seeds
|
||
|
const privKeyHexLength = 32 * 2;
|
||
|
if (seedHex.length !== privKeyHexLength) {
|
||
5 years ago
|
seedHex = seedHex.concat('0'.repeat(32));
|
||
5 years ago
|
seedHex = seedHex.substring(0, privKeyHexLength);
|
||
|
}
|
||
|
const seed = window.dcodeIO.ByteBuffer.wrap(
|
||
|
seedHex,
|
||
|
'hex'
|
||
|
).toArrayBuffer();
|
||
5 years ago
|
const keyPair = await window.sessionGenerateKeyPair(seed);
|
||
5 years ago
|
const hexGeneratedPubKey = StringUtils.decode(keyPair.pubKey, 'hex');
|
||
5 years ago
|
|
||
|
this.setState({
|
||
5 years ago
|
generatedRecoveryPhrase: mnemonic,
|
||
5 years ago
|
hexGeneratedPubKey, // our 'frontend' sessionID
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
private readonly handleTabSelect = (tabType: TabType): void => {
|
||
5 years ago
|
this.setState({
|
||
|
selectedTab: tabType,
|
||
|
signInMode: SignInMode.Default,
|
||
|
signUpMode: SignUpMode.Default,
|
||
5 years ago
|
displayName: '',
|
||
|
password: '',
|
||
|
validatePassword: '',
|
||
|
passwordErrorString: '',
|
||
|
passwordFieldsMatch: false,
|
||
5 years ago
|
recoveryPhrase: '',
|
||
5 years ago
|
mnemonicError: undefined,
|
||
|
displayNameError: undefined,
|
||
5 years ago
|
});
|
||
5 years ago
|
};
|
||
|
|
||
|
private onSeedChanged(val: string) {
|
||
5 years ago
|
this.setState({
|
||
5 years ago
|
recoveryPhrase: val,
|
||
|
mnemonicError: !val ? window.i18n('recoveryPhraseEmpty') : undefined,
|
||
5 years ago
|
});
|
||
5 years ago
|
}
|
||
|
|
||
|
private onDisplayNameChanged(val: string) {
|
||
5 years ago
|
const sanitizedName = this.sanitiseNameInput(val);
|
||
5 years ago
|
const trimName = sanitizedName.trim();
|
||
|
|
||
5 years ago
|
this.setState({
|
||
|
displayName: sanitizedName,
|
||
5 years ago
|
displayNameError: !trimName ? window.i18n('displayNameEmpty') : undefined,
|
||
5 years ago
|
});
|
||
5 years ago
|
}
|
||
|
|
||
|
private onPasswordChanged(val: string) {
|
||
5 years ago
|
this.setState({ password: val }, () => {
|
||
|
this.validatePassword();
|
||
|
});
|
||
5 years ago
|
}
|
||
|
|
||
|
private onPasswordVerifyChanged(val: string) {
|
||
|
this.setState({ validatePassword: val });
|
||
5 years ago
|
this.setState({ validatePassword: val }, () => {
|
||
|
this.validatePassword();
|
||
|
});
|
||
5 years ago
|
}
|
||
|
|
||
|
private renderSections() {
|
||
|
const { selectedTab } = this.state;
|
||
4 years ago
|
if (selectedTab === TabType.SignUp) {
|
||
|
return (
|
||
|
<SignUpTab
|
||
|
signUpMode={this.state.signUpMode}
|
||
|
continueSignUp={() => {
|
||
|
this.setState({
|
||
|
signUpMode: SignUpMode.EnterDetails,
|
||
|
});
|
||
|
}}
|
||
|
createSessionID={() => {
|
||
|
this.setState(
|
||
|
{
|
||
|
signUpMode: SignUpMode.SessionIDShown,
|
||
|
},
|
||
|
() => {
|
||
|
window.Session.setNewSessionID(this.state.hexGeneratedPubKey);
|
||
|
}
|
||
|
);
|
||
|
}}
|
||
|
onCompleteSignUpClick={this.onCompleteSignUpClick}
|
||
|
displayName={this.state.displayName}
|
||
|
password={this.state.password}
|
||
|
passwordErrorString={this.state.passwordErrorString}
|
||
|
passwordFieldsMatch={this.state.passwordFieldsMatch}
|
||
|
displayNameError={this.state.displayNameError}
|
||
|
recoveryPhrase={this.state.recoveryPhrase}
|
||
|
onPasswordVerifyChanged={this.onPasswordVerifyChanged}
|
||
|
handlePressEnter={this.handlePressEnter}
|
||
|
onPasswordChanged={this.onPasswordChanged}
|
||
|
onDisplayNameChanged={this.onDisplayNameChanged}
|
||
|
onSeedChanged={this.onSeedChanged}
|
||
|
/>
|
||
|
);
|
||
5 years ago
|
}
|
||
|
|
||
|
return this.renderSignIn();
|
||
|
}
|
||
|
|
||
5 years ago
|
private getRenderTermsConditionAgreement() {
|
||
4 years ago
|
const { selectedTab, signInMode } = this.state;
|
||
|
if (selectedTab !== TabType.SignUp) {
|
||
|
return signInMode !== SignInMode.Default ? <TermsAndConditions /> : null;
|
||
5 years ago
|
}
|
||
4 years ago
|
return <></>;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
private onCompleteSignUpClick() {
|
||
4 years ago
|
void this.register();
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
private renderSignIn() {
|
||
5 years ago
|
return (
|
||
5 years ago
|
<div className="session-registration__content">
|
||
5 years ago
|
{this.renderRegistrationContent()}
|
||
|
|
||
|
{this.renderSignInButtons()}
|
||
|
{this.getRenderTermsConditionAgreement()}
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
private renderRegistrationContent() {
|
||
4 years ago
|
const { signInMode } = this.state;
|
||
5 years ago
|
|
||
4 years ago
|
const isSignInNotDefault = signInMode !== SignInMode.Default;
|
||
|
|
||
|
if (isSignInNotDefault) {
|
||
|
const sharedProps = {
|
||
|
displayName: this.state.displayName,
|
||
|
handlePressEnter: this.handlePressEnter,
|
||
|
onDisplayNameChanged: this.onDisplayNameChanged,
|
||
|
onPasswordChanged: this.onPasswordChanged,
|
||
|
onPasswordVerifyChanged: this.onPasswordVerifyChanged,
|
||
|
onSeedChanged: this.onSeedChanged,
|
||
|
password: this.state.password,
|
||
|
passwordErrorString: this.state.passwordErrorString,
|
||
|
passwordFieldsMatch: this.state.passwordFieldsMatch,
|
||
|
recoveryPhrase: this.state.recoveryPhrase,
|
||
|
stealAutoFocus: true,
|
||
|
};
|
||
|
|
||
|
if (signInMode === SignInMode.UsingRecoveryPhrase) {
|
||
|
return (
|
||
|
<RegistrationUserDetails
|
||
|
showDisplayNameField={true}
|
||
|
showSeedField={true}
|
||
|
{...sharedProps}
|
||
5 years ago
|
/>
|
||
4 years ago
|
);
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
if (signInMode === SignInMode.LinkDevice) {
|
||
|
return (
|
||
|
<RegistrationUserDetails
|
||
|
showDisplayNameField={false}
|
||
|
showSeedField={true}
|
||
|
{...sharedProps}
|
||
|
/>
|
||
|
);
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
5 years ago
|
private renderSignInButtons() {
|
||
|
const { signInMode } = this.state;
|
||
|
|
||
4 years ago
|
const or = window.i18n('or');
|
||
5 years ago
|
|
||
|
if (signInMode === SignInMode.Default) {
|
||
|
return (
|
||
|
<div>
|
||
4 years ago
|
{this.renderRestoreUsingRecoveryPhraseButton()}
|
||
|
<div className="or">{or}</div>
|
||
|
{this.renderLinkDeviceButton()}
|
||
5 years ago
|
</div>
|
||
|
);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
return (
|
||
|
<SessionButton
|
||
|
onClick={this.handleContinueYourSessionClick}
|
||
|
buttonType={SessionButtonType.Brand}
|
||
|
buttonColor={SessionButtonColor.Green}
|
||
|
text={window.i18n('continueYourSession')}
|
||
|
/>
|
||
|
);
|
||
5 years ago
|
}
|
||
|
|
||
|
private renderTermsConditionAgreement() {
|
||
4 years ago
|
return <TermsAndConditions />;
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
private handleContinueYourSessionClick() {
|
||
5 years ago
|
if (this.state.signInMode === SignInMode.UsingRecoveryPhrase) {
|
||
4 years ago
|
void this.register();
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
private renderRestoreUsingRecoveryPhraseButton() {
|
||
5 years ago
|
return (
|
||
|
<SessionButton
|
||
|
onClick={() => {
|
||
|
this.setState({
|
||
5 years ago
|
signInMode: SignInMode.UsingRecoveryPhrase,
|
||
|
recoveryPhrase: '',
|
||
5 years ago
|
displayName: '',
|
||
|
signUpMode: SignUpMode.Default,
|
||
|
});
|
||
|
}}
|
||
4 years ago
|
buttonType={SessionButtonType.BrandOutline}
|
||
|
buttonColor={SessionButtonColor.Green}
|
||
5 years ago
|
text={window.i18n('restoreUsingRecoveryPhrase')}
|
||
5 years ago
|
/>
|
||
|
);
|
||
|
}
|
||
|
|
||
4 years ago
|
private renderLinkDeviceButton() {
|
||
|
return (
|
||
|
<SessionButton
|
||
|
onClick={() => {
|
||
|
this.setState({
|
||
|
signInMode: SignInMode.LinkDevice,
|
||
|
recoveryPhrase: '',
|
||
|
displayName: '',
|
||
|
signUpMode: SignUpMode.Default,
|
||
|
});
|
||
|
}}
|
||
|
buttonType={SessionButtonType.BrandOutline}
|
||
|
buttonColor={SessionButtonColor.Green}
|
||
|
text={window.i18n('linkDevice')}
|
||
|
/>
|
||
|
);
|
||
|
}
|
||
|
|
||
5 years ago
|
private handlePressEnter() {
|
||
|
const { signInMode, signUpMode } = this.state;
|
||
|
if (signUpMode === SignUpMode.EnterDetails) {
|
||
|
this.onCompleteSignUpClick();
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
5 years ago
|
if (signInMode === SignInMode.UsingRecoveryPhrase) {
|
||
5 years ago
|
this.handleContinueYourSessionClick();
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
private trim(value: string) {
|
||
|
return value ? value.trim() : value;
|
||
|
}
|
||
|
|
||
|
private validatePassword() {
|
||
|
const input = this.trim(this.state.password);
|
||
|
const confirmationInput = this.trim(this.state.validatePassword);
|
||
|
|
||
|
// If user hasn't set a value then skip
|
||
|
if (!input && !confirmationInput) {
|
||
5 years ago
|
this.setState({
|
||
|
passwordErrorString: '',
|
||
|
passwordFieldsMatch: true,
|
||
|
});
|
||
|
|
||
|
return;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
const error = PasswordUtil.validatePassword(input, window.i18n);
|
||
5 years ago
|
if (error) {
|
||
5 years ago
|
this.setState({
|
||
|
passwordErrorString: error,
|
||
|
passwordFieldsMatch: true,
|
||
|
});
|
||
5 years ago
|
|
||
5 years ago
|
return;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
if (input !== confirmationInput) {
|
||
5 years ago
|
this.setState({
|
||
|
passwordErrorString: '',
|
||
5 years ago
|
passwordFieldsMatch: false,
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
return;
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
this.setState({
|
||
|
passwordErrorString: '',
|
||
|
passwordFieldsMatch: true,
|
||
|
});
|
||
5 years ago
|
}
|
||
|
|
||
|
private sanitiseNameInput(val: string) {
|
||
5 years ago
|
return val.replace(window.displayNameRegex, '');
|
||
5 years ago
|
}
|
||
|
|
||
|
private async resetRegistration() {
|
||
4 years ago
|
await removeAll();
|
||
4 years ago
|
await window.storage.reset();
|
||
5 years ago
|
await window.storage.fetch();
|
||
4 years ago
|
ConversationController.getInstance().reset();
|
||
|
await ConversationController.getInstance().load();
|
||
5 years ago
|
|
||
|
this.setState({
|
||
|
secretWords: undefined,
|
||
|
});
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
private async register() {
|
||
5 years ago
|
const {
|
||
|
password,
|
||
5 years ago
|
recoveryPhrase,
|
||
|
generatedRecoveryPhrase,
|
||
5 years ago
|
signInMode,
|
||
5 years ago
|
displayName,
|
||
|
passwordErrorString,
|
||
|
passwordFieldsMatch,
|
||
|
} = this.state;
|
||
5 years ago
|
// Make sure the password is valid
|
||
5 years ago
|
window.log.info('starting registration');
|
||
5 years ago
|
|
||
|
const trimName = displayName.trim();
|
||
|
|
||
|
if (!trimName) {
|
||
5 years ago
|
window.log.warn('invalid trimmed name for registration');
|
||
5 years ago
|
ToastUtils.pushToastError(
|
||
|
'invalidDisplayName',
|
||
|
window.i18n('displayNameEmpty')
|
||
|
);
|
||
5 years ago
|
return;
|
||
|
}
|
||
|
|
||
5 years ago
|
if (passwordErrorString) {
|
||
5 years ago
|
window.log.warn('invalid password for registration');
|
||
5 years ago
|
ToastUtils.pushToastError(
|
||
|
'invalidPassword',
|
||
|
window.i18n('invalidPassword')
|
||
|
);
|
||
5 years ago
|
return;
|
||
|
}
|
||
5 years ago
|
|
||
|
if (!!password && !passwordFieldsMatch) {
|
||
5 years ago
|
window.log.warn('passwords does not match for registration');
|
||
5 years ago
|
ToastUtils.pushToastError(
|
||
|
'invalidPassword',
|
||
|
window.i18n('passwordsDoNotMatch')
|
||
|
);
|
||
5 years ago
|
return;
|
||
|
}
|
||
|
|
||
5 years ago
|
if (signInMode === SignInMode.UsingRecoveryPhrase && !recoveryPhrase) {
|
||
5 years ago
|
window.log.warn('empty mnemonic seed passed in seed restoration mode');
|
||
|
|
||
|
return;
|
||
5 years ago
|
} else if (!generatedRecoveryPhrase) {
|
||
5 years ago
|
window.log.warn('empty generated seed');
|
||
|
|
||
5 years ago
|
return;
|
||
|
}
|
||
|
|
||
5 years ago
|
const seedToUse =
|
||
5 years ago
|
signInMode === SignInMode.UsingRecoveryPhrase
|
||
|
? recoveryPhrase
|
||
|
: generatedRecoveryPhrase;
|
||
5 years ago
|
|
||
5 years ago
|
try {
|
||
|
await this.resetRegistration();
|
||
|
|
||
|
await window.setPassword(password);
|
||
4 years ago
|
const isRestoringFromSeed = signInMode === SignInMode.UsingRecoveryPhrase;
|
||
|
UserUtils.setRestoringFromSeed(isRestoringFromSeed);
|
||
|
|
||
4 years ago
|
await window
|
||
|
.getAccountManager()
|
||
|
.registerSingleDevice(seedToUse, 'english', trimName);
|
||
4 years ago
|
// if we are just creating a new account, no need to wait for a configuration message
|
||
|
if (!isRestoringFromSeed) {
|
||
|
trigger('openInbox');
|
||
|
} else {
|
||
|
// We have to pull for all messages of the user of this menmonic
|
||
|
// We are looking for the most recent ConfigurationMessage he sent to himself.
|
||
|
// When we find it, we can just get the displayName, avatar and groups saved in it.
|
||
|
// If we do not find one, we will need to ask for a display name.
|
||
|
window.log.warn('isRestoringFromSeed');
|
||
|
}
|
||
5 years ago
|
} catch (e) {
|
||
5 years ago
|
ToastUtils.pushToastError(
|
||
|
'registrationError',
|
||
|
`Error: ${e.message || 'Something went wrong'}`
|
||
|
);
|
||
5 years ago
|
let exmsg = '';
|
||
|
if (e.message) {
|
||
|
exmsg += e.message;
|
||
|
}
|
||
|
if (e.stack) {
|
||
|
exmsg += ` | stack: + ${e.stack}`;
|
||
5 years ago
|
}
|
||
5 years ago
|
window.log.warn('exception during registration:', exmsg);
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
}
|