2021-02-04 19:15:23 +01:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import getCaretCoordinates from 'textarea-caret';
|
2021-10-05 21:20:42 +02:00
|
|
|
import Button from '../../components/ui/button';
|
2021-02-04 19:15:23 +01:00
|
|
|
import TextField from '../../components/ui/text-field';
|
|
|
|
import Mascot from '../../components/ui/mascot';
|
|
|
|
import { DEFAULT_ROUTE } from '../../helpers/constants/routes';
|
2022-08-11 19:33:33 +02:00
|
|
|
import {
|
|
|
|
EVENT,
|
|
|
|
EVENT_NAMES,
|
|
|
|
CONTEXT_PROPS,
|
|
|
|
} from '../../../shared/constants/metametrics';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { SUPPORT_LINK } from '../../../shared/lib/ui-utils';
|
2018-07-10 23:56:12 +02:00
|
|
|
|
|
|
|
export default class UnlockPage extends Component {
|
2018-05-11 01:51:26 +02:00
|
|
|
static contextTypes = {
|
2022-03-29 15:46:24 +02:00
|
|
|
trackEvent: PropTypes.func,
|
2018-05-11 01:51:26 +02:00
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2018-07-10 23:56:12 +02:00
|
|
|
static propTypes = {
|
2021-12-07 00:08:13 +01:00
|
|
|
/**
|
|
|
|
* History router for redirect after action
|
|
|
|
*/
|
2020-02-10 18:42:59 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
2021-12-07 00:08:13 +01:00
|
|
|
/**
|
|
|
|
* If isUnlocked is true will redirect to most recent route in history
|
|
|
|
*/
|
2018-07-10 23:56:12 +02:00
|
|
|
isUnlocked: PropTypes.bool,
|
2021-12-07 00:08:13 +01:00
|
|
|
/**
|
2022-02-22 16:45:19 +01:00
|
|
|
* onClick handler for "Forgot password?" link
|
2021-12-07 00:08:13 +01:00
|
|
|
*/
|
2019-01-23 16:25:34 +01:00
|
|
|
onRestore: PropTypes.func,
|
2021-12-07 00:08:13 +01:00
|
|
|
/**
|
|
|
|
* onSumbit handler when form is submitted
|
|
|
|
*/
|
2019-01-23 16:25:34 +01:00
|
|
|
onSubmit: PropTypes.func,
|
2021-12-07 00:08:13 +01:00
|
|
|
/**
|
|
|
|
* Force update metamask data state
|
|
|
|
*/
|
2019-03-12 15:39:56 +01:00
|
|
|
forceUpdateMetamaskState: PropTypes.func,
|
2021-12-07 00:08:13 +01:00
|
|
|
/**
|
|
|
|
* Event handler to show metametrics modal
|
|
|
|
*/
|
2019-03-12 15:39:56 +01:00
|
|
|
showOptInModal: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-07-10 23:56:12 +02:00
|
|
|
|
2019-12-19 23:00:22 +01:00
|
|
|
state = {
|
|
|
|
password: '',
|
|
|
|
error: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
submitting = false;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2022-08-16 18:39:23 +02:00
|
|
|
failed_attempts = 0;
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
animationEventEmitter = new EventEmitter();
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
UNSAFE_componentWillMount() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { isUnlocked, history } = this.props;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
if (isUnlocked) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(DEFAULT_ROUTE);
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
handleSubmit = async (event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { password } = this.state;
|
|
|
|
const { onSubmit, forceUpdateMetamaskState, showOptInModal } = this.props;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2018-07-10 23:56:12 +02:00
|
|
|
if (password === '' || this.submitting) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ error: null });
|
|
|
|
this.submitting = true;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2018-05-30 18:23:31 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await onSubmit(password);
|
|
|
|
const newState = await forceUpdateMetamaskState();
|
2022-03-29 15:46:24 +02:00
|
|
|
this.context.trackEvent(
|
|
|
|
{
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.APP_UNLOCKED,
|
2022-03-29 15:46:24 +02:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
failed_attempts: this.failed_attempts,
|
2022-03-29 15:46:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isNewVisit: true,
|
2019-03-05 16:45:01 +01:00
|
|
|
},
|
2022-03-29 15:46:24 +02:00
|
|
|
);
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
newState.participateInMetaMetrics === null ||
|
|
|
|
newState.participateInMetaMetrics === undefined
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
showOptInModal();
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
2018-05-30 18:23:31 +02:00
|
|
|
} catch ({ message }) {
|
2022-08-16 18:39:23 +02:00
|
|
|
this.failed_attempts += 1;
|
|
|
|
|
2019-03-05 16:45:01 +01:00
|
|
|
if (message === 'Incorrect password') {
|
2022-08-16 18:39:23 +02:00
|
|
|
await forceUpdateMetamaskState();
|
2022-03-29 15:46:24 +02:00
|
|
|
this.context.trackEvent({
|
2022-04-22 18:09:10 +02:00
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
2022-08-16 18:39:23 +02:00
|
|
|
event: EVENT_NAMES.APP_UNLOCKED_FAILED,
|
2022-03-29 15:46:24 +02:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
reason: 'incorrect_password',
|
|
|
|
failed_attempts: this.failed_attempts,
|
2019-03-05 16:45:01 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ error: message });
|
|
|
|
this.submitting = false;
|
2018-05-30 18:23:31 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
handleInputChange({ target }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ password: target.value, error: null });
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
// tell mascot to look at page action
|
2020-01-30 20:34:45 +01:00
|
|
|
if (target.getBoundingClientRect) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const element = target;
|
|
|
|
const boundingRect = element.getBoundingClientRect();
|
|
|
|
const coordinates = getCaretCoordinates(element, element.selectionEnd);
|
2020-01-30 20:34:45 +01:00
|
|
|
this.animationEventEmitter.emit('point', {
|
|
|
|
x: boundingRect.left + coordinates.left - element.scrollLeft,
|
|
|
|
y: boundingRect.top + coordinates.top - element.scrollTop,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
}
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderSubmitButton() {
|
2018-05-11 01:51:26 +02:00
|
|
|
const style = {
|
2022-03-25 23:15:31 +01:00
|
|
|
backgroundColor: 'var(--color-primary-default)',
|
2022-03-28 19:23:27 +02:00
|
|
|
color: 'var(--color-primary-inverse)',
|
2018-05-11 01:51:26 +02:00
|
|
|
marginTop: '20px',
|
|
|
|
height: '60px',
|
|
|
|
fontWeight: '400',
|
|
|
|
boxShadow: 'none',
|
2021-10-05 21:20:42 +02:00
|
|
|
borderRadius: '100px',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
style={style}
|
|
|
|
disabled={!this.state.password}
|
2020-10-15 17:36:15 +02:00
|
|
|
variant="contained"
|
2018-05-11 01:51:26 +02:00
|
|
|
size="large"
|
2019-01-23 16:25:34 +01:00
|
|
|
onClick={this.handleSubmit}
|
2018-05-11 01:51:26 +02:00
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('unlock')}
|
2018-05-11 01:51:26 +02:00
|
|
|
</Button>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { password, error } = this.state;
|
|
|
|
const { t } = this.context;
|
2021-04-23 00:31:13 +02:00
|
|
|
const { onRestore } = this.props;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="unlock-page__container">
|
2022-08-31 01:53:24 +02:00
|
|
|
<div className="unlock-page" data-testid="unlock-page">
|
2018-05-11 01:51:26 +02:00
|
|
|
<div className="unlock-page__mascot-container">
|
|
|
|
<Mascot
|
|
|
|
animationEventEmitter={this.animationEventEmitter}
|
|
|
|
width="120"
|
|
|
|
height="120"
|
|
|
|
/>
|
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
<h1 className="unlock-page__title">{t('welcomeBack')}</h1>
|
|
|
|
<div>{t('unlockMessage')}</div>
|
|
|
|
<form className="unlock-page__form" onSubmit={this.handleSubmit}>
|
2018-05-11 01:51:26 +02:00
|
|
|
<TextField
|
|
|
|
id="password"
|
2018-07-10 23:56:12 +02:00
|
|
|
label={t('password')}
|
2018-05-11 01:51:26 +02:00
|
|
|
type="password"
|
2018-07-10 23:56:12 +02:00
|
|
|
value={password}
|
2020-02-15 21:34:12 +01:00
|
|
|
onChange={(event) => this.handleInputChange(event)}
|
2018-05-11 01:51:26 +02:00
|
|
|
error={error}
|
|
|
|
autoFocus
|
|
|
|
autoComplete="current-password"
|
2019-12-04 03:21:55 +01:00
|
|
|
theme="material"
|
2018-05-11 01:51:26 +02:00
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
</form>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderSubmitButton()}
|
2018-05-11 01:51:26 +02:00
|
|
|
<div className="unlock-page__links">
|
2022-02-22 16:45:19 +01:00
|
|
|
<Button
|
|
|
|
type="link"
|
|
|
|
key="import-account"
|
|
|
|
className="unlock-page__link"
|
|
|
|
onClick={() => onRestore()}
|
|
|
|
>
|
|
|
|
{t('forgotPassword')}
|
|
|
|
</Button>
|
2021-04-23 00:31:13 +02:00
|
|
|
</div>
|
|
|
|
<div className="unlock-page__support">
|
|
|
|
{t('needHelp', [
|
|
|
|
<a
|
2022-01-06 01:55:20 +01:00
|
|
|
href={SUPPORT_LINK}
|
2021-04-23 00:31:13 +02:00
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
key="need-help-link"
|
2022-08-11 19:33:33 +02:00
|
|
|
onClick={() => {
|
|
|
|
this.context.trackEvent(
|
|
|
|
{
|
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
|
|
|
event: EVENT_NAMES.SUPPORT_LINK_CLICKED,
|
|
|
|
properties: {
|
|
|
|
url: SUPPORT_LINK,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
contextPropsIntoEventProperties: [
|
|
|
|
CONTEXT_PROPS.PAGE_TITLE,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}}
|
2021-04-23 00:31:13 +02:00
|
|
|
>
|
|
|
|
{t('needHelpLinkText')}
|
|
|
|
</a>,
|
|
|
|
])}
|
2018-05-11 01:51:26 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
}
|