SHARE
TWEET

Untitled

a guest Jun 27th, 2019 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //USER SERVICE
  2. function login(username, password) {
  3.   const requestOptions = {
  4.     method: "POST",
  5.     headers: { "Content-Type": "application/x-www-form-urlencoded" },
  6.     body:
  7.       "username=" + username + "&password=" + password + "&grant_type=password"
  8.   };
  9.  
  10.   return fetch(`${Config.apiUrl}/Token`, requestOptions)
  11.     .then(handleLoginResponse)
  12.     .then(user => {
  13.       // login successful if there's a jwt token in the response
  14.       if (user.access_token) {
  15.         // store user details and jwt token in local storage to keep user logged in between page refreshes
  16.         localStorage.setItem("user", JSON.stringify(user));
  17.       }
  18.  
  19.       return user;
  20.     })
  21.     .catch(error => {
  22.       return { error };
  23.     });
  24. }
  25.  
  26. //AUTH SAGA
  27. export function* login(action) {
  28.   const { username, password } = action.payload;
  29.   const response = userService.login(username, password);
  30.   console.log(response);
  31.   if (response.error) {
  32.     yield put({ type: actions.LOGIN_FAILURE, payload: response.error });
  33.   } else {
  34.     yield put(actions.loginUserSuccessful());
  35.     yield delay(1000);
  36.     // yield history.push("/");
  37.   }
  38. }
  39.  
  40. //AUTH ACTIONS
  41. export function loginUserSuccessful() {
  42.   return { type: LOGIN_SUCCESS };
  43. }
  44.  
  45. //AUTH REDUCER
  46. export function authentication(state = initialState, action) {
  47.   const { payload } = action;
  48.  
  49.   switch (action.type) {
  50.     case actions.LOGIN_SUCCESS:
  51.       return {
  52.         loggedIn: true,
  53.         user: action.user
  54.       };
  55.     case actions.LOGIN_FAILURE:
  56.       return state;
  57.     case actions.USERS_LOGOUT:
  58.       return state;
  59.     case actions.LOGIN_TOGGLE_MODAL:
  60.       const { isLoginModalOpen } = payload;
  61.       return {
  62.         ...state,
  63.         isLoginModalOpen
  64.       };
  65.     default:
  66.       return state;
  67.   }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top