-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
#config | ||
Local-config.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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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; |
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; | ||
} |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be nice if the password is hidden behind |
||
</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; |
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; | ||
} |
There was a problem hiding this comment.
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
andidToken
?