Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. //===================ACTION CREATOR=====================//
  2. import axios from 'axios';
  3. import {
  4. USER_LOGIN_SUCCESS,
  5. USER_NOT_FOUND,
  6. LOGIN_SYSTEM_ERROR,
  7. LOGIN_LOADING,
  8. REGISTER_LOADING,
  9. LOGOUT
  10. } from './types';
  11.  
  12. export const onUserRegister = ({ username, email, phone, password }) => {
  13. return ( dispatch ) => {
  14. //=============INSERT USERNAME & PASSWORD KE JSON==============//
  15. dispatch({ type: REGISTER_LOADING });
  16.  
  17. axios.post('http://localhost:1988/users', {
  18. username: username,
  19. email: email,
  20. phone: phone,
  21. password: password
  22. })
  23. .then((res) => {
  24. console.log(res);
  25. })
  26. .catch((err) => {
  27. console.log(err);
  28. })
  29.  
  30. }
  31. }
  32.  
  33. export const onUserLogout = () => {
  34. return { type: LOGOUT }
  35. }
  36.  
  37. export const onUserLogin = ({ username, password }) => {
  38.  
  39. return ( dispatch ) => {
  40.  
  41. //=============VALIDASI USERNAME & PASSWORD KE JSON==============//
  42. dispatch({ type: LOGIN_LOADING });
  43.  
  44. axios.get('http://localhost:1988/users', {
  45. params: {
  46. username: username,
  47. password: password
  48. }
  49. })
  50. .then((res) => {
  51. console.log(res);
  52. if(res.data.length > 0) {
  53. dispatch({ type: USER_LOGIN_SUCCESS, payload: username });
  54. } else {
  55. dispatch({ type: USER_NOT_FOUND });
  56. }
  57. })
  58. .catch((err) => {
  59. console.log(err);
  60. dispatch({ type: LOGIN_SYSTEM_ERROR });
  61. })
  62.  
  63. }
  64.  
  65. }
  66.  
  67. export const keepLogin = (username) => {
  68. return { type: USER_LOGIN_SUCCESS, payload: username }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement