diff --git a/smart-hut/package-lock.json b/smart-hut/package-lock.json index cb13d46..fe9918b 100644 --- a/smart-hut/package-lock.json +++ b/smart-hut/package-lock.json @@ -10587,6 +10587,11 @@ "whatwg-fetch": "^3.0.0" } }, + "react-axios": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/react-axios/-/react-axios-2.0.3.tgz", + "integrity": "sha512-63kY2iupdRgbvPq9G8xmM0NWUnt2Q5YmpotMoLQsxKOzKXKZg2Lo6CzF/bcZvtmv3WnfjBU6Bg8nZQO28eIAZw==" + }, "react-dev-utils": { "version": "10.2.0", "resolved": "https://registry.npmjs.org/react-dev-utils/-/react-dev-utils-10.2.0.tgz", diff --git a/smart-hut/package.json b/smart-hut/package.json index 8a47c6f..68bd548 100644 --- a/smart-hut/package.json +++ b/smart-hut/package.json @@ -8,9 +8,11 @@ "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", + "axios": "^0.19.2", "classnames": "^2.2.6", "material-ui-image": "^3.2.3", "react": "^16.12.0", + "react-axios": "^2.0.3", "react-dom": "^16.12.0", "react-router": "^5.1.2", "react-router-dom": "^5.1.2", diff --git a/smart-hut/src/App.js b/smart-hut/src/App.js index da7d978..869bed8 100644 --- a/smart-hut/src/App.js +++ b/smart-hut/src/App.js @@ -1,12 +1,109 @@ -import React from 'react'; +import React, { Component } from "react"; +import { Switch, Route, Link, Redirect } from "react-router-dom"; +import Home from "./views/Home"; +import Dashboard from "./views/DashboardTest"; +import Signup from "./views/Signup"; +import Login from "./views/Login"; -import Routes from './Routes'; +import { call } from './client_server'; -function App() { +const ProtectedRoute = ({ component: Comp, loggedIn, logOut, path, ...rest }) => { return ( - + { + return loggedIn ? ( + + ) : ( + + ); + }} + /> ); +}; + +class App extends React.Component { + constructor(props) { + super(props); + + let loggedIn = false; + + try { + let userJsonString = localStorage.getItem("token"); + if (userJsonString) { + loggedIn = true; + } + } catch (exception) { + } + + this.state = { + loggedIn: loggedIn, + }; + + this.auth = this.auth.bind(this); + this.logout = this.logout.bind(this); + } + + auth(data) { + call.login(data.params) + .then(res => { + console.log(res); + if (res.data && res.status == 200) { + this.setState( + { + user: data.params.user, + token: res.data.jwttoken, + loggedIn: true, + } + ); + localStorage.setItem("token", JSON.stringify(res.data.jwttoken)); + this.props.history.push("/dashboard"); + + } else { + this.setState({ + error: res.data.message + }); + } + }).catch(err => { + console.log(err); + }); + }; + + logout() { + this.setState({ + loggedIn : false, + }); + + localStorage.removeItem("token"); + this.props.history.push("/"); + }; + + render() { + return ( + + + + {this.state.loggedIn ? : } + + + + {this.state.loggedIn ? : } + + + + ); + } } export default App; + diff --git a/smart-hut/src/Routes.js b/smart-hut/src/Routes.js deleted file mode 100644 index 0afa599..0000000 --- a/smart-hut/src/Routes.js +++ /dev/null @@ -1,15 +0,0 @@ -import React from 'react'; -import {Route, Switch} from 'react-router-dom'; -import Home from './views/Home'; -import Login from "./views/Login"; -import Signup from "./views/Signup"; - -export default function Routes() { - return ( - - - - - - ) -} diff --git a/smart-hut/src/client_server.js b/smart-hut/src/client_server.js new file mode 100644 index 0000000..74880b4 --- /dev/null +++ b/smart-hut/src/client_server.js @@ -0,0 +1,14 @@ +import axios from 'axios'; + +let config = 'http://localhost:8080/'; + +export var call = { + login: function(data, headers) { + return axios.post(config +'auth/login', data) + .then(res => { + return res; + }).catch(err => { + console.error(err); + }); + }, +}; diff --git a/smart-hut/src/index.js b/smart-hut/src/index.js index df2ab27..1ec8388 100644 --- a/smart-hut/src/index.js +++ b/smart-hut/src/index.js @@ -3,12 +3,17 @@ import ReactDOM from 'react-dom'; import App from './App'; import * as serviceWorker from './serviceWorker'; //React Router -import { BrowserRouter as Router} from 'react-router-dom'; +import { BrowserRouter, Route, Switch } from "react-router-dom"; +const routes = ( + + + + + + ); ReactDOM.render( - - - + routes , document.getElementById('root')); serviceWorker.unregister(); diff --git a/smart-hut/src/views/DashboardTest.js b/smart-hut/src/views/DashboardTest.js new file mode 100644 index 0000000..bc54d5d --- /dev/null +++ b/smart-hut/src/views/DashboardTest.js @@ -0,0 +1,26 @@ +import React, {Component} from 'react'; +import {Button } from 'semantic-ui-react'; +import { Link } from "react-router-dom"; + + +export default class Dashboard extends Component { + constructor(props) { + super(props); + } + + handleLogOut = (e) => { + console.log(this.props); + this.props.logout(); + }; + + render() { + return ( + + ) + } +} \ No newline at end of file diff --git a/smart-hut/src/views/Login.js b/smart-hut/src/views/Login.js index 051847d..425b605 100644 --- a/smart-hut/src/views/Login.js +++ b/smart-hut/src/views/Login.js @@ -5,15 +5,24 @@ export default class Login extends Component { constructor(props) { super(props); this.state = { - logged : false, - email: "", - password : "" + user: "", + password : "", + fireRedirect: false, }; } handleLogin = (e) => { - // TODO Login should be handled here - console.log(this.state); + + e.preventDefault(); + const params = { + username: this.state.user, + password: this.state.password, + }; + + this.props.auth({ + user: this.state.user, + params : params, + }); }; onChangeHandler = (event) => { @@ -43,11 +52,11 @@ export default class Login extends Component {