Add login code
This commit is contained in:
parent
a9a8353866
commit
eae9242c19
58
src/index.js
58
src/index.js
|
@ -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"));
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
const config = {
|
||||
apiUrl: "http://localhost:5000"
|
||||
};
|
||||
export { config };
|
Loading…
Reference in New Issue