import React from 'react';
import PropTypes from 'prop-types';
import ReactCardFlip from 'react-card-flip';
import { withTranslation } from 'react-i18next';
import AppContainer from '~/client/services/AppContainer';
import { withUnstatedContainers } from './UnstatedUtils';
class LoginForm extends React.Component {
constructor(props) {
super(props);
this.state = {
isRegistering: false,
};
this.switchForm = this.switchForm.bind(this);
this.handleLoginWithExternalAuth = this.handleLoginWithExternalAuth.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);
const { hash } = window.location;
if (hash === '#register') {
this.state.isRegistering = true;
}
}
switchForm() {
this.setState({ isRegistering: !this.state.isRegistering });
}
handleLoginWithExternalAuth(e) {
const auth = e.currentTarget.id;
const { csrf } = this.props.appContainer;
window.location.href = `/passport/${auth}?_csrf=${csrf}`;
}
renderLocalOrLdapLoginForm() {
const { t, appContainer, isLdapStrategySetup } = this.props;
return (
);
}
renderExternalAuthInput(auth) {
const { t } = this.props;
const authIconNames = {
google: 'google',
github: 'github',
facebook: 'facebook',
twitter: 'twitter',
oidc: 'openid',
saml: 'key',
basic: 'lock',
};
return (
by {auth} Account
);
}
renderExternalAuthLoginForm() {
const { isLocalStrategySetup, isLdapStrategySetup, objOfIsExternalAuthEnableds } = this.props;
const isExternalAuthCollapsible = isLocalStrategySetup || isLdapStrategySetup;
const collapsibleClass = isExternalAuthCollapsible ? 'collapse collapse-external-auth' : '';
return (
<>
{Object.keys(objOfIsExternalAuthEnableds).map((auth) => {
if (!objOfIsExternalAuthEnableds[auth]) {
return;
}
return this.renderExternalAuthInput(auth);
})}
>
);
}
renderRegisterForm() {
const {
t,
appContainer,
isEmailAuthenticationEnabled,
username,
name,
email,
registrationMode,
registrationWhiteList,
} = this.props;
const { isMailerSetup } = appContainer.config;
let registerAction = '/register';
let submitText = t('Sign up');
if (isEmailAuthenticationEnabled) {
registerAction = '/user-activation/register';
submitText = t('page_register.send_email');
}
return (
{registrationMode === 'Restricted' && (
{t('page_register.notice.restricted')}
{t('page_register.notice.restricted_defail')}
)}
{ (!isMailerSetup && isEmailAuthenticationEnabled) && (
{t('security_setting.Local.please_enable_mailer')}
)}
);
}
render() {
const {
t,
isLocalStrategySetup,
isLdapStrategySetup,
isRegistrationEnabled,
isPasswordResetEnabled,
objOfIsExternalAuthEnableds,
} = this.props;
const isLocalOrLdapStrategiesEnabled = isLocalStrategySetup || isLdapStrategySetup;
const isSomeExternalAuthEnabled = Object.values(objOfIsExternalAuthEnableds).some(elem => elem);
return (
{isLocalOrLdapStrategiesEnabled && this.renderLocalOrLdapLoginForm()}
{isSomeExternalAuthEnabled && this.renderExternalAuthLoginForm()}
{isLocalOrLdapStrategiesEnabled && isPasswordResetEnabled && (
)}
{isRegistrationEnabled && (
)}
{isRegistrationEnabled && this.renderRegisterForm()}
GROWI.ORG
);
}
}
/**
* Wrapper component for using unstated
*/
const LoginFormWrapper = withUnstatedContainers(LoginForm, [AppContainer]);
LoginForm.propTypes = {
// i18next
t: PropTypes.func.isRequired,
appContainer: PropTypes.instanceOf(AppContainer).isRequired,
isRegistering: PropTypes.bool,
username: PropTypes.string,
name: PropTypes.string,
email: PropTypes.string,
isRegistrationEnabled: PropTypes.bool,
registrationMode: PropTypes.string,
registrationWhiteList: PropTypes.array,
isPasswordResetEnabled: PropTypes.bool,
isEmailAuthenticationEnabled: PropTypes.bool,
isLocalStrategySetup: PropTypes.bool,
isLdapStrategySetup: PropTypes.bool,
objOfIsExternalAuthEnableds: PropTypes.object,
};
export default withTranslation()(LoginFormWrapper);