Skip to content

Commit ebfcad6

Browse files
committed
use axios instead of fetch
1 parent 4694fdb commit ebfcad6

6 files changed

Lines changed: 5919 additions & 37 deletions

File tree

03-Calling-an-API/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"auth0-js": "^8.7.0",
10+
"axios": "^0.16.2",
1011
"bootstrap": "^3.3.7",
1112
"cors": "^2.8.1",
1213
"dotenv": "^4.0.0",

03-Calling-an-API/src/Auth/Auth.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export default class Auth {
2121
this.isAuthenticated = this.isAuthenticated.bind(this);
2222
this.getAccessToken = this.getAccessToken.bind(this);
2323
this.getProfile = this.getProfile.bind(this);
24-
this.authFetch = this.authFetch.bind(this);
2524
}
2625

2726
login() {
@@ -87,29 +86,4 @@ export default class Auth {
8786
let expiresAt = JSON.parse(localStorage.getItem('expires_at'));
8887
return new Date().getTime() < expiresAt;
8988
}
90-
91-
authFetch(url, options) {
92-
const headers = {
93-
Accept: 'application/json',
94-
'Content-Type': 'application/json'
95-
};
96-
97-
if (this.isAuthenticated()) {
98-
headers['Authorization'] = 'Bearer ' + this.getAccessToken();
99-
}
100-
101-
return fetch(url, { headers, ...options })
102-
.then(this.checkStatus)
103-
.then(response => response.json());
104-
}
105-
106-
checkStatus(response) {
107-
if (response.status >= 200 && response.status < 300) {
108-
return response;
109-
} else {
110-
let error = new Error(response.statusText);
111-
error.response = response;
112-
throw error;
113-
}
114-
}
11589
}

03-Calling-an-API/src/Auth/auth0-variables.js.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export const AUTH_CONFIG = {
22
domain: '{DOMAIN}',
33
clientId: '{CLIENT_ID}',
44
callbackUrl: 'http://localhost:3000/callback',
5-
apiUrl: {API_IDENTIFIER}
5+
apiUrl: '{API_IDENTIFIER}'
66
}

03-Calling-an-API/src/Home/Home.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React, { Component } from 'react';
2-
import { Link } from 'react-router-dom';
32

43
class Home extends Component {
4+
login() {
5+
this.props.auth.login();
6+
}
57
render() {
68
const { isAuthenticated } = this.props.auth;
79
return (
@@ -17,11 +19,12 @@ class Home extends Component {
1719
!isAuthenticated() && (
1820
<h4>
1921
You are not logged in! Please{' '}
20-
<Link
21-
to={'/login'}
22+
<a
23+
style={{ cursor: 'pointer' }}
24+
onClick={this.login.bind(this)}
2225
>
2326
Log In
24-
</Link>
27+
</a>
2528
{' '}to continue.
2629
</h4>
2730
)

03-Calling-an-API/src/Ping/Ping.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import React, { Component } from 'react';
22
import { Button } from 'react-bootstrap';
33
import { API_URL } from './../constants';
4+
import axios from 'axios';
45

56
class Ping extends Component {
67
componentWillMount() {
78
this.setState({ message: '' });
89
}
910
ping() {
10-
fetch(`${API_URL}/public`)
11-
.then(res => res.json())
12-
.then(data => this.setState({ message: data.message }));
11+
axios.get(`${API_URL}/public`)
12+
.then(response => this.setState({ message: response.data.message }))
13+
.catch(error => this.setState({ message: error.message }));
1314
}
1415
securedPing() {
15-
const { authFetch } = this.props.auth;
16-
authFetch(`${API_URL}/private`)
17-
.then(data => this.setState({ message: data.message }))
16+
const { getAccessToken } = this.props.auth;
17+
const headers = { 'Authorization': `Bearer ${getAccessToken()}`}
18+
axios.get(`${API_URL}/private`, { headers })
19+
.then(response => this.setState({ message: response.data.message }))
1820
.catch(error => this.setState({ message: error.message }));
1921
}
2022
render() {

0 commit comments

Comments
 (0)