-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (68 loc) · 1.6 KB
/
index.js
File metadata and controls
72 lines (68 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Auth from '../Auth'
import jumpTo from '../Navigation'
import axios from 'axios'
import qs from 'qs'
import paypalConfig from '../../configs/paypalConfig'
//const URL = 'https://zack-ecommerce-nodejs.herokuapp.com'
const URL = 'http://localhost:4000'
const serverCall = (config) => {
//header authorization
if (Auth.user_token) {
const token = Auth.getToken()
config.headers = {
"authorization": token
}
}
//interceptors handle network error
axios.interceptors.response.use(
(response) => {
return response;
},
function (error) {
if (!error.response) {
error.response = {
data: 'net work error',
status: 500
}
}
if(error.response.status===401){
Auth.logout()
jumpTo('/login')
throw error
}
return Promise.reject(error);
});
config.baseURL = URL
return axios(config)
}
export default serverCall
export const login = (email, password) => {
const body =
{
"credential": {
"email": email,
"password": password
}
}
return serverCall({
method: 'POST',
url: '/users/login',
data: body
})
.then(res => {
Auth.setUserToken(res.data.user_token)
return res
})
}
export const getPaypalToken = () => {
return axios({
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
auth: {
username: paypalConfig.username,
password: paypalConfig.password
},
data: qs.stringify({ "grant_type": "client_credentials" })
})
}