Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import api from './../helpers/api'
  2. import {addNotification} from './notifications'
  3. import cookie from 'react-cookie'
  4. import {push} from 'redux-router'
  5.  
  6. const AUTH_SUCCESS = 'auth/signin/success'
  7. const AUTH_LOADING = 'auth/signin/loading'
  8. const AUTH_FAILURE = 'auth/signin/failure'
  9. const LOGOUT = 'auth/logout'
  10.  
  11. const UPDATE_USER_INFORMATION = 'user/update/information'
  12.  
  13. function signIn(email, password, redirect = '/') {
  14. return (dispatch, getState, api) => {
  15. api.post('/user/signin', {
  16. email: email,
  17. password: password
  18. })
  19. .then(data => data.data)
  20. .then(data => {
  21. dispatch(authSuccess(data))
  22. dispatch(push(redirect))
  23. },
  24. error => {
  25. if(error.data && error.data.message) {
  26. dispatch(authFailure(error.data.message))
  27. }else{
  28. dispatch(authFailure(error.data))
  29. }
  30. })
  31. }
  32. }
  33.  
  34. function signUp(d, redirect = '/') {
  35. console.log(d)
  36. return (dispatch, getState, api) => {
  37. api.post('/user/signup', d)
  38. .then(data => data.data)
  39. .then(data => {
  40. console.log(data)
  41. dispatch(addNotification('success', 'You successfully registered'))
  42. dispatch(authSuccess(data))
  43. dispatch(push(redirect))
  44. },
  45. error => {
  46. if(error.data && error.data.message) {
  47. dispatch(authFailure(error.data.message))
  48. }else{
  49. dispatch(authFailure(error.data))
  50. }
  51. })
  52. }
  53. }
  54.  
  55. function checkToken(token) {
  56. return (dispatch, getState, api) => {
  57. api.get(`/user/check?token=${token}`)
  58. .then(data => data.data)
  59. .then(data => {
  60. dispatch(authSuccess(data, token))
  61. })
  62. .catch(error => {
  63. if(error.data && error.data.message) {
  64. dispatch(authFailure(error.data.message))
  65. }else{
  66. dispatch(authFailure(error.data))
  67. }
  68. })
  69. }
  70. }
  71.  
  72. function logout() {
  73. localStorage.removeItem('token');
  74. return (dispatch, getState) => {
  75. let state = getState()
  76. console.log(state.user)
  77. dispatch(addNotification('success', `Good bye, ${state.user.user.username}`))
  78. dispatch({type: LOGOUT, logout: true})
  79. }
  80. }
  81.  
  82. function authSuccess(data, token = data.token) {
  83. localStorage.setItem('token', token)
  84. return dispatch => {
  85. dispatch(addNotification('success', `Welcome back, ${data.username}`))
  86. dispatch({
  87. type: AUTH_SUCCESS,
  88. user: data.user,
  89. token: token
  90. })
  91. }
  92. }
  93.  
  94. function authFailure(error) {
  95. localStorage.removeItem('token')
  96. return dispatch => {
  97. dispatch(addNotification('danger', `Error: ${error}`))
  98. dispatch({
  99. type: AUTH_FAILURE,
  100. error: error
  101. })
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108. function updateUserInformation(data) {
  109. return dispatch => {
  110. dispatch({
  111. type: UPDATE_USER_INFORMATION
  112. })
  113.  
  114. }
  115. }
  116.  
  117.  
  118. export {
  119. AUTH_LOADING, AUTH_SUCCESS, AUTH_FAILURE, LOGOUT,
  120. UPDATE_USER_INFORMATION,
  121. signIn, signUp, checkToken, logout,
  122. updateUserInformation
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement