From cf693ca341efa95bb99c8b90b5a0ebd5fee59bd4 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Tue, 14 May 2019 23:18:24 +0100 Subject: [PATCH] WIP - more work on sign in feature --- interface/src/AppRouting.js | 4 +- interface/src/components/MenuAppBar.js | 4 +- interface/src/constants/Endpoints.js | 1 + .../{LoginPage.js => SignInPage.js} | 40 ++++++++++++++++--- 4 files changed, 39 insertions(+), 10 deletions(-) rename interface/src/containers/{LoginPage.js => SignInPage.js} (67%) diff --git a/interface/src/AppRouting.js b/interface/src/AppRouting.js index 5270cbc..4508a8f 100644 --- a/interface/src/AppRouting.js +++ b/interface/src/AppRouting.js @@ -12,7 +12,7 @@ import WiFiConfiguration from './containers/WiFiConfiguration'; import NTPConfiguration from './containers/NTPConfiguration'; import OTAConfiguration from './containers/OTAConfiguration'; import APConfiguration from './containers/APConfiguration'; -import LoginPage from './containers/LoginPage'; +import SignInPage from './containers/SignInPage'; class AppRouting extends Component { @@ -24,7 +24,7 @@ class AppRouting extends Component { return ( - + diff --git a/interface/src/components/MenuAppBar.js b/interface/src/components/MenuAppBar.js index 697d7c3..af2b25c 100644 --- a/interface/src/components/MenuAppBar.js +++ b/interface/src/components/MenuAppBar.js @@ -98,7 +98,7 @@ class MenuAppBar extends React.Component { const drawer = (
- + {APP_NAME} @@ -146,7 +146,7 @@ class MenuAppBar extends React.Component { > - + {sectionTitle} diff --git a/interface/src/constants/Endpoints.js b/interface/src/constants/Endpoints.js index efcd06e..6295f29 100644 --- a/interface/src/constants/Endpoints.js +++ b/interface/src/constants/Endpoints.js @@ -9,3 +9,4 @@ export const LIST_NETWORKS_ENDPOINT = ENDPOINT_ROOT + "listNetworks"; export const WIFI_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "wifiSettings"; export const WIFI_STATUS_ENDPOINT = ENDPOINT_ROOT + "wifiStatus"; export const OTA_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "otaSettings"; +export const SIGN_IN_ENDPOINT = ENDPOINT_ROOT + "signIn"; \ No newline at end of file diff --git a/interface/src/containers/LoginPage.js b/interface/src/containers/SignInPage.js similarity index 67% rename from interface/src/containers/LoginPage.js rename to interface/src/containers/SignInPage.js index d26dbce..6d98ea9 100644 --- a/interface/src/containers/LoginPage.js +++ b/interface/src/containers/SignInPage.js @@ -7,6 +7,9 @@ import Typography from '@material-ui/core/Typography'; import Fab from '@material-ui/core/Fab'; import { APP_NAME } from '../constants/App'; import ForwardIcon from '@material-ui/icons/Forward'; +import { withNotifier } from '../components/SnackbarNotification'; +import { SIGN_IN_ENDPOINT } from '../constants/Endpoints'; +import { withAuthenticationContext } from '../authentication/Context'; const styles = theme => ({ loginPage: { @@ -20,7 +23,7 @@ const styles = theme => ({ paddingTop: "200px", backgroundImage: 'url("/app/icon.png")', backgroundRepeat: "no-repeat", - backgroundPosition: "50% "+ theme.spacing.unit * 2 +"px", + backgroundPosition: "50% " + theme.spacing.unit * 2 + "px", backgroundSize: "auto 150px", textAlign: "center" }, @@ -49,7 +52,8 @@ class LoginPage extends Component { super(props); this.state = { username: '', - password: '' + password: '', + fetched: false }; } @@ -57,8 +61,32 @@ class LoginPage extends Component { this.setState({ [name]: event.target.value }); }; - onSubmit = event => { - // TODO + onSubmit = () => { + const { username, password } = this.state; + const { authenticationContext } = this.props; + this.setState({ fetched: false }); + fetch(SIGN_IN_ENDPOINT, { + method: 'POST', + body: JSON.stringify({ username, password }), + headers: new Headers({ + 'Content-Type': 'application/json' + }) + }) + .then(response => { + if (response.status === 200) { + return response.json(); + } else if (response.status === 401) { + throw Error("Login details invalid!"); + } else { + throw Error("Invalid status code: " + response.status); + } + }).then(json =>{ + authenticationContext.signIn(json.access_token); + }) + .catch(error => { + this.props.raiseNotification(error.message); + this.setState({ fetched: true }); + }); }; render() { @@ -91,7 +119,7 @@ class LoginPage extends Component { /> - Login + Sign In @@ -101,4 +129,4 @@ class LoginPage extends Component { } -export default withStyles(styles)(LoginPage); +export default withAuthenticationContext(withNotifier(withStyles(styles)(LoginPage)));