From c6f8e9bba585cbcc924c874d1abe53da45f9503e Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Wed, 2 May 2018 17:54:23 -0400 Subject: [PATCH] Add some documentation to Login Signed-off-by: Mcat12 --- src/utils.js | 2 +- src/views/Login.js | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/utils.js b/src/utils.js index d4ac9ba..c122e4f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -173,7 +173,7 @@ export const api = { if(config.fakeAPI) apiLocation = window.location.host + process.env.PUBLIC_URL + "/fakeAPI"; else if(config.developmentMode) - apiLocation = "pi.hole/admin/api"; + apiLocation = "pi.hole:8000/admin/api"; else apiLocation = window.location.hostname + (window.location.port ? ':' + window.location.port : '') + "/admin/api"; diff --git a/src/views/Login.js b/src/views/Login.js index 1f294c9..d39d083 100644 --- a/src/views/Login.js +++ b/src/views/Login.js @@ -29,20 +29,32 @@ export default class Login extends Component { this.setState({ cookiesEnabled: true }); } + /** + * Called when the user types into the password box. + * If they clicked Enter, try to authenticate them. + * Otherwise update the password in the state. + * + * @param e the event + */ handlePasswordChange = e => { if(e.keyCode === 13) - this.onAuth(); + this.authenticate(); else this.setState({ password: e.target.value }) }; - onAuth = () => { + /** + * Try to authenticate the user + */ + authenticate = () => { // 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 }); + // Send the password to the API to authenticate the user api.authenticate(hashedPassword) .then(data => { // Verify status @@ -58,10 +70,12 @@ export default class Login extends Component { const redirect = this.props.location.state.from || '/'; this.props.history.push(redirect); }) + // If there was an error, tell the user they used the wrong password .catch(() => this.setState({ error: true })); }; render() { + // If the user is already logged in, then go to the home page if(api.loggedIn) return ; @@ -82,6 +96,8 @@ export default class Login extends Component {

Sign in to start your session { + // If the user tried to go to a protected page and is not logged in, + // tell them they will be redirected once login is successful this.props.location.state && this.props.location.state.from.pathname in routes ? ( @@ -91,6 +107,7 @@ export default class Login extends Component { ) : null } { + // If cookies are not enabled (or detected), show a warning !this.state.cookiesEnabled ?

Verify that cookies are allowed for {window.location.host} @@ -119,7 +136,8 @@ export default class Login extends Component {
-