diff --git a/src/constants/index.js b/src/constants/index.js new file mode 100644 index 0000000..25109c2 --- /dev/null +++ b/src/constants/index.js @@ -0,0 +1,3 @@ +export default { + baseUrl: "http://192.168.68.150:8469" +} \ No newline at end of file diff --git a/src/pages/login/index.js b/src/pages/login/index.js new file mode 100644 index 0000000..9ee0403 --- /dev/null +++ b/src/pages/login/index.js @@ -0,0 +1,74 @@ +import React from 'react'; +import './style.css'; + +import { baseUrl } from "../../constants"; + +class Login extends React.Component { + state = { + username: '', + password: '' + }; + + handleChange = (event) => { + this.setState({ [event.target.name]: event.target.value }); + }; + + handleSubmit = (event) => { + event.preventDefault(); + const { username, password } = this.state; + fetch(`${baseUrl}/auth/login`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ username, password }) + }) + .then(response => response.json()) + .then(data => { + if (data.token) { + // store the token in local storage or state + localStorage.setItem('token', data.token); + this.props.history.push('/dashboard'); + } else { + // handle login failure + alert('Invalid username or password'); + } + }) + .catch(error => console.error(error)); + }; + + render() { + return ( +