LoginForm.jsx 10 KB

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