add redirect feature for authentication

This commit is contained in:
Rick Watson 2019-05-19 21:22:01 +01:00
parent adeb9d27ed
commit 5c6ba73e1f
3 changed files with 28 additions and 3 deletions

View File

@ -1,11 +1,12 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Redirect, Route, Switch } from 'react-router'; import { Redirect, Switch } from 'react-router';
// authentication // authentication
import * as Authentication from './authentication/Authentication'; import * as Authentication from './authentication/Authentication';
import AuthenticationWrapper from './authentication/AuthenticationWrapper'; import AuthenticationWrapper from './authentication/AuthenticationWrapper';
import AuthenticatedRoute from './authentication/AuthenticatedRoute'; import AuthenticatedRoute from './authentication/AuthenticatedRoute';
import UnauthenticatedRoute from './authentication/UnauthenticatedRoute';
// containers // containers
import WiFiConfiguration from './containers/WiFiConfiguration'; import WiFiConfiguration from './containers/WiFiConfiguration';
@ -24,7 +25,7 @@ class AppRouting extends Component {
return ( return (
<AuthenticationWrapper> <AuthenticationWrapper>
<Switch> <Switch>
<Route exact path="/" component={SignInPage} /> <UnauthenticatedRoute exact path="/" component={SignInPage} />
<AuthenticatedRoute exact path="/wifi-configuration" component={WiFiConfiguration} /> <AuthenticatedRoute exact path="/wifi-configuration" component={WiFiConfiguration} />
<AuthenticatedRoute exact path="/ap-configuration" component={APConfiguration} /> <AuthenticatedRoute exact path="/ap-configuration" component={APConfiguration} />
<AuthenticatedRoute exact path="/ntp-configuration" component={NTPConfiguration} /> <AuthenticatedRoute exact path="/ntp-configuration" component={NTPConfiguration} />

View File

@ -21,7 +21,7 @@ export function fetchLoginRedirect() {
const loginSearch = localStorage.getItem(LOGIN_SEARCH); const loginSearch = localStorage.getItem(LOGIN_SEARCH);
clearLoginRedirect(); clearLoginRedirect();
return { return {
pathname: loginPathname || "/", pathname: loginPathname || "/wifi-configuration",
search: (loginPathname && loginSearch) || undefined search: (loginPathname && loginSearch) || undefined
}; };
} }

View File

@ -0,0 +1,24 @@
import * as React from 'react';
import {
Redirect, Route
} from "react-router-dom";
import { withAuthenticationContext } from './Context.js';
import * as Authentication from './Authentication';
class UnauthenticatedRoute extends React.Component {
render() {
const { component:Component, ...rest } = this.props;
const renderComponent = (props) => {
if (this.props.authenticationContext.jwt) {
return (<Redirect to={Authentication.fetchLoginRedirect()} />);
}
return (<Component {...props} />);
}
return (
<Route {...rest} render={renderComponent} />
);
}
}
export default withAuthenticationContext(UnauthenticatedRoute);