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.

wb-sign-in.js 3.6KB

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