import React from 'react'; import PropTypes from 'prop-types'; import loggerFactory from '@alias/logger'; import { withTranslation } from 'react-i18next'; import LoginContainer from '../services/LoginContainer'; import { createSubscribedElement } from './UnstatedUtils'; const logger = loggerFactory('growi:loginForm'); class LoginForm extends React.Component { constructor(props) { super(props); this.isRegistrationEnabled = false; this.registrationMode = 'Closed'; this.registrationWhiteList = []; this.isLocalStrategySetup = false; this.isLdapStrategySetup = false; this.objOfIsExternalAuthEnableds = {}; this.switchForm = this.switchForm.bind(this); this.renderLocalOrLdapLoginForm = this.renderLocalOrLdapLoginForm.bind(this); this.renderExternalAuthLoginForm = this.renderExternalAuthLoginForm.bind(this); this.renderExternalAuthInput = this.renderExternalAuthInput.bind(this); this.renderRegisterForm = this.renderRegisterForm.bind(this); } async componentDidMount() { const { loginContainer } = this.props; try { await loginContainer.retrieveData(); } catch (err) { loginContainer.setState({ retrieveError: err.message }); logger.error(err); } } // for flip [TODO][GW-1865] use state or react component for flip switchForm(e) { if (e.target.id === 'register') { $('#login-dialog').addClass('to-flip'); } else { $('#login-dialog').removeClass('to-flip'); } } renderLocalOrLdapLoginForm() { const { t, csrf } = this.props; return (
{this.isLdapStrategySetup && (
LDAP
)}
{/* [TODO][GW-1913] An AppContainer gets csrf data */}
); } renderExternalAuthInput(auth) { const { t, csrf } = this.props; return (
{/* [TODO][GW-1913] use onClick, and delete form tag */}
{/* [TODO][GW-1913] An AppContainer gets csrf data */}
by {auth} Account
); } renderExternalAuthLoginForm() { const isExternalAuthCollapsible = this.isLocalStrategySetup || this.isLdapStrategySetup; const collapsibleClass = isExternalAuthCollapsible ? 'collapse collapse-external-auth collapse-anchor' : ''; return ( <>
{Object.keys(this.objOfIsExternalAuthEnableds).map(auth => { if (!this.objOfIsExternalAuthEnableds[auth]) { return; } return this.renderExternalAuthInput(auth); })}
); } renderRegisterForm() { const { t, csrf } = this.props; return (
{this.registrationMode === 'Restricted' && (

{t('page_register.notice.restricted')}
{t('page_register.notice.restricted_defail')}

)}

{this.registrationWhiteList.length > 0 && ( <>

{t('page_register.form_help.email')}

)}
{/* [TODO][GW-1913] An AppContainer gets csrf data */}
{t('Sign in is here')}
); } render() { const { t, isRegistering } = this.props; const isLocalOrLdapStrategiesEnabled = this.isLocalStrategySetup || this.isLdapStrategySetup; const registerFormClass = isRegistering ? 'to-flip' : ''; const isSomeExternalAuthEnabled = Object.values(this.objOfIsExternalAuthEnableds).some(elem => elem); return (
{isLocalOrLdapStrategiesEnabled && this.renderLocalOrLdapLoginForm()} {isSomeExternalAuthEnabled && this.renderExternalAuthLoginForm()} {this.isRegistrationEnabled && (
)}
{this.isRegistrationEnabled && this.renderRegisterForm()} GROWI.ORG
); } } /** * Wrapper component for using unstated */ const LoginFormWrapper = (props) => { return createSubscribedElement(LoginForm, props, [LoginContainer]); }; LoginForm.propTypes = { // i18next t: PropTypes.func.isRequired, loginContainer: PropTypes.instanceOf(LoginContainer).isRequired, isRegistering: PropTypes.bool, username: PropTypes.string, name: PropTypes.string, email: PropTypes.string, csrf: PropTypes.string, }; export default withTranslation()(LoginFormWrapper);