From c7e7833834ad4182dad9636567c9b1ed78086a0d Mon Sep 17 00:00:00 2001 From: Hannah Gooding <47333850+hannahgooding@users.noreply.github.com> Date: Tue, 26 May 2020 15:19:39 +0100 Subject: [PATCH] Fix Login and Signup with tokens in local storage Relates #3 #6 Co-authored-by: CampbellDocherty --- wip-app/src/components/LogInForm/LogInForm.js | 20 +++++----- .../src/components/SignUpForm/SignUpForm.js | 2 +- wip-app/src/utils/fetch.js | 38 ++++--------------- 3 files changed, 18 insertions(+), 42 deletions(-) diff --git a/wip-app/src/components/LogInForm/LogInForm.js b/wip-app/src/components/LogInForm/LogInForm.js index a988d5b..04af16b 100644 --- a/wip-app/src/components/LogInForm/LogInForm.js +++ b/wip-app/src/components/LogInForm/LogInForm.js @@ -1,5 +1,5 @@ import React from "react"; -import { Link } from "react-router-dom"; +// import { Link } from "react-router-dom"; import { Container, Button, TextField } from "@material-ui/core"; import { makeStyles } from "@material-ui/core/styles"; import { useHistory } from "react-router-dom"; @@ -34,11 +34,11 @@ const LogInForm = (props) => { const logInFormData = new FormData(form); logInPost({ - emai: logInFormData.email, - password: logInFormData.password, + username: logInFormData.get("username"), + password: logInFormData.get("password"), }) .then(() => history.push("/feed")) - .catch(console.error("LoginForm.js line 41")); + .catch(console.error("Could not log in!")); }; return ( @@ -48,7 +48,7 @@ const LogInForm = (props) => { className={classes.form} noValidate autoComplete="off" - onSubmit={handleSubmit} + // onSubmit={handleSubmit} > { margin="normal" required fullWidth - id="email" - label="Email Address" - name="email" - autoComplete="email" + id="username" + label="Username" + name="username" + autoComplete="username" autoFocus /> { className={classes.formElement} variant="contained" color="primary" - // onClick={handleSubmit} + onClick={handleSubmit} > Log In diff --git a/wip-app/src/components/SignUpForm/SignUpForm.js b/wip-app/src/components/SignUpForm/SignUpForm.js index 329a2d6..e84538d 100644 --- a/wip-app/src/components/SignUpForm/SignUpForm.js +++ b/wip-app/src/components/SignUpForm/SignUpForm.js @@ -1,5 +1,5 @@ import React from "react"; -import { Link } from "react-router-dom"; +// import { Link } from "react-router-dom"; import { Container, Button, TextField } from "@material-ui/core"; import { makeStyles } from "@material-ui/core/styles"; import { useHistory } from "react-router-dom"; diff --git a/wip-app/src/utils/fetch.js b/wip-app/src/utils/fetch.js index 800a668..991fc7b 100644 --- a/wip-app/src/utils/fetch.js +++ b/wip-app/src/utils/fetch.js @@ -1,12 +1,9 @@ async function postFetch({ endpoint, body, error }) { - const headers = { - "content-type": "application/JSON", - }; - const fetchObject = { method: "POST", - mode: "no-cors", - headers, + headers: { + "content-type": "application/json", + }, body: JSON.stringify(body), }; @@ -36,43 +33,22 @@ function signUpPost(signUpFormData) { }; return postFetch(options).then((res) => { console.log("postFetch-> res", res); - localStorage.setItem("auth", JSON.stringify(res)); + localStorage.setItem("auth", JSON.stringify(res.token)); }); } -// function signUpPost(signUpFormData) { -// const formObject = { -// username: signUpFormData.username, -// email: signUpFormData.email, -// password: signUpFormData.password -// } - -// return fetch(`https://wip-rest-api.herokuapp.com/signup`, { -// method: "POST", -// mode: "no-cors", -// headers: { -// "content-type": "application/json" -// }, -// body: JSON.stringify(formObject) -// }).then((res) => { -// if (res.status !== 201) { -// throw new Error(`Error, status: ${res.status}`); -// } -// return res.json(); -// }) -// } - function logInPost(logInFormData) { const options = { endpoint: "logIn", body: { - email: logInFormData.email, + username: logInFormData.username, password: logInFormData.password, }, errorMessage: "Could not log you in", }; + console.log("hello", options); return postFetch(options).then((res) => { - localStorage.setItem("auth", JSON.stringify(res)); + localStorage.setItem("auth", JSON.stringify(res.token)); }); }