From 02fa375af92f7a42d95062c10d5c886c1b41e717 Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Mon, 15 Oct 2018 11:24:07 -0400 Subject: [PATCH] Use BrowserRouter and fix login page redirection The login page was refreshing the whole page and not redirecting to the correct page (if you tried to load a protected page without auth). Signed-off-by: Mcat12 --- src/index.js | 6 +++--- src/views/Login.js | 43 ++++++++++++++++++++++++++----------------- src/views/Logout.js | 10 +++++++++- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/index.js b/src/index.js index 396f4a0..01ff80a 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import { HashRouter, Route, Switch } from 'react-router-dom' +import { BrowserRouter, Route, Switch } from 'react-router-dom' import "ionicons/dist/css/ionicons.min.css"; import "font-awesome/css/font-awesome.min.css"; import "simple-line-icons/css/simple-line-icons.css"; @@ -28,11 +28,11 @@ setupI18n(); ReactDOM.render( ( - + - + ), document.getElementById('root') ); diff --git a/src/views/Login.js b/src/views/Login.js index f9263ad..c00666d 100644 --- a/src/views/Login.js +++ b/src/views/Login.js @@ -8,18 +8,19 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -import React, { Component, Fragment } from 'react'; -import { Redirect } from 'react-router-dom'; -import sha from 'sha.js'; +import React, { Component, Fragment } from "react"; +import { Redirect } from "react-router-dom"; +import sha from "sha.js"; import { api } from "../utils"; -import logo from '../img/logo.svg'; +import logo from "../img/logo.svg"; import { routes } from "../routes"; import ForgotPassword from "../components/login/ForgotPassword"; -import { translate } from 'react-i18next'; +import { translate } from "react-i18next"; +import config from "../config"; class Login extends Component { state = { - password: '', + password: "", error: false, cookiesEnabled: false }; @@ -41,19 +42,22 @@ class Login extends Component { if(e.keyCode === 13) this.authenticate(); else - this.setState({ password: e.target.value }) + this.setState({ password: e.target.value }); }; /** * Try to authenticate the user */ - authenticate = () => { + authenticate = e => { + // Prevent the page from reloading when the user gets redirected + e.preventDefault(); + // Hash the password twice before sending to the API let hashedPassword = sha("sha256").update(this.state.password).digest("hex"); hashedPassword = sha("sha256").update(hashedPassword).digest("hex"); // Clear the state - this.setState({ password: '', error: false }); + this.setState({ password: "", error: false }); // Send the password to the API to authenticate the user api.authenticate(hashedPassword) @@ -67,9 +71,14 @@ class Login extends Component { api.loggedIn = true; + if(config.fakeAPI) { + // When using the fake API, set the cookie ourselves + document.cookie = "user_id=;"; + } + // Redirect to the page the user was originally going to, or if that doesn't exist, go to home - const redirect = this.props.location.state.from || '/'; - this.props.history.push(redirect); + const locationState = this.props.location.state || "/"; + this.props.history.push(locationState.from.pathname); }) // If there was an error, tell the user they used the wrong password .catch(() => this.setState({ error: true })); @@ -83,16 +92,16 @@ class Login extends Component { const { t } = this.props; return ( -
+
-
+
Logo

-
- +
+ Pi-hole
@@ -115,7 +124,7 @@ class Login extends Component { { // If cookies are not enabled (or detected), show a warning !this.state.cookiesEnabled ? -
+
{t("Verify that cookies are allowed for {{host}}", { host: window.location.host })}
: null @@ -135,7 +144,7 @@ class Login extends Component {
-
+
diff --git a/src/views/Logout.js b/src/views/Logout.js index 90244b5..eddda78 100644 --- a/src/views/Logout.js +++ b/src/views/Logout.js @@ -11,11 +11,19 @@ import React, { Component } from 'react'; import { Redirect } from 'react-router-dom'; import { api } from "../utils"; +import config from "../config"; export default class Logout extends Component { componentWillMount() { api.loggedIn = false; - api.logout(); + + if(config.fakeAPI) { + // When using the fake API, don't try deleting the resource + // (it results in an error). Instead, delete the cookie. + document.cookie = 'user_id=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; + } else { + api.logout(); + } } render() {