Add login code

This commit is contained in:
Ceda EI 2019-03-28 16:26:24 +05:30
parent a9a8353866
commit eae9242c19
2 changed files with 50 additions and 12 deletions

View File

@ -1,15 +1,49 @@
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import React, { useState } from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
import "./index.css";
import { config } from "./config";
import axios from "axios";
class App extends Component {
render() {
return (
<div className="App">
<h1>Hello, React!</h1>
</div>
)
};
function auth(token) {
return axios.get(`${config.apiUrl}/auth?token=${encodeURIComponent(token)}`)
.then((res) => res.data.success);
}
ReactDOM.render(<App />, document.getElementById('root'));
function Login(props) {
const [input, setInput] = useState("");
return (
<div className="login">
<input type="text" placeholder="Token ID" value={input}
onChange={evt => setInput(evt.target.value)} />
<button onClick={() => auth(input).then(x => props.setLoggedIn(x))}>Submit</button>
</div>
);
}
Login.propTypes = {
setLoggedIn: PropTypes.func,
};
function MainApp() {
return (
<div className="main">
</div>
);
}
function App(){
const [loggedIn, setLoggedIn] = useState(false);
if (loggedIn){
return (
<MainApp />
);
}
else {
return (
<Login setLoggedIn={setLoggedIn} />
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));

4
src/sample.config.js Normal file
View File

@ -0,0 +1,4 @@
const config = {
apiUrl: "http://localhost:5000"
};
export { config };