Skip to content

Commit a440993

Browse files
committed
update ping
1 parent c43fed4 commit a440993

3 files changed

Lines changed: 9 additions & 32 deletions

File tree

04-Authorization/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.3",
1213
"dotenv": "^4.0.0",

04-Authorization/src/Auth/Auth.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export default class Auth {
2323
this.userHasScopes = this.userHasScopes.bind(this);
2424
this.getAccessToken = this.getAccessToken.bind(this);
2525
this.getProfile = this.getProfile.bind(this);
26-
this.authFetch = this.authFetch.bind(this);
2726
}
2827

2928
login() {
@@ -102,29 +101,4 @@ export default class Auth {
102101
const grantedScopes = JSON.parse(localStorage.getItem('scopes')).split(' ');
103102
return scopes.every(scope => grantedScopes.includes(scope));
104103
}
105-
106-
authFetch(url, options) {
107-
const headers = {
108-
Accept: 'application/json',
109-
'Content-Type': 'application/json'
110-
};
111-
112-
if (this.isAuthenticated()) {
113-
headers['Authorization'] = 'Bearer ' + this.getAccessToken();
114-
}
115-
116-
return fetch(url, { headers, ...options })
117-
.then(this.checkStatus)
118-
.then(response => response.json());
119-
}
120-
121-
checkStatus(response) {
122-
if (response.status >= 200 && response.status < 300) {
123-
return response;
124-
} else {
125-
let error = new Error(response.statusText);
126-
error.response = response;
127-
throw error;
128-
}
129-
}
130104
}

04-Authorization/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)