File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11import React , { Component } from 'react' ;
2- import { Link } from 'react-router-dom' ;
32
43class 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 )
Original file line number Diff line number Diff line change 11import React , { Component } from 'react' ;
22import { Button } from 'react-bootstrap' ;
33import { API_URL } from './../constants' ;
4+ import axios from 'axios' ;
45
56class 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 ( ) {
You can’t perform that action at this time.
0 commit comments