Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added login pages, and register/login functionality #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

#config
Local-config.js
33 changes: 32 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,47 @@ import FeedPage from "./pages/Feed.js";
import { Routes, Route } from "react-router-dom";
import { React, useState } from "react";
import MainNavigation from "./components/layout/MainNavigation";
import LoginPage from "./pages/Login.js";

function App() {
const [refreshPost, setRefreshPost] = useState(false);
const [showError, setShowError] = useState(false);
const [errorText, setErrorText] = useState("");
const [idToken, setIdToken] = useState();
const [userIdToken, setUserIdToken] = useState();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between userIdToken and idToken?


if (!idToken) {
return (
<LoginPage
setShowError={setShowError}
setErrorText={setErrorText}
setIdToken={setIdToken}
/>
);
}

return (
<>
<MainNavigation setRefreshPost={setRefreshPost} />
<MainNavigation
setRefreshPost={setRefreshPost}
showError={showError}
setShowError={setShowError}
errorText={errorText}
setErrorText={setErrorText}
/>
<div>
<Routes>
<Route
path="/"
element={
<LoginPage
setShowError={setShowError}
setErrorText={setErrorText}
/>
}
/>
<Route
path="/feed"
element={
<FeedPage
refreshPost={refreshPost}
Expand Down
11 changes: 8 additions & 3 deletions src/components/layout/MainNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import classes from "./MainNavigation.module.css";
import Backdrop from "../UI/Backdrop";
import ErrorModal from "../UI/ErrorModal";

function MainNavigation({ setRefreshPost }) {
function MainNavigation({
setRefreshPost,
showError,
setShowError,
errorText,
setErrorText,
}) {
const [showForm, setShowForm] = useState(false);
const [showError, setShowError] = useState(false);
const [errorText, setErrorText] = useState("");

function addForm() {
setShowForm(true);
}
Expand Down
73 changes: 73 additions & 0 deletions src/components/login/CreateLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { useRef, useState } from "react";
import { useNavigate } from "react-router";
import action from "../posts/buttons/Button.module.css";
import Post from "../UI/Post";
import classes from "./CreateLogin.module.css";
import { API_KEY } from "../../Local-config";

function CreateLoginForm({ setShowError, setErrorText, setIdToken }) {
const [emailError, setEmailError] = useState("");
const navigate = useNavigate();
const emailRef = useRef();
const passRef = useRef();
let fireResp = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine but it isn't a very reacty thing to do. A quick improvement would be to move it inside the function createAccount(account) because there at least the side effects are managed. In general though with good react there should be no side-effects inside a component which means that all variables should be consts at the top level or hooks if state is required. The better option might be to attempt to embed the boolean inside the data of the .then though this might not necessarily be possible

function submitHandler(event) {
event.preventDefault();

const account = {
email: emailRef.current.value,
password: passRef.current.value,
returnSecureToken: true,
};

createAccount(account);
}

function createAccount(account) {
fetch(
`https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=${API_KEY}`,
{
method: "POST",
body: JSON.stringify(account),
headers: {
"Content-Type": "application/json",
},
}
)
.then((response) => {
if (response.status === 200) {
fireResp = true;
} else {
fireResp = false;
}
return response.json();
})
.then((data) => {
if (fireResp === true) {
setIdToken(data.idToken);
navigate("/feed");
} else {
setEmailError(data.error.message);
}
});
}
return (
<form className={classes.form} onSubmit={submitHandler}>
<Post>
<h3>Create Account</h3>
<div className={classes.content}>
<input placeholder="Email" ref={emailRef} />
<input placeholder="Password" ref={passRef} />
</div>
{emailError !== "" && <p className={classes.error}>{emailError}</p>}
<div className={action.actions}>
<div className={classes.actions}>
<button>Create Account</button>
</div>
</div>
</Post>
</form>
);
}

export default CreateLoginForm;
44 changes: 44 additions & 0 deletions src/components/login/CreateLogin.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.form {
position: absolute;
z-index: 10;
right: 30%;
left: 30%
}

.form h3{
padding-top: 6%;
margin: 0;
padding-bottom: 0px;
text-align: center;
}

.content {
display: flex;
flex-direction: column;
padding-left: 10%;
padding-right:10%;
width: 100%;


}
.content input{
margin: 3px;
display: inline-block;
font: inherit;
border-radius: 4px;
border: 1px solid #ccc;
padding: 0.25rem;
width: 100%;
}

.actions{
text-align: center;
padding-top: 2%;
padding-bottom: 5%;
/* height: 50%; */
}

.error{
text-align: center;
color: red;
}
1 change: 0 additions & 1 deletion src/components/posts/AddPostForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ function AddPostForm({ onCancel, onAddPost, setErrorText, setShowError }) {
};

onAddPost(postData);
} else {
}
}

Expand Down
93 changes: 93 additions & 0 deletions src/pages/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Post from "../components/UI/Post";
import classes from "./Login.module.css";
import action from "../components/posts/buttons/Button.module.css";
import { useNavigate } from "react-router-dom";
import { useRef, useState } from "react";
import CreateLoginForm from "../components/login/CreateLogin";
import Backdrop from "../components/UI/Backdrop";
import { API_KEY } from "../Local-config";

function LoginPage({ setShowError, setErrorText, setIdToken }) {
const [showCreateLogin, setShowCreateLogin] = useState(false);
const [emailError, setEmailError] = useState("");
const navigate = useNavigate();
const emailRef = useRef();
const passRef = useRef();
let fireResp = false;

function onSubmit(event) {
event.preventDefault();

const account = {
email: emailRef.current.value,
password: passRef.current.value,
returnSecureToken: true,
};

fetch(
`https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=${API_KEY}`,
{
method: "POST",
body: JSON.stringify(account),
headers: {
"Content-Type": "application/json",
},
}
)
.then((response) => {
if (response.status === 200) {
fireResp = true;
} else {
fireResp = false;
}
return response.json();
})
.then((data) => {
if (fireResp === true) {
setIdToken(data.idToken);
navigate("/feed");
} else {
setEmailError(data.error.message);
}
});
}

function createLoginHandler() {
setShowCreateLogin(!showCreateLogin);
}
return (
<>
<form className={classes.login} onSubmit={onSubmit}>
<Post>
<div className={classes.content}>
<div className={classes.input}>
<input required placeholder="Email" ref={emailRef}></input>
<input required placeholder="Password" ref={passRef}></input>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice if the password is hidden behind *s or dots when displayed, might be manageable using hooks and states or a configure option

</div>
{emailError !== "" && <p className={classes.error}>{emailError}</p>}
<div className={action.actions}>
<button>Login</button>
</div>
</div>
</Post>
</form>
<div className={action.actions}>
<div className={classes.create}>
<button onClick={createLoginHandler}>Create Login</button>
</div>
</div>
{showCreateLogin && (
<>
<CreateLoginForm
setShowError={setShowError}
setErrorText={setErrorText}
setIdToken={setIdToken}
/>
<Backdrop onClick={createLoginHandler} />
</>
)}
</>
);
}

export default LoginPage;
29 changes: 29 additions & 0 deletions src/pages/Login.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.login{
display: flex;
margin: 100px 30% 1% 30%;
justify-content: center;
}

.content {
text-align: center;
padding: 10%;
}

.input input{
margin: 3px;
display: inline-block;
font: inherit;
border-radius: 4px;
border: 1px solid #ccc;
padding: 0.25rem;
width: 100%;
}

.create {
text-align: center;
}

.error{
text-align: center;
color: red;
}