IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.8KB

  1. //
  2. // Copyright (C) Stanislaw Adaszewski, 2020
  3. // Contact: s.adaszewski@gmail.com
  4. // Website: https://adared.ch/wba
  5. // License: GNU Affero General Public License, Version 3
  6. //
  7. import { h, Component } from 'preact';
  8. import { route } from 'preact-router';
  9. import WBNavbar from 'wb-navbar';
  10. import WBTabs from 'wb-tabs';
  11. import linkState from 'linkstate';
  12. import makeArvadosRequest from 'make-arvados-request';
  13. class WBSignIn extends Component {
  14. constructor(...args) {
  15. super(...args);
  16. const search = new URLSearchParams(window.location.search);
  17. this.state.arvHost = window.localStorage.arvHost;
  18. this.state.arvToken = search.get('api_token');
  19. }
  20. componentDidMount() {
  21. // const arvHost = window.localStorage.arvHost;
  22. // const arvToken = search.get('api_token');
  23. const { arvHost, arvToken } = this.state;
  24. if (arvHost && arvToken) {
  25. this.state.arvHost = arvHost;
  26. this.state.arvToken = arvToken;
  27. this.submitToken();
  28. }
  29. }
  30. submit() {
  31. const { mode } = this.props;
  32. if (mode === 'token')
  33. this.submitToken();
  34. else if (!mode || mode === 'sso')
  35. this.submitSingleSignOn();
  36. else
  37. throw Error('Unsupported mode');
  38. }
  39. submitSingleSignOn() {
  40. const { arvHost } = this.state;
  41. window.localStorage.arvHost = arvHost;
  42. window.location = 'https://' + arvHost + '/login?return_to='
  43. + encodeURIComponent(window.location.protocol + '//' + window.location.host + '/sign-in/token');
  44. }
  45. submitToken() {
  46. let { appState } = this.props;
  47. let { arvHost, arvToken } = this.state;
  48. let prom = makeArvadosRequest(arvHost, arvToken, '/arvados/v1/users/current');
  49. prom = prom.then(xhr => {
  50. window.localStorage['arvHost'] = arvHost;
  51. window.localStorage['arvToken'] = arvToken;
  52. window.localStorage['currentUser'] = JSON.stringify(xhr.response);
  53. appState.arvHost = arvHost;
  54. appState.arvToken = arvToken;
  55. appState.currentUser = xhr.response;
  56. route('/browse/' + xhr.response['uuid']);
  57. });
  58. prom = prom.catch(() => {
  59. alert('Sign in unsuccessful. Verify your input and try again.')
  60. });
  61. }
  62. render({ mode }, { arvHost, arvToken }) {
  63. return (
  64. <div>
  65. <WBNavbar />
  66. <div class="container my-3">
  67. <div class="row justify-content-center">
  68. <div class="col-6">
  69. <h1>Sign In</h1>
  70. <WBTabs class="my-3" tabs={ [ { name: 'SSO', isActive: (!mode || mode === 'sso') },
  71. { name: 'Token', isActive: (mode === 'token') } ] }
  72. onTabChanged={ t => route(t.name === 'Token' ? '/sign-in/token' : '/sign-in/sso') } />
  73. <form>
  74. <div class="form-group">
  75. <label for="arvHost">Arvados API Host</label>
  76. <input type="text" class="form-control" id="arvHost"
  77. placeholder="Enter Arvados API Host"
  78. value={ arvHost }
  79. onInput={ linkState(this, 'arvHost') } />
  80. </div>
  81. { mode === 'token' ? (
  82. <div class="form-group">
  83. <label for="arvToken">Token</label>
  84. <input type="text" class="form-control" id="arvToken"
  85. placeholder="Enter Arvados API Token"
  86. value={ arvToken }
  87. onInput={ linkState(this, 'arvToken') } />
  88. </div>
  89. ) : null }
  90. <button type="submit" class="btn btn-primary"
  91. onclick={ e => { e.preventDefault(); this.submit(); } }>Submit</button>
  92. </form>
  93. </div>
  94. </div>
  95. </div>
  96. </div>
  97. );
  98. }
  99. }
  100. export default WBSignIn;