2021-02-04 19:15:23 +01:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
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 {
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsContextProp,
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
2022-08-11 19:33:33 +02:00
|
|
|
} from '../../../shared/constants/metametrics';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { SUPPORT_LINK } from '../../../shared/lib/ui-utils';
|
2022-11-09 17:58:47 +01:00
|
|
|
import { isBeta } from '../../helpers/utils/build-types';
|
2023-01-16 14:07:59 +01:00
|
|
|
import { getCaretCoordinates } from './unlock-page.util';
|
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
|
|
|
/**
|
2023-01-16 14:07:59 +01:00
|
|
|
* onSubmit handler when form is submitted
|
2021-12-07 00:08:13 +01:00
|
|
|
*/
|
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-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;
|
2023-02-23 20:58:09 +01:00
|
|
|
const { onSubmit, forceUpdateMetamaskState } = 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);
|
2022-03-29 15:46:24 +02:00
|
|
|
this.context.trackEvent(
|
|
|
|
{
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event: MetaMetricsEventName.AppUnlocked,
|
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
|
|
|
);
|
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({
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event: MetaMetricsEventName.AppUnlockedFailed,
|
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"
|
2022-10-11 19:54:50 +02:00
|
|
|
data-testid="unlock-submit"
|
2018-05-11 01:51:26 +02:00
|
|
|
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"
|
|
|
|
/>
|
2022-11-09 17:58:47 +01:00
|
|
|
{isBeta() ? (
|
|
|
|
<div className="unlock-page__mascot-container__beta">
|
|
|
|
{t('beta')}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2018-05-11 01:51:26 +02:00
|
|
|
</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"
|
2022-10-11 19:54:50 +02:00
|
|
|
data-testid="unlock-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(
|
|
|
|
{
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event: MetaMetricsEventName.SupportLinkClicked,
|
2022-08-11 19:33:33 +02:00
|
|
|
properties: {
|
|
|
|
url: SUPPORT_LINK,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
contextPropsIntoEventProperties: [
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsContextProp.PageTitle,
|
2022-08-11 19:33:33 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}}
|
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
|
|
|
}
|
|
|
|
}
|