Guest User

Untitled

a guest
Apr 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. REDIRECT_URL,
  4. AUTH_URL,
  5. getUserInfo,
  6. saveUserInfo
  7. } from '../constants';
  8.  
  9. export default class App extends React.Component {
  10.  
  11. constructor() {
  12. super();
  13. this.state = {
  14. loading: true,
  15. error: null,
  16. userInfo: null
  17. }
  18. }
  19.  
  20. showLoadingIndicator = (shouldShow) => {
  21. this.setState({
  22. ...this.state,
  23. loading: shouldShow
  24. })
  25. }
  26.  
  27. componentDidMount() {
  28. const userInfo = getUserInfo();
  29. if (userInfo !== null) {
  30. this.setState({
  31. ...this.state,
  32. loading: false,
  33. userInfo: userInfo
  34. })
  35. return;
  36. }
  37.  
  38. this.showLoadingIndicator(true);
  39. var url = AUTH_URL + '/v1/user/info';
  40. var requestOptions = {
  41. "method": "GET",
  42. "headers": {
  43. "Content-Type": "application/json"
  44. },
  45. "credentials": "include"
  46. };
  47.  
  48. fetch(url, requestOptions)
  49. .then(response => {
  50. if (response.ok) {
  51. return response.json();
  52. } else {
  53. window.location = AUTH_URL + '/ui?redirect_url=' + REDIRECT_URL;
  54. }
  55. })
  56. .then(json => {
  57. saveUserInfo(json);
  58. this.setState({
  59. ...this.state,
  60. loading: false,
  61. userInfo: json
  62. });
  63. })
  64. .catch(error => {
  65. this.setState({
  66. ...this.state,
  67. loading: false,
  68. error: error
  69. });
  70. });
  71. }
  72.  
  73. render() {
  74. if (this.state.loading) {
  75. return (
  76. <div>Loading. Please wait...</div>
  77. );
  78. }
  79. if (this.state.error) {
  80. return (
  81. <div>{this.state.error}</div>
  82. );
  83. }
  84. return (
  85. <div>LoggedIn UserId: {this.state.userInfo.hasura_id}</div>
  86. )
  87. }
  88. }
Add Comment
Please, Sign In to add comment