Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import { take, call, put, race } from 'redux-saga/effects';
  2. import { forwardTo } from '../utils';
  3. import AuthService from '../services/auth';
  4.  
  5. const auth = new AuthService();
  6.  
  7. import {
  8. SENDING_REQUEST,
  9. LOGIN_REQUEST,
  10. REGISTER_REQUEST,
  11. SET_AUTH,
  12. LOGOUT,
  13. CHANGE_FORM,
  14. REQUEST_ERROR,
  15. USER_PROFILE
  16. } from '../constants/ActionTypes';
  17.  
  18. /**
  19. * Effect to handle authorization
  20. * @param {string} username The username of the user
  21. * @param {string} password The password of the user
  22. * @param {string} email The email of the user
  23. * @param {boolean} options.isRegistering Is this a register request?
  24. */
  25. export function* authorize ({username, password, email, isRegistering}) {
  26. yield put({ type: SENDING_REQUEST, sending: true });
  27.  
  28. // try to register or log in the user, depending on the request
  29. try {
  30. let response;
  31.  
  32. if (isRegistering) {
  33. response = yield call(auth.signup , {username, password, email});
  34. } else {
  35. response = yield call(auth.login, {username, password});
  36. }
  37.  
  38. return response;
  39. } catch (error) {
  40. yield put({ type: REQUEST_ERROR, error });
  41. return false;
  42. } finally {
  43. yield put({ type: SENDING_REQUEST, sending: false });
  44. }
  45. }
  46.  
  47. /**
  48. * Effect to handle logging out
  49. */
  50. export function* logout () {
  51. yield put({ type: SENDING_REQUEST, sending: true });
  52.  
  53. try {
  54. let response = yield call(auth.logout);
  55. yield put({ type: SENDING_REQUEST, sending: false });
  56.  
  57. return response;
  58. } catch (error) {
  59. yield put({ type: REQUEST_ERROR, error });
  60. }
  61. }
  62.  
  63. export function* getProfile () {
  64. const profile = yield call(auth.getProfile);
  65. yield put({ type: SET_AUTH, isAuthenticated: true });
  66. yield put({ type: USER_PROFILE, profile });
  67. yield put({
  68. type: CHANGE_FORM,
  69. newFormState: {
  70. username: '',
  71. password: '',
  72. email: ''
  73. }
  74. });
  75. }
  76.  
  77. /**
  78. * Log in saga
  79. */
  80. export function* loginFlow () {
  81. while (true) {
  82. let request = yield take(LOGIN_REQUEST);
  83. let { username, password } = request.data;
  84.  
  85. let winner = yield race({
  86. auth: call(authorize, { username, password, isRegistering: false }),
  87. logout: take(LOGOUT)
  88. });
  89.  
  90. if (winner.auth) {
  91. yield call(getProfile);
  92. forwardTo('/dashboard');
  93. } else if (winner.logout) {
  94. yield put({ type: SET_AUTH, isAuthenticated: false });
  95. yield call(logout);
  96.  
  97. forwardTo('/');
  98. }
  99. }
  100. }
  101.  
  102. /**
  103. * Log out saga
  104. * This is basically the same as the `if (winner.logout)` of above, just written
  105. * as a saga that is always listening to `LOGOUT` actions
  106. */
  107. export function* logoutFlow () {
  108. while (true) {
  109. yield take(LOGOUT);
  110. yield put({ type: SET_AUTH, isAuthenticated: false });
  111.  
  112. yield call(logout);
  113. forwardTo('/');
  114. }
  115. }
  116.  
  117. /**
  118. * Register saga
  119. * Very similar to log in saga!
  120. */
  121. export function* registerFlow () {
  122.  
  123. while (true) {
  124. let request = yield take(REGISTER_REQUEST);
  125. let { username, password, name, email } = request.data;
  126.  
  127. let wasSuccessful = yield call(authorize, {
  128. username, password, name, email, isRegistering: true
  129. });
  130.  
  131. if (wasSuccessful) {
  132. yield put({ type: SET_AUTH, isAuthenticated: true });
  133. yield put({
  134. type: CHANGE_FORM,
  135. newFormState: {
  136. username: '',
  137. password: '',
  138. email: ''
  139. }
  140. });
  141.  
  142. forwardTo('/dashboard');
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement