From 5c6ba73e1fb8b55ac9cf7642901e41709434314c Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sun, 19 May 2019 21:22:01 +0100 Subject: [PATCH] add redirect feature for authentication --- interface/src/AppRouting.js | 5 ++-- .../src/authentication/Authentication.js | 2 +- .../authentication/UnauthenticatedRoute.js | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 interface/src/authentication/UnauthenticatedRoute.js diff --git a/interface/src/AppRouting.js b/interface/src/AppRouting.js index 4508a8f..f668ba9 100644 --- a/interface/src/AppRouting.js +++ b/interface/src/AppRouting.js @@ -1,11 +1,12 @@ import React, { Component } from 'react'; -import { Redirect, Route, Switch } from 'react-router'; +import { Redirect, Switch } from 'react-router'; // authentication import * as Authentication from './authentication/Authentication'; import AuthenticationWrapper from './authentication/AuthenticationWrapper'; import AuthenticatedRoute from './authentication/AuthenticatedRoute'; +import UnauthenticatedRoute from './authentication/UnauthenticatedRoute'; // containers import WiFiConfiguration from './containers/WiFiConfiguration'; @@ -24,7 +25,7 @@ class AppRouting extends Component { return ( - + diff --git a/interface/src/authentication/Authentication.js b/interface/src/authentication/Authentication.js index 8606d4a..5dabb04 100644 --- a/interface/src/authentication/Authentication.js +++ b/interface/src/authentication/Authentication.js @@ -21,7 +21,7 @@ export function fetchLoginRedirect() { const loginSearch = localStorage.getItem(LOGIN_SEARCH); clearLoginRedirect(); return { - pathname: loginPathname || "/", + pathname: loginPathname || "/wifi-configuration", search: (loginPathname && loginSearch) || undefined }; } diff --git a/interface/src/authentication/UnauthenticatedRoute.js b/interface/src/authentication/UnauthenticatedRoute.js new file mode 100644 index 0000000..28fbbd6 --- /dev/null +++ b/interface/src/authentication/UnauthenticatedRoute.js @@ -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 (); + } + return (); + } + return ( + + ); + } +} + +export default withAuthenticationContext(UnauthenticatedRoute);