{moment(article.createdOn).format("ddd, MMM Do YYYY")}
+By {article.author || "Associated Press"}
+{article.body}
+bubbles
- -colors
-Blogger Pro
+Build a login page here
- - > - ); -}; + const [credentials, setCredentials] = useState(state); + const {push} = useHistory(); + const state = { + + credentials: { + username: "", + password: "" + }, + errorMessage : "" + } + + axios.post("http://localhost:5000/api/login", this.state.credentials) + .then{resp => { + console.log(resp); + localStorage.setItem("token", resp.data.token); + push("/view"); + }) + .catch(err=> { + console.log(err); + setCredentials({ + ...state, + errorMessage: err.response.data.error; + }) + }) + + return(+{state.errorMessage} +
export default Login; -//Task List: -//1. Build a form containing a username and password field. -//2. Add whatever state nessiary for form functioning. -//3. MAKE SURE THAT FORM INPUTS INCLUDE THE LABEL TEXT "username" and "password" RESPECTIVELY. -//4. If either the username or password is not displaied display EXACTLY the following words: Username or Password not valid. -//5. If the username / password is equal to Lambda School / i<3Lambd4, save that token to localStorage. \ No newline at end of file +//Task List +//1. Build login form DOM from scratch, making use of styled components if needed. Make sure the username input has id="username" and the password input as id="password". +//2. Add in a p tag with the id="error" under the login form for use in error display. +//3. Add in necessary local state to support login form and error display. +//4. When login form is submitted, make an http call to the login route. Save the auth token on a successful response and redirect to view page. +//5. If the response is not successful, display an error statement. **a server provided error message can be found in ```err.response.data```** +//6. MAKE SURE TO ADD id="username", id="password", id="error" AND id="submit" TO THE APPROPRIATE DOM ELEMENTS. YOUR AUTOTESTS WILL FAIL WITHOUT THEM. + +const ComponentContainer = styled.div` + height: 70%; + justify-content: center; + align-items: center; + display:flex; +` + +const ModalContainer = styled.div` + width: 500px; + background: white; + padding: 2rem; + text-align: center; +` + +const Label = styled.label` + display: block; + text-align: left; + font-size: 1.5rem; +` + +const FormGroup = styled.form` + padding:1rem; +` + +const Input = styled.input` + font-size: 1rem; + padding: 1rem 0; + width:100%; +` + +const Button = styled.button` + padding:1rem; + width: 100%; +` diff --git a/src/components/Logout.js b/src/components/Logout.js new file mode 100644 index 0000000000..c5c589f4b1 --- /dev/null +++ b/src/components/Logout.js @@ -0,0 +1,24 @@ +import React from 'react'; +import {useHistory} from 'react-router'; + +const Logout = () => { + const {push}=useHistory(); + + axios.post ('http://localhost:5000/api/logout') + .then(response =>{ + localStorage.removeItem('token') + push("/") + }) + .then(data => this.setState({ totalReactPackages: data.total })); +return( + +