LoginForm.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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, isLdapStrategySetup } = 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. {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-2112] 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-2112] use onClick, and delete form tag */}
  94. <form role="form" action={`/passport/${auth}`} className="d-inline-flex flex-column">
  95. {/* [TODO][GW-2112] 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 { isLocalStrategySetup, isLdapStrategySetup, objOfIsExternalAuthEnableds } = this.props;
  111. const isExternalAuthCollapsible = isLocalStrategySetup || isLdapStrategySetup;
  112. const collapsibleClass = isExternalAuthCollapsible ? 'collapse collapse-external-auth collapse-anchor' : '';
  113. return (
  114. <>
  115. <div className="border-bottom"></div>
  116. <div id="external-auth" className={`external-auth ${collapsibleClass}`}>
  117. <div className="spacer"></div>
  118. <div className="d-flex flex-row justify-content-between flex-wrap">
  119. {Object.keys(objOfIsExternalAuthEnableds).map((auth) => {
  120. if (!objOfIsExternalAuthEnableds[auth]) {
  121. return;
  122. }
  123. return this.renderExternalAuthInput(auth);
  124. })}
  125. </div>
  126. <div className="spacer"></div>
  127. </div>
  128. <div className="border-bottom"></div>
  129. <div className="text-center">
  130. <button
  131. type="button"
  132. className="collapse-anchor btn btn-xs btn-collapse-external-auth mb-3"
  133. data-toggle={isExternalAuthCollapsible ? 'collapse' : ''}
  134. data-target="#external-auth"
  135. aria-expanded="false"
  136. aria-controls="external-auth"
  137. >
  138. External Auth
  139. </button>
  140. </div>
  141. </>
  142. );
  143. }
  144. renderRegisterForm() {
  145. const {
  146. t,
  147. csrf,
  148. registrationMode,
  149. registrationWhiteList,
  150. } = this.props;
  151. return (
  152. <div className="back">
  153. {registrationMode === 'Restricted' && (
  154. <p className="alert alert-warning">
  155. {t('page_register.notice.restricted')}
  156. <br />
  157. {t('page_register.notice.restricted_defail')}
  158. </p>
  159. )}
  160. <form role="form" action="/register" method="post" id="register-form">
  161. <div className="input-group" id="input-group-username">
  162. <div className="input-group-prepend">
  163. <span className="input-group-text">
  164. <i className="icon-user"></i>
  165. </span>
  166. </div>
  167. <input type="text" className="form-control" placeholder={t('User ID')} name="registerForm[username]" defaultValue={this.props.username} required />
  168. </div>
  169. <p className="form-text text-danger">
  170. <span id="help-block-username"></span>
  171. </p>
  172. <div className="input-group">
  173. <div className="input-group-prepend">
  174. <span className="input-group-text">
  175. <i className="icon-tag"></i>
  176. </span>
  177. </div>
  178. <input type="text" className="form-control" placeholder={t('Name')} name="registerForm[name]" defaultValue={this.props.name} required />
  179. </div>
  180. <div className="input-group">
  181. <div className="input-group-prepend">
  182. <span className="input-group-text">
  183. <i className="icon-envelope"></i>
  184. </span>
  185. </div>
  186. <input type="email" className="form-control" placeholder={t('Email')} name="registerForm[email]" defaultValue={this.props.email} required />
  187. </div>
  188. {registrationWhiteList.length > 0 && (
  189. <>
  190. <p className="form-text">{t('page_register.form_help.email')}</p>
  191. <ul>
  192. {registrationWhiteList.map((elem) => {
  193. return (
  194. <li key={elem}>
  195. <code>{elem}</code>
  196. </li>
  197. );
  198. })}
  199. </ul>
  200. </>
  201. )}
  202. <div className="input-group">
  203. <div className="input-group-prepend">
  204. <span className="input-group-text">
  205. <i className="icon-lock"></i>
  206. </span>
  207. </div>
  208. <input type="password" className="form-control" placeholder={t('Password')} name="registerForm[password]" required />
  209. </div>
  210. <div className="input-group justify-content-center mt-5">
  211. {/* [TODO][GW-2112] An AppContainer gets csrf data */}
  212. <input type="hidden" name="_csrf" value={csrf} />
  213. <button type="submit" className="btn btn-fill px-0 py-2" id="register">
  214. <div className="eff"></div>
  215. <span className="btn-label p-3">
  216. <i className="icon-user-follow"></i>
  217. </span>
  218. <span className="btn-label-text p-3">{t('Sign up')}</span>
  219. </button>
  220. </div>
  221. </form>
  222. <div className="border-bottom mb-3"></div>
  223. <div className="row">
  224. <div className="text-right col-12 py-1">
  225. <a href="#login" id="login" className="link-switch" onClick={this.onClickSwitchFormBtn}>
  226. <i className="icon-fw icon-login"></i>
  227. {t('Sign in is here')}
  228. </a>
  229. </div>
  230. </div>
  231. </div>
  232. );
  233. }
  234. render() {
  235. const {
  236. t,
  237. isRegistering,
  238. isLocalStrategySetup,
  239. isLdapStrategySetup,
  240. isRegistrationEnabled,
  241. objOfIsExternalAuthEnableds,
  242. } = this.props;
  243. const isLocalOrLdapStrategiesEnabled = isLocalStrategySetup || isLdapStrategySetup;
  244. const registerFormClass = isRegistering ? 'to-flip' : '';
  245. const isSomeExternalAuthEnabled = Object.values(objOfIsExternalAuthEnableds).some(elem => elem);
  246. return (
  247. <div className={`login-dialog mx-auto flipper ${registerFormClass}`} id="login-dialog">
  248. <div className="row mx-0">
  249. <div className="col-12">
  250. <div className="front">
  251. {isLocalOrLdapStrategiesEnabled && this.renderLocalOrLdapLoginForm()}
  252. {isSomeExternalAuthEnabled && this.renderExternalAuthLoginForm()}
  253. {isRegistrationEnabled && (
  254. <div className="row">
  255. <div className="col-12 text-right py-2">
  256. <a href="#register" id="register" className="link-switch" onClick={this.onClickSwitchFormBtn}>
  257. <i className="ti-check-box"></i> {t('Sign up is here')}
  258. </a>
  259. </div>
  260. </div>
  261. )}
  262. </div>
  263. {isRegistrationEnabled && this.renderRegisterForm()}
  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. </div>
  269. </div>
  270. );
  271. }
  272. }
  273. LoginForm.propTypes = {
  274. // i18next
  275. t: PropTypes.func.isRequired,
  276. username: PropTypes.string,
  277. name: PropTypes.string,
  278. email: PropTypes.string,
  279. csrf: PropTypes.string,
  280. isRegistrationEnabled: PropTypes.bool,
  281. registrationMode: PropTypes.string,
  282. registrationWhiteList: PropTypes.array,
  283. isLocalStrategySetup: PropTypes.bool,
  284. isLdapStrategySetup: PropTypes.bool,
  285. objOfIsExternalAuthEnableds: PropTypes.object,
  286. };
  287. export default withTranslation()(LoginForm);