LoginForm.jsx 10 KB

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