DocSpace-buildtools/web/ASC.Web.Login/src/Login.jsx

485 lines
13 KiB
React
Raw Normal View History

2020-11-24 10:20:14 +00:00
import React, { Component, useEffect } from "react";
import styled, { css } from "styled-components";
2020-11-26 16:41:43 +00:00
import { withTranslation } from "react-i18next";
2020-11-24 10:20:14 +00:00
import PropTypes from "prop-types";
2020-12-01 10:45:27 +00:00
import { withRouter } from "react-router";
import Box from "@appserver/components/box";
import Button from "@appserver/components/button";
import Text from "@appserver/components/text";
import TextInput from "@appserver/components/text-input";
import Link from "@appserver/components/link";
import toastr from "@appserver/components/toast/toastr";
import Checkbox from "@appserver/components/checkbox";
2021-02-25 08:44:46 +00:00
//import HelpButton from "@appserver/components/help-button";
import PasswordInput from "@appserver/components/password-input";
import FieldContainer from "@appserver/components/field-container";
import PageLayout from "@appserver/common/PageLayout";
2020-11-26 16:41:43 +00:00
import ForgotPasswordModalDialog from "./sub-components/forgot-password-modal-dialog";
import Register from "./sub-components/register-container";
import { checkPwd } from "@appserver/common/desktop";
import { sendInstructionsToChangePassword } from "@appserver/common/api/people";
import { createPasswordHash, tryRedirectTo } from "@appserver/common/utils";
2021-02-03 12:42:47 +00:00
import { inject, observer } from "mobx-react";
2021-02-25 08:44:46 +00:00
import "./i18n";
2020-11-24 10:20:14 +00:00
const LoginContainer = styled.div`
display: flex;
flex-direction: column;
align-items: center;
margin: 120px auto 0 auto;
max-width: 960px;
@media (max-width: 768px) {
padding: 0 16px;
max-width: 475px;
}
@media (max-width: 375px) {
margin: 72px auto 0 auto;
max-width: 311px;
}
.greeting-title {
width: 100%;
@media (max-width: 768px) {
text-align: left;
}
@media (max-width: 375px) {
font-size: 23px;
}
}
.auth-form-container {
margin: 32px 213px 0 213px;
width: 311px;
@media (max-width: 768px) {
margin: 32px 0 0 0;
width: 100%;
}
@media (max-width: 375px) {
margin: 32px 0 0 0;
width: 100%;
}
.login-forgot-wrapper {
height: 36px;
padding: 14px 0;
.login-checkbox-wrapper {
display: flex;
2020-11-24 10:20:14 +00:00
.login-checkbox {
float: left;
span {
font-size: 12px;
}
}
}
.login-link {
line-height: 18px;
margin-left: auto;
2020-11-24 10:20:14 +00:00
}
}
.login-button {
margin-bottom: 16px;
}
.login-button-dialog {
margin-right: 8px;
}
.login-bottom-border {
width: 100%;
height: 1px;
background: #eceef1;
}
.login-bottom-text {
margin: 0 8px;
}
}
`;
2020-11-27 15:43:22 +00:00
const LoginFormWrapper = styled.div`
display: grid;
2020-12-04 08:38:15 +00:00
grid-template-rows: ${(props) =>
2020-12-28 07:37:53 +00:00
props.enabledJoin
? props.isDesktop
? css`1fr 10px`
: css`1fr 66px`
: css`1fr`};
2020-11-27 15:43:22 +00:00
width: 100%;
height: calc(100vh-56px);
2020-11-27 15:43:22 +00:00
`;
2020-11-24 10:20:14 +00:00
class Form extends Component {
constructor(props) {
super(props);
this.state = {
identifierValid: true,
identifier: "",
isLoading: false,
isDisabled: false,
passwordValid: true,
password: "",
isChecked: false,
openDialog: false,
email: "",
emailError: false,
errorText: "",
socialButtons: [],
};
}
onChangeLogin = (event) => {
this.setState({ identifier: event.target.value });
!this.state.identifierValid && this.setState({ identifierValid: true });
this.state.errorText && this.setState({ errorText: "" });
};
onChangePassword = (event) => {
this.setState({ password: event.target.value });
!this.state.passwordValid && this.setState({ passwordValid: true });
this.state.errorText && this.setState({ errorText: "" });
};
onChangeEmail = (event) => {
this.setState({ email: event.target.value, emailError: false });
};
onChangeCheckbox = () => this.setState({ isChecked: !this.state.isChecked });
onClick = () => {
this.setState({
openDialog: true,
isDisabled: true,
email: this.state.identifier,
});
};
onKeyPress = (event) => {
if (event.key === "Enter") {
!this.state.isDisabled
? this.onSubmit()
: this.onSendPasswordInstructions();
}
};
onSendPasswordInstructions = () => {
if (!this.state.email.trim()) {
this.setState({ emailError: true });
} else {
this.setState({ isLoading: true });
sendInstructionsToChangePassword(this.state.email)
.then(
(res) => toastr.success(res),
(message) => toastr.error(message)
)
.finally(this.onDialogClose());
}
};
onDialogClose = () => {
this.setState({
openDialog: false,
isDisabled: false,
isLoading: false,
email: "",
emailError: false,
});
};
onSubmit = () => {
const { errorText, identifier, password } = this.state;
2021-02-03 12:42:47 +00:00
const { login, hashSettings, isDesktop, defaultPage } = this.props;
2020-11-24 10:20:14 +00:00
errorText && this.setState({ errorText: "" });
let hasError = false;
const userName = identifier.trim();
if (!userName) {
hasError = true;
this.setState({ identifierValid: !hasError });
}
const pass = password.trim();
if (!pass) {
hasError = true;
this.setState({ passwordValid: !hasError });
}
if (hasError) return false;
this.setState({ isLoading: true });
const hash = createPasswordHash(pass, hashSettings);
2020-11-13 12:58:14 +00:00
isDesktop && checkPwd();
2020-11-24 10:20:14 +00:00
login(userName, hash)
2021-02-03 12:42:47 +00:00
.then(() => tryRedirectTo(defaultPage))
2020-11-24 10:20:14 +00:00
.catch((error) => {
this.setState({
errorText: error,
identifierValid: !error,
passwordValid: !error,
isLoading: false,
});
2020-11-24 10:20:14 +00:00
});
};
componentDidMount() {
2021-02-03 12:42:47 +00:00
const { match, t, organizationName } = this.props;
2020-12-01 10:45:27 +00:00
const { error, confirmedEmail } = match.params;
2020-11-24 10:20:14 +00:00
document.title = `${t("Authorization")} ${organizationName}`; //TODO: implement the setDocumentTitle() utility in ASC.Web.Common
error && this.setState({ errorText: error });
confirmedEmail && this.setState({ identifier: confirmedEmail });
window.addEventListener("keyup", this.onKeyPress);
}
componentWillUnmount() {
window.removeEventListener("keyup", this.onKeyPress);
}
settings = {
minLength: 6,
upperCase: false,
digits: false,
specSymbols: false,
};
render() {
const { greetingTitle, match, t } = this.props;
const {
identifierValid,
identifier,
isLoading,
passwordValid,
password,
isChecked,
openDialog,
email,
emailError,
errorText,
socialButtons,
} = this.state;
2020-12-01 10:45:27 +00:00
const { confirmedEmail } = match.params;
2020-11-24 10:20:14 +00:00
//console.log("Login render");
return (
<>
<LoginContainer>
<Text
fontSize="32px"
fontWeight={600}
textAlign="center"
className="greeting-title"
>
{greetingTitle}
</Text>
<form className="auth-form-container">
<FieldContainer
isVertical={true}
labelVisible={false}
hasError={!identifierValid}
errorMessage={errorText ? errorText : t("RequiredFieldMessage")} //TODO: Add wrong login server error
2020-11-24 10:20:14 +00:00
>
<TextInput
id="login"
name="login"
hasError={!identifierValid}
value={identifier}
placeholder={t("RegistrationEmailWatermark")}
size="large"
scale={true}
isAutoFocussed={true}
tabIndex={1}
isDisabled={isLoading}
autoComplete="username"
onChange={this.onChangeLogin}
onKeyDown={this.onKeyPress}
/>
</FieldContainer>
<FieldContainer
isVertical={true}
labelVisible={false}
hasError={!passwordValid}
errorMessage={errorText ? "" : t("RequiredFieldMessage")} //TODO: Add wrong password server error
2020-11-24 10:20:14 +00:00
>
<PasswordInput
simpleView={true}
passwordSettings={this.settings}
id="password"
inputName="password"
placeholder={t("Password")}
type="password"
hasError={!passwordValid}
inputValue={password}
size="large"
scale={true}
tabIndex={1}
isDisabled={isLoading}
autoComplete="current-password"
onChange={this.onChangePassword}
onKeyDown={this.onKeyPress}
/>
</FieldContainer>
<div className="login-forgot-wrapper">
<div className="login-checkbox-wrapper">
<Checkbox
className="login-checkbox"
isChecked={isChecked}
onChange={this.onChangeCheckbox}
label={<Text fontSize="13px">{t("Remember")}</Text>}
/>
{/*<HelpButton
2020-11-24 10:20:14 +00:00
className="login-tooltip"
helpButtonHeaderContent={t("CookieSettingsTitle")}
tooltipContent={
<Text fontSize="12px">{t("RememberHelper")}</Text>
}
/>*/}
<Link
fontSize="13px"
color="#316DAA"
className="login-link"
type="page"
isHovered={false}
onClick={this.onClick}
>
{t("ForgotPassword")}
</Link>
2020-11-24 10:20:14 +00:00
</div>
</div>
2020-11-26 16:41:43 +00:00
{openDialog && (
2020-11-24 10:20:14 +00:00
<ForgotPasswordModalDialog
openDialog={openDialog}
isLoading={isLoading}
email={email}
emailError={emailError}
onChangeEmail={this.onChangeEmail}
onSendPasswordInstructions={this.onSendPasswordInstructions}
onDialogClose={this.onDialogClose}
t={t}
/>
2020-11-26 16:41:43 +00:00
)}
2020-11-24 10:20:14 +00:00
<Button
id="button"
className="login-button"
primary
size="large"
scale={true}
label={isLoading ? t("LoadingProcessing") : t("LoginButton")}
tabIndex={1}
isDisabled={isLoading}
isLoading={isLoading}
onClick={this.onSubmit}
/>
{confirmedEmail && (
<Text isBold={true} fontSize="16px">
{t("MessageEmailConfirmed")} {t("MessageAuthorize")}
</Text>
)}
{/* TODO: old error indication
2020-11-24 10:20:14 +00:00
<Text fontSize="14px" color="#c30">
{errorText}
</Text> */}
2020-11-24 10:20:14 +00:00
{socialButtons.length ? (
<Box displayProp="flex" alignItems="center">
<div className="login-bottom-border"></div>
<Text className="login-bottom-text" color="#A3A9AE">
{t("Or")}
</Text>
<div className="login-bottom-border"></div>
</Box>
) : null}
</form>
</LoginContainer>
</>
);
}
}
2020-12-01 10:45:27 +00:00
Form.propTypes = {
login: PropTypes.func.isRequired,
match: PropTypes.object.isRequired,
hashSettings: PropTypes.object,
2020-12-01 10:45:27 +00:00
greetingTitle: PropTypes.string.isRequired,
t: PropTypes.func.isRequired,
socialButtons: PropTypes.array,
organizationName: PropTypes.string,
homepage: PropTypes.string,
2020-12-04 11:21:51 +00:00
defaultPage: PropTypes.string,
2021-02-03 12:42:47 +00:00
isDesktop: PropTypes.bool,
2020-12-01 10:45:27 +00:00
};
2020-11-24 10:20:14 +00:00
Form.defaultProps = {
identifier: "",
password: "",
email: "",
};
const FormWrapper = withTranslation()(Form);
2021-02-25 08:44:46 +00:00
//const RegisterWrapper = withTranslation()(Register);
2020-11-26 16:41:43 +00:00
const LoginForm = (props) => {
2021-02-25 08:44:46 +00:00
const { enabledJoin, isDesktop } = props;
2020-11-26 16:41:43 +00:00
return (
2020-12-28 07:37:53 +00:00
<LoginFormWrapper enabledJoin={enabledJoin} isDesktop={isDesktop}>
<PageLayout>
<PageLayout.SectionBody>
2021-02-25 08:44:46 +00:00
<FormWrapper {...props} />
</PageLayout.SectionBody>
</PageLayout>
<Register />
</LoginFormWrapper>
2020-11-26 16:41:43 +00:00
);
};
2020-11-24 10:20:14 +00:00
2020-11-26 16:41:43 +00:00
LoginForm.propTypes = {
isLoaded: PropTypes.bool,
enabledJoin: PropTypes.bool,
2020-12-28 07:39:30 +00:00
isDesktop: PropTypes.bool.isRequired,
2020-11-26 16:41:43 +00:00
};
2020-11-24 10:20:14 +00:00
2021-02-15 19:43:07 +00:00
export default inject(({ auth }) => {
2021-02-25 08:44:46 +00:00
const { settingsStore, isAuthenticated, isLoaded, login } = auth;
2020-11-26 16:41:43 +00:00
const {
2021-02-03 12:42:47 +00:00
greetingSettings: greetingTitle,
2020-11-26 16:41:43 +00:00
organizationName,
hashSettings,
2020-12-04 08:38:15 +00:00
enabledJoin,
defaultPage,
2021-02-03 12:42:47 +00:00
isDesktopClient: isDesktop,
} = settingsStore;
2020-11-26 16:41:43 +00:00
return {
2020-12-04 08:38:15 +00:00
isAuthenticated,
2020-11-26 16:41:43 +00:00
isLoaded,
organizationName,
2021-02-03 12:42:47 +00:00
greetingTitle,
2020-11-26 16:41:43 +00:00
hashSettings,
2020-12-04 08:38:15 +00:00
enabledJoin,
defaultPage,
2021-02-03 12:42:47 +00:00
isDesktop,
login,
2020-11-26 16:41:43 +00:00
};
2021-02-03 12:42:47 +00:00
})(withRouter(observer(LoginForm)));