Last active
December 23, 2021 19:22
-
-
Save shooksm/ef77a1600bcb435a7d6d27367d778a98 to your computer and use it in GitHub Desktop.
Axios interceptor setup component for use with @okta/okta-react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2019 Matthew Shooks | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import axios from 'axios'; | |
import PropTypes from 'prop-types'; | |
import { withAuth } from '@okta/okta-react'; | |
import { withRouter } from 'react-router-dom'; | |
import { get } from 'lodash'; | |
export class UndecoratedSetupAxios extends React.Component { | |
static propTypes = { | |
auth: PropTypes.object.isRequired, | |
history: PropTypes.object.isRequired, | |
location: PropTypes.object.isRequired, | |
match: PropTypes.object.isRequired, | |
axios: PropTypes.func, | |
children: PropTypes.node, | |
requestInterceptorHandler: PropTypes.func, | |
requestInterceptorErrorHandler: PropTypes.func, | |
responseInterceptorSuccessHandler: PropTypes.func, | |
responseInterceptorErrorHandler: PropTypes.func, | |
}; | |
static defaultProps = { | |
axios, | |
children: null, | |
requestInterceptorHandler: config => config, | |
requestInterceptorErrorHandler: error => Promise.reject(error), | |
responseInterceptorSuccessHandler: response => response, | |
responseInterceptorErrorHandler: error => Promise.reject(error), | |
}; | |
static displayName = 'SetupAxios'; | |
requestInterceptor = null; | |
responseInterceptor = null; | |
componentDidMount() { | |
this.requestInterceptor = this.props.axios.interceptors.request.use( | |
this.requestInterceptorSuccessHandler, | |
this.requestInterceptorErrorHandler | |
); | |
this.responseInterceptor = this.props.axios.interceptors.response.use( | |
this.responseInterceptorSuccessHandler, | |
this.responseInterceptorErrorHandler | |
); | |
} | |
componentWillUnmount() { | |
this.props.axios.interceptors.request.eject(this.requestInterceptor); | |
this.props.axios.interceptors.response.eject(this.responseInterceptor); | |
} | |
requestInterceptorSuccessHandler = async config => { | |
const token = await this.props.auth.getAccessToken(); | |
// If not able to retrieve a token, send the user back to login | |
if (typeof token === 'undefined') { | |
this.props.auth.login( | |
`${this.props.location.pathname}${this.props.location.search}${ | |
this.props.location.hash | |
}` | |
); | |
return config; | |
} | |
// Process the user supplied requestInterceptorHandler | |
const newConfig = this.props.requestInterceptorHandler(config); | |
// Return the config with the token appended to the Authorization Header | |
return { | |
...newConfig, | |
headers: { | |
...get(newConfig, 'headers', {}), | |
Authorization: `Bearer ${token}`, | |
}, | |
}; | |
}; | |
requestInterceptorErrorHandler = error => | |
this.props.requestInterceptorErrorHandler(error); | |
responseInterceptorSuccessHandler = response => | |
this.props.responseInterceptorSuccessHandler(response); | |
responseInterceptorErrorHandler = error => { | |
if (get(error, 'response.status') === 401) { | |
this.props.auth.login( | |
`${this.props.location.pathname}${this.props.location.search}${ | |
this.props.location.hash | |
}` | |
); | |
} | |
return this.props.responseInterceptorErrorHandler(error); | |
}; | |
render() { | |
return this.props.children; | |
} | |
} | |
export default withRouter(withAuth(UndecoratedSetupAxios)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
withAuth is no longer used in okta react library. By adding withOktaAuth I get an error at this step
const token = await this.props.auth.getAccessToken();