LoginForm.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ReactCardFlip from 'react-card-flip';
  4. import { withTranslation } from 'react-i18next';
  5. import AppContainer from '../services/AppContainer';
  6. import { withUnstatedContainers } from './UnstatedUtils';
  7. class LoginForm extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. isRegistering: false,
  12. isPasswordResetEnabled: false,
  13. };
  14. this.switchForm = this.switchForm.bind(this);
  15. this.handleLoginWithExternalAuth = this.handleLoginWithExternalAuth.bind(this);
  16. this.renderLocalOrLdapLoginForm = this.renderLocalOrLdapLoginForm.bind(this);
  17. this.renderExternalAuthLoginForm = this.renderExternalAuthLoginForm.bind(this);
  18. this.renderExternalAuthInput = this.renderExternalAuthInput.bind(this);
  19. this.renderRegisterForm = this.renderRegisterForm.bind(this);
  20. const { hash } = window.location;
  21. if (hash === '#register') {
  22. this.state.isRegistering = true;
  23. }
  24. }
  25. componentDidMount() {
  26. this.setState({ isPasswordResetEnabled: true });
  27. }
  28. switchForm() {
  29. this.setState({ isRegistering: !this.state.isRegistering });
  30. }
  31. handleLoginWithExternalAuth(e) {
  32. const auth = e.currentTarget.id;
  33. const { csrf } = this.props.appContainer;
  34. window.location.href = `/passport/${auth}?_csrf=${csrf}`;
  35. }
  36. renderLocalOrLdapLoginForm() {
  37. const { t, appContainer, isLdapStrategySetup } = this.props;
  38. return (
  39. <form role="form" action="/login" method="post">
  40. <div className="input-group">
  41. <div className="input-group-prepend">
  42. <span className="input-group-text">
  43. <i className="icon-user"></i>
  44. </span>
  45. </div>
  46. <input type="text" className="form-control rounded-0" placeholder="Username or E-mail" name="loginForm[username]" />
  47. {isLdapStrategySetup && (
  48. <div className="input-group-append">
  49. <small className="input-group-text text-success">
  50. <i className="icon-fw icon-check"></i> LDAP
  51. </small>
  52. </div>
  53. )}
  54. </div>
  55. <div className="input-group">
  56. <div className="input-group-prepend">
  57. <span className="input-group-text">
  58. <i className="icon-lock"></i>
  59. </span>
  60. </div>
  61. <input type="password" className="form-control rounded-0" placeholder="Password" name="loginForm[password]" />
  62. </div>
  63. <div className="input-group my-4">
  64. <input type="hidden" name="_csrf" value={appContainer.csrfToken} />
  65. <button type="submit" id="login" className="btn btn-fill rounded-0 login mx-auto">
  66. <div className="eff"></div>
  67. <span className="btn-label">
  68. <i className="icon-login"></i>
  69. </span>
  70. <span className="btn-label-text">{t('Sign in')}</span>
  71. </button>
  72. </div>
  73. </form>
  74. );
  75. }
  76. renderExternalAuthInput(auth) {
  77. const { t } = this.props;
  78. const authIconNames = {
  79. google: 'google',
  80. github: 'github',
  81. facebook: 'facebook',
  82. twitter: 'twitter',
  83. oidc: 'openid',
  84. saml: 'key',
  85. basic: 'lock',
  86. };
  87. return (
  88. <div key={auth} className="col-6 my-2">
  89. <button type="button" className="btn btn-fill rounded-0" id={auth} onClick={this.handleLoginWithExternalAuth}>
  90. <div className="eff"></div>
  91. <span className="btn-label">
  92. <i className={`fa fa-${authIconNames[auth]}`}></i>
  93. </span>
  94. <span className="btn-label-text">{t('Sign in')}</span>
  95. </button>
  96. <div className="small text-right">by {auth} Account</div>
  97. </div>
  98. );
  99. }
  100. renderExternalAuthLoginForm() {
  101. const { isLocalStrategySetup, isLdapStrategySetup, objOfIsExternalAuthEnableds } = this.props;
  102. const isExternalAuthCollapsible = isLocalStrategySetup || isLdapStrategySetup;
  103. const collapsibleClass = isExternalAuthCollapsible ? 'collapse collapse-external-auth' : '';
  104. return (
  105. <>
  106. <div className="grw-external-auth-form border-top border-bottom">
  107. <div id="external-auth" className={`external-auth ${collapsibleClass}`}>
  108. <div className="row mt-2">
  109. {Object.keys(objOfIsExternalAuthEnableds).map((auth) => {
  110. if (!objOfIsExternalAuthEnableds[auth]) {
  111. return;
  112. }
  113. return this.renderExternalAuthInput(auth);
  114. })}
  115. </div>
  116. </div>
  117. </div>
  118. <div className="text-center">
  119. <button
  120. type="button"
  121. className="btn btn-secondary btn-external-auth-tab btn-sm rounded-0 mb-3"
  122. data-toggle={isExternalAuthCollapsible ? 'collapse' : ''}
  123. data-target="#external-auth"
  124. aria-expanded="false"
  125. aria-controls="external-auth"
  126. >
  127. External Auth
  128. </button>
  129. </div>
  130. </>
  131. );
  132. }
  133. renderRegisterForm() {
  134. const {
  135. t,
  136. appContainer,
  137. username,
  138. name,
  139. email,
  140. registrationMode,
  141. registrationWhiteList,
  142. } = this.props;
  143. return (
  144. <React.Fragment>
  145. {registrationMode === 'Restricted' && (
  146. <p className="alert alert-warning">
  147. {t('page_register.notice.restricted')}
  148. <br />
  149. {t('page_register.notice.restricted_defail')}
  150. </p>
  151. )}
  152. <form role="form" action="/register" method="post" id="register-form">
  153. <div className="input-group" id="input-group-username">
  154. <div className="input-group-prepend">
  155. <span className="input-group-text">
  156. <i className="icon-user"></i>
  157. </span>
  158. </div>
  159. <input type="text" className="form-control rounded-0" placeholder={t('User ID')} name="registerForm[username]" defaultValue={username} required />
  160. </div>
  161. <p className="form-text text-danger">
  162. <span id="help-block-username"></span>
  163. </p>
  164. <div className="input-group">
  165. <div className="input-group-prepend">
  166. <span className="input-group-text">
  167. <i className="icon-tag"></i>
  168. </span>
  169. </div>
  170. <input type="text" className="form-control rounded-0" placeholder={t('Name')} name="registerForm[name]" defaultValue={name} required />
  171. </div>
  172. <div className="input-group">
  173. <div className="input-group-prepend">
  174. <span className="input-group-text">
  175. <i className="icon-envelope"></i>
  176. </span>
  177. </div>
  178. <input type="email" className="form-control rounded-0" placeholder={t('Email')} name="registerForm[email]" defaultValue={email} required />
  179. </div>
  180. {registrationWhiteList.length > 0 && (
  181. <>
  182. <p className="form-text">{t('page_register.form_help.email')}</p>
  183. <ul>
  184. {registrationWhiteList.map((elem) => {
  185. return (
  186. <li key={elem}>
  187. <code>{elem}</code>
  188. </li>
  189. );
  190. })}
  191. </ul>
  192. </>
  193. )}
  194. <div className="input-group">
  195. <div className="input-group-prepend">
  196. <span className="input-group-text">
  197. <i className="icon-lock"></i>
  198. </span>
  199. </div>
  200. <input type="password" className="form-control rounded-0" placeholder={t('Password')} name="registerForm[password]" required />
  201. </div>
  202. <div className="input-group justify-content-center my-4">
  203. <input type="hidden" name="_csrf" value={appContainer.csrfToken} />
  204. <button type="submit" className="btn btn-fill rounded-0" id="register">
  205. <div className="eff"></div>
  206. <span className="btn-label">
  207. <i className="icon-user-follow"></i>
  208. </span>
  209. <span className="btn-label-text">{t('Sign up')}</span>
  210. </button>
  211. </div>
  212. </form>
  213. <div className="border-bottom"></div>
  214. <div className="row">
  215. <div className="text-right col-12 mt-2 py-2">
  216. <a href="#login" id="login" className="link-switch" onClick={this.switchForm}>
  217. <i className="icon-fw icon-login"></i>
  218. {t('Sign in is here')}
  219. </a>
  220. </div>
  221. </div>
  222. </React.Fragment>
  223. );
  224. }
  225. render() {
  226. const {
  227. t,
  228. isLocalStrategySetup,
  229. isLdapStrategySetup,
  230. isRegistrationEnabled,
  231. objOfIsExternalAuthEnableds,
  232. } = this.props;
  233. const isLocalOrLdapStrategiesEnabled = isLocalStrategySetup || isLdapStrategySetup;
  234. const isSomeExternalAuthEnabled = Object.values(objOfIsExternalAuthEnableds).some(elem => elem);
  235. return (
  236. <div className="login-dialog mx-auto" id="login-dialog">
  237. <div className="row mx-0">
  238. <div className="col-12">
  239. <ReactCardFlip isFlipped={this.state.isRegistering} flipDirection="horizontal" cardZIndex="3">
  240. <div className="front">
  241. {isLocalOrLdapStrategiesEnabled && this.renderLocalOrLdapLoginForm()}
  242. {isSomeExternalAuthEnabled && this.renderExternalAuthLoginForm()}
  243. {isRegistrationEnabled && (
  244. <div className="row">
  245. <div className="col-12 text-right py-2">
  246. {this.state.isPasswordResetEnabled && (
  247. <a href="/forgot-password" className="d-block link-switch mb-1">
  248. <i className="icon-key"></i> {t('forgot_password.forgot_password')}
  249. </a>
  250. )}
  251. <a href="#register" id="register" className="link-switch" onClick={this.switchForm}>
  252. <i className="ti-check-box"></i> {t('Sign up is here')}
  253. </a>
  254. </div>
  255. </div>
  256. )}
  257. </div>
  258. <div className="back">
  259. {isRegistrationEnabled && this.renderRegisterForm()}
  260. </div>
  261. </ReactCardFlip>
  262. </div>
  263. </div>
  264. <a href="https://growi.org" className="link-growi-org pl-3">
  265. <span className="growi">GROWI</span>.<span className="org">ORG</span>
  266. </a>
  267. </div>
  268. );
  269. }
  270. }
  271. /**
  272. * Wrapper component for using unstated
  273. */
  274. const LoginFormWrapper = withUnstatedContainers(LoginForm, [AppContainer]);
  275. LoginForm.propTypes = {
  276. // i18next
  277. t: PropTypes.func.isRequired,
  278. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  279. isRegistering: PropTypes.bool,
  280. username: PropTypes.string,
  281. name: PropTypes.string,
  282. email: PropTypes.string,
  283. isRegistrationEnabled: PropTypes.bool,
  284. registrationMode: PropTypes.string,
  285. registrationWhiteList: PropTypes.array,
  286. isLocalStrategySetup: PropTypes.bool,
  287. isLdapStrategySetup: PropTypes.bool,
  288. objOfIsExternalAuthEnableds: PropTypes.object,
  289. };
  290. export default withTranslation()(LoginFormWrapper);