From 49814e123b1ebf94ac97ce13cb18de6753c27d3d Mon Sep 17 00:00:00 2001 From: Marx-wrld Date: Tue, 6 Dec 2022 16:04:30 +0300 Subject: [PATCH] Firebase, usehistory, Redirect --- checks.json | 1 + src/components/App.js | 35 +++++----- src/components/Dashboard.js | 51 +++++++------- src/components/ForgotPassword.js | 96 +++++++++++++------------- src/components/Login.js | 98 +++++++++++++-------------- src/components/Signup.js | 111 +++++++++++++++---------------- src/components/UpdateProfile.js | 21 +++--- src/firebase.js | 24 ++++--- src/index.js | 14 ++-- 9 files changed, 226 insertions(+), 225 deletions(-) create mode 100644 checks.json diff --git a/checks.json b/checks.json new file mode 100644 index 0000000..8b70b53 --- /dev/null +++ b/checks.json @@ -0,0 +1 @@ +{"enabled":true,"categories":{}} \ No newline at end of file diff --git a/src/components/App.js b/src/components/App.js index 1b6ec8d..3ae4f49 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,8 +1,8 @@ import React from "react" -import Signup from "./Signup" -import { Container } from "react-bootstrap" -import { AuthProvider } from "../contexts/AuthContext" -import { BrowserRouter as Router, Switch, Route } from "react-router-dom" +import { Container } from "react-bootstrap"; +import { AuthProvider } from "../contexts/AuthContext"; +import { BrowserRouter as Router, Routes, Route } from "react-router-dom" +import Signup from "./Signup"; import Dashboard from "./Dashboard" import Login from "./Login" import PrivateRoute from "./PrivateRoute" @@ -11,25 +11,24 @@ import UpdateProfile from "./UpdateProfile" function App() { return ( - +
- - - - - + + + + {/* this private route locks us to the official home page if not yet logged in, so we can't go to the dashboard*/ } + } component={Dashboard} /> + - - - + + +
- ) + ) } -export default App +export default App; diff --git a/src/components/Dashboard.js b/src/components/Dashboard.js index 96e120f..6cbc511 100644 --- a/src/components/Dashboard.js +++ b/src/components/Dashboard.js @@ -1,41 +1,38 @@ -import React, { useState } from "react" -import { Card, Button, Alert } from "react-bootstrap" +import React, { useState } from 'react'; +import { Card, Button, Alert } from 'react-bootstrap' import { useAuth } from "../contexts/AuthContext" -import { Link, useHistory } from "react-router-dom" +import { Link, useNavigate } from "react-router-dom" export default function Dashboard() { - const [error, setError] = useState("") const { currentUser, logout } = useAuth() - const history = useHistory() - + const [error, setError] = useState("") + const history = useNavigate() async function handleLogout() { - setError("") + setError('') - try { + try{ await logout() - history.push("/login") - } catch { - setError("Failed to log out") + history.pushState('/login') } + catch{ + setError('Failed to logout') + } } - - return ( + return ( <> - + -

Profile

- {error && {error}} - Email: {currentUser.email} - - Update Profile - +

+ Profile +

+ {error && {error}} + Email: {currentUser.email} + Update Profile
-
-
- -
+
+
+ +
- ) + ); } diff --git a/src/components/ForgotPassword.js b/src/components/ForgotPassword.js index 4a55997..8e08a3a 100644 --- a/src/components/ForgotPassword.js +++ b/src/components/ForgotPassword.js @@ -4,52 +4,52 @@ import { useAuth } from "../contexts/AuthContext" import { Link } from "react-router-dom" export default function ForgotPassword() { - const emailRef = useRef() - const { resetPassword } = useAuth() - const [error, setError] = useState("") - const [message, setMessage] = useState("") - const [loading, setLoading] = useState(false) - - async function handleSubmit(e) { - e.preventDefault() - - try { - setMessage("") - setError("") - setLoading(true) - await resetPassword(emailRef.current.value) - setMessage("Check your inbox for further instructions") - } catch { - setError("Failed to reset password") + const emailRef = useRef() + const { resetPassword } = useAuth() + const [error, setError] = useState("") + const [message, setMessage] = useState("") + const [loading, setLoading] = useState(false) + + async function handleSubmit(e) { + e.preventDefault() + + try { + setMessage("") + setError("") + setLoading(true) + await resetPassword(emailRef.current.value) + setMessage("Check your inbox for further instructions") + } catch { + setError("Failed to reset password") + } + + setLoading(false) } - - setLoading(false) - } - - return ( - <> - - -

Password Reset

- {error && {error}} - {message && {message}} -
- - Email - - - -
-
- Login -
-
-
-
- Need an account? Sign Up -
- - ) -} + + return ( + <> + + +

Password Reset

+ {error && {error}} + {message && {message}} +
+ + Email + + + +
+
+ Login +
+
+
+
+ Need an account? Register +
+ + ) + } \ No newline at end of file diff --git a/src/components/Login.js b/src/components/Login.js index 53ec8b7..8b7fd02 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -1,58 +1,56 @@ -import React, { useRef, useState } from "react" -import { Form, Button, Card, Alert } from "react-bootstrap" -import { useAuth } from "../contexts/AuthContext" -import { Link, useHistory } from "react-router-dom" +import React, { useRef, useState } from 'react'; +import { Form, Button, Card, Alert } from 'react-bootstrap'; +import { useAuth } from '../contexts/AuthContext' +import { Link, useNavigate } from "react-router-dom" export default function Login() { - const emailRef = useRef() - const passwordRef = useRef() - const { login } = useAuth() - const [error, setError] = useState("") - const [loading, setLoading] = useState(false) - const history = useHistory() + const emailRef = useRef() + const passwordRef = useRef() + const { login } = useAuth() + const [error, setError] = useState("") + const [loading, setLoading] = useState(false) + const history = useNavigate() - async function handleSubmit(e) { - e.preventDefault() - - try { - setError("") - setLoading(true) - await login(emailRef.current.value, passwordRef.current.value) - history.push("/") - } catch { - setError("Failed to log in") + async function handleSubmit(e){ + e.preventDefault() + try { + setError('') + setLoading(true) + await login(emailRef.current.value, passwordRef.current.value) + history.push('/') + } catch{ + setError('Failed to Login') + } + setLoading(false) } - - setLoading(false) - } - return ( <> - - -

Log In

- {error && {error}} -
- - Email - - - - Password - - - -
-
- Forgot Password? -
-
-
-
- Need an account? Sign Up -
+ + +

Login

+ {error && {error}} +
+ + Email + + + + Password + + +
+ +
+
+ Forgot Password? +
+
+
+
+ Not yet Registered? Register +
- ) + ); } diff --git a/src/components/Signup.js b/src/components/Signup.js index 546b77b..f4ceebb 100644 --- a/src/components/Signup.js +++ b/src/components/Signup.js @@ -1,64 +1,63 @@ -import React, { useRef, useState } from "react" -import { Form, Button, Card, Alert } from "react-bootstrap" -import { useAuth } from "../contexts/AuthContext" -import { Link, useHistory } from "react-router-dom" +import React, { useRef, useState } from 'react'; +import { Form, Button, Card, Alert } from 'react-bootstrap'; +import { useAuth } from '../contexts/AuthContext' +import { Link, useNavigate } from "react-router-dom" export default function Signup() { - const emailRef = useRef() - const passwordRef = useRef() - const passwordConfirmRef = useRef() - const { signup } = useAuth() - const [error, setError] = useState("") - const [loading, setLoading] = useState(false) - const history = useHistory() + const emailRef = useRef() + const passwordRef = useRef() + const passwordConfirmRef = useRef() + const { signup } = useAuth() + const [error, setError] = useState("") + const [loading, setLoading] = useState(false) + const history = useNavigate() - async function handleSubmit(e) { - e.preventDefault() + async function handleSubmit(e){ + e.preventDefault() - if (passwordRef.current.value !== passwordConfirmRef.current.value) { - return setError("Passwords do not match") + if(passwordRef.current.value === passwordConfirmRef.current.value){ + return setError('Passwords do not match') + }//we dont want to continue to do the signup, we just want to exit out of the function immediately because there was an error. + try { + setError('') + setLoading(true) + await signup(emailRef.current.value, passwordRef.current.value) + history.push('/') + } catch{ + setError('Failed to create an account') + } + setLoading(false) } - - try { - setError("") - setLoading(true) - await signup(emailRef.current.value, passwordRef.current.value) - history.push("/") - } catch { - setError("Failed to create an account") - } - - setLoading(false) - } - return ( <> - - -

Sign Up

- {error && {error}} -
- - Email - - - - Password - - - - Password Confirmation - - - -
-
-
-
- Already have an account? Log In -
+ + +

Register

+ {error && {error}} +
+ + Email + + + + Password + + + + Confirm Password + + +
+ +
+
+
+
+ Already have an account? Login +
- ) -} + ); +} \ No newline at end of file diff --git a/src/components/UpdateProfile.js b/src/components/UpdateProfile.js index 67a8b39..cdc41c2 100644 --- a/src/components/UpdateProfile.js +++ b/src/components/UpdateProfile.js @@ -1,7 +1,7 @@ import React, { useRef, useState } from "react" import { Form, Button, Card, Alert } from "react-bootstrap" import { useAuth } from "../contexts/AuthContext" -import { Link, useHistory } from "react-router-dom" +import { Link, useNavigate } from "react-router-dom" export default function UpdateProfile() { const emailRef = useRef() @@ -10,9 +10,10 @@ export default function UpdateProfile() { const { currentUser, updatePassword, updateEmail } = useAuth() const [error, setError] = useState("") const [loading, setLoading] = useState(false) - const history = useHistory() + const history = useNavigate() function handleSubmit(e) { + //if passwords are not same I set this error e.preventDefault() if (passwordRef.current.value !== passwordConfirmRef.current.value) { return setError("Passwords do not match") @@ -24,19 +25,21 @@ export default function UpdateProfile() { if (emailRef.current.value !== currentUser.email) { promises.push(updateEmail(emailRef.current.value)) + //Check if our email is not equal to our current email + //if we've changed our email, I'll want to add that email by using (promises.push) } if (passwordRef.current.value) { promises.push(updatePassword(passwordRef.current.value)) } - Promise.all(promises) - .then(() => { - history.push("/") + Promise.all(promises) //Our array promises + .then(() => { //our .then will run everytime our promises execute + history.push("/") //redirecting to our home page }) .catch(() => { setError("Failed to update account") }) - .finally(() => { + .finally(() => { //our .finally will set our loading back to false and it runs if we either succeed or fail setLoading(false) }) } @@ -62,7 +65,7 @@ export default function UpdateProfile() { @@ -70,7 +73,7 @@ export default function UpdateProfile() {