Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. handleClick() {
  2.  
  3. fetch('/backend/login', {
  4. method: 'POST',
  5. headers: {
  6. 'Accept': 'application/json',
  7. 'Content-Type': 'application/json',
  8. },
  9. body: JSON.stringify({
  10. username: this.state.username,
  11. password: this.state.password
  12. })
  13. })
  14. .then(response =>{
  15. if (response.ok) {
  16. response.json().then(data => {
  17. sessionStorage.setItem('loggedIn', data.success);
  18. console.log('login success: ' + sessionStorage.getItem('loggedIn'));
  19. });
  20. }
  21. else {
  22. console.log('failed');
  23. }
  24. })
  25. }
  26.  
  27. render () {
  28. <LinkContainer to='/Dashboard'>
  29. <Button onClick={this.handleClick} type="submit"> Submit />
  30. </Button>
  31. </LinkContainer>
  32. }
  33.  
  34. function requireAuth(nextState, replace) {
  35. if (!(sessionStorage.getItem('loggedIn') === true)) {
  36. console.log(sessionStorage.getItem('loggedIn'));
  37. replace({
  38. pathname: '/',
  39. state: { nextPathname: nextState.location.pathname }
  40. });
  41. }
  42. }
  43.  
  44. ReactDOM.render(
  45. <Router history={browserHistory}>
  46. <Route path="/" component={Container}>
  47. <IndexRoute component={Homepage}/>
  48. <Route path="dashboard" component={Dashboard} onEnter=
  49. {requireAuth}/>
  50. </Route>
  51. </Router>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement