Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. export const USER_LOGIN = 'user_login_action';
  2. export const USER_LOGINING = 'logining_users';
  3. export const USER_LOGEDIN = 'user_logged_in';
  4.  
  5. import axios from 'axios';
  6. import { USER_LOGIN, USER_WALLETS,USER_LOGINING } from './api_types';
  7.  
  8. const AUTH_API_URL = 'http:/api/v1';
  9. const CORE_API_URL = 'http:/api/v1';
  10. let username="";
  11. let password="";
  12. let auth_token ="";
  13. let AuthStr = "";
  14.  
  15. export function UserWallets(){
  16.  
  17. return function(dispatch){
  18. AuthStr ="Bearer "+auth_token;
  19. console.log ("new auth : "+AuthStr);
  20. axios.defaults.headers.common['Authorization'] = AuthStr
  21. axios.get(`${CORE_API_URL}/wallet/allwallets`)
  22. .then(response => {
  23. dispatch({
  24. type: USER_WALLETS,
  25. payload: response['data']
  26. });
  27. }).catch((error) => {
  28. console.log(error);
  29. })
  30. }
  31. }
  32. export function UserLogin() {
  33. return function(dispatch) {
  34. dispatch({
  35. type:USER_LOGINING
  36. });
  37.  
  38. axios.post(
  39. `${AUTH_API_URL}/authenticate/users`,
  40. {
  41. email: username,
  42. password: password
  43. }
  44.  
  45. )
  46. .then(response => {
  47. dispatch({
  48. type: USER_LOGIN,
  49. payload: response['data']
  50. });
  51. auth_token=response['data']['token'];
  52.  
  53. }
  54.  
  55. )
  56. .catch((error) => {
  57. console.log(error);
  58. })
  59. }
  60. }
  61.  
  62. export function username(term) {
  63. username=term;
  64. console.log("username " +username);
  65. return{
  66. type:"username",
  67. username
  68. };
  69. }
  70. export function password(term) {
  71. password=term;
  72. console.log("password " +password);
  73. return{
  74. type:"password",
  75. password
  76. };
  77. }
  78. export function authToken (term){
  79. auth_token = term;
  80. return{
  81. type:"authtoken",
  82. auth_token
  83. }
  84. }
  85.  
  86. import { USER_LOGIN ,USER_LOGINING } from '../actions/api_types';
  87.  
  88. const INTIAL_STATE = {
  89. message: '',
  90. token:'',
  91. logging: false,
  92. loggedin: false,
  93. loginerr: null,
  94. };
  95.  
  96. export default function (state = INTIAL_STATE, action) {
  97. console.log("present state"+action.type);
  98. switch(action.type) {
  99. case USER_LOGIN:{
  100. return { ...state, message: action.payload.message, token:action.payload.token,loggedin:true};
  101. }
  102. case USER_LOGINING:{
  103. return {...state,logging:true }
  104. }
  105. default:{
  106. console.log("default "+action.type);
  107. }
  108. }
  109. return state;
  110. }
  111.  
  112. import { combineReducers } from 'redux';
  113.  
  114. import drawer from './drawer';
  115. import AuthReducer from './auth_reducer';
  116. import CoreReducer from './core_reducer';
  117.  
  118.  
  119. export default combineReducers({
  120. auth: AuthReducer,
  121.  
  122. });
  123.  
  124. login(){
  125. if(this.state.email==""){
  126. alert("Email require");
  127. return;
  128. }else if(this.state.password==""){
  129. alert("password require");
  130. return;
  131. }else{
  132. //set the paramter for the reducer to use
  133. this.props.username(this.state.email);
  134. this.props.password(this.state.password);
  135. //activate the user login action
  136.  
  137. this.props.UserLogin();
  138.  
  139. if(!this.props.auth.loggedin){
  140. console.log("logging in");
  141. //show loadging gif
  142. }
  143. //checking from response from the auth api
  144. if(this.props.auth.message=="user successfully logged in"){
  145. alert(this.props.auth.token);
  146. Actions.home();
  147. }else{
  148. alert("invalid Username/Password");
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement