I am trying to port the front end of our React application today. But the withAuth component wrapper does not seem to be passing the ‘auth’ object as a prop. It is undefined.
Here is my application container:
import React, { Component, PropTypes } from ‘react’;
import { Route, Router } from ‘react-router’;
import { Provider } from ‘react-redux’;
import { Security, ImplicitCallback } from ‘@okta/okta-react’;
import { config } from ‘…/modules/auth’;
import TestView from ‘…/routes/Test/TestView’;
var $ = require(‘jquery’);
class AppContainer extends Component {
static propTypes = {
routes : PropTypes.object.isRequired,
store : PropTypes.object.isRequired
}
shouldComponentUpdate () {
return false
}
render () {
const { store, history } = this .props
return (
<Provider store={store}>
<div style={{ height: ‘100%’ }}>
<Router history={history}>
<Security issuer={config.issuer} client_id={config.client_id} redirect_uri={config.redirect_uri}>
<Route path=’/’ exact={ true } component={TestView} store={store} />
<Route path=’/implicit/callback’ component={ImplicitCallback}/>
</Security>
</Router>
</div>
</Provider>
)
}
}
export default AppContainer
And here is the test component:
import React, { Component } from ‘react’;
import { connect } from ‘react-redux’;
import { withAuth } from ‘@okta/okta-react’;
var $ = require(‘jquery’);
const mapStateToProps = (state) => {
}
const mapDispatchToProps = {
}
class TestView extends Component {
constructor(props) {
super (props);
if (!props.auth){
console.log(‘auth not provided’);
}
this .state = { authenticated: null };
this .checkAuthentication = this .checkAuthentication.bind( this );
this .checkAuthentication();
this .login = this .login.bind( this );
this .logout = this .logout.bind( this );
}
async checkAuthentication() {
const authenticated = await this .props.auth.isAuthenticated();
if (authenticated !== this .state.authenticated) {
this .setState({ authenticated });
}
}
componentDidUpdate() {
this .checkAuthentication();
}
async login() {
// Redirect to ‘/’ after login
this .props.auth.login(’/’);
}
async logout() {
// Redirect to ‘/’ after logout
this .props.auth.logout(’/’);
}
render() {
if ( this .state.authenticated === null ) return null ;
const button = this .state.authenticated ?
<button onClick={ this .logout}>Logout</button> :
<button onClick={ this .login}>Login</button>;
return <div>
<h1>This is the test view.</h1>
<div>
{{button}}
</div>
</div>;
}
}
// export default connect(mapStateToProps, mapDispatchToProps)(withAuth(TestView));
export default withAuth(TestView);
Because it is an older app, I am integrating with it is using some older versions: React 15, React-Router 2.8, and React-Router-Redux 4. Would that make a difference?