#13-login-page #19
@@ -2,12 +2,15 @@ import "./App.css";
|
|||||||
import { Route } from "wouter";
|
import { Route } from "wouter";
|
||||||
import HomePage from "./pages/HomePage";
|
import HomePage from "./pages/HomePage";
|
||||||
import LoginPage from "./pages/LoginPage";
|
import LoginPage from "./pages/LoginPage";
|
||||||
|
import { UserContextProvider } from "./contexts/UserContext";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<Route path="/" component={HomePage} />
|
<UserContextProvider>
|
||||||
<Route path="/login" component={LoginPage} />
|
<Route path="/" component={HomePage} />
|
||||||
|
<Route path="/login" component={LoginPage} />
|
||||||
|
</UserContextProvider>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
import { useContext } from "react";
|
||||||
import { Nav, Container, Navbar } from "react-bootstrap";
|
import { Nav, Container, Navbar } from "react-bootstrap";
|
||||||
|
import UserContext from "../contexts/UserContext";
|
||||||
|
|
||||||
const MyNavbar = () => {
|
const MyNavbar = () => {
|
||||||
|
const { currentUser } = useContext(UserContext);
|
||||||
return (
|
return (
|
||||||
<Navbar variant="dark" bg="dark" expand="lg">
|
<Navbar variant="dark" bg="dark" expand="lg">
|
||||||
<Container>
|
<Container>
|
||||||
@@ -9,7 +12,9 @@ const MyNavbar = () => {
|
|||||||
<Navbar.Collapse id="navbar-nav">
|
<Navbar.Collapse id="navbar-nav">
|
||||||
<Nav className="ms-auto">
|
<Nav className="ms-auto">
|
||||||
<Nav.Link href="/">Home</Nav.Link>
|
<Nav.Link href="/">Home</Nav.Link>
|
||||||
<Nav.Link href="/login">Login</Nav.Link>
|
{(currentUser?.id && (
|
||||||
|
<Nav.Link href="/logout">Logout</Nav.Link>
|
||||||
|
)) || <Nav.Link href="/login">Login</Nav.Link>}
|
||||||
</Nav>
|
</Nav>
|
||||||
</Navbar.Collapse>
|
</Navbar.Collapse>
|
||||||
</Container>
|
</Container>
|
||||||
|
|||||||
30
frontend/src/contexts/UserContext.jsx
Normal file
30
frontend/src/contexts/UserContext.jsx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { createContext, useMemo, useState } from "react";
|
||||||
|
|
||||||
|
const UserContext = createContext({});
|
||||||
|
|
||||||
|
const UserContextProvider = ({ children }) => {
|
||||||
|
|
||||||
|
const [currentUser, setCurrentUser] = useState(undefined);
|
||||||
|
|
||||||
|
// useMemo does not wait for after render, like useEffect.
|
||||||
|
useMemo(()=>{
|
||||||
|
setCurrentUser(JSON.parse(localStorage.getItem("currentUser")));
|
||||||
|
},[]);
|
||||||
|
|
||||||
|
const contextValues = {
|
||||||
|
currentUser,
|
||||||
|
setCurrentUser: (data) => {
|
||||||
|
localStorage.setItem("currentUser", JSON.stringify(data))
|
||||||
|
setCurrentUser(data);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<UserContext.Provider value={contextValues}>
|
||||||
|
{children}
|
||||||
|
</UserContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UserContext;
|
||||||
|
export { UserContextProvider };
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
import { useEffect } from "react";
|
import { useContext, useEffect } from "react";
|
||||||
import { Container } from "react-bootstrap";
|
import { Container } from "react-bootstrap";
|
||||||
import MyNavbar from "../components/MyNavbar";
|
import MyNavbar from "../components/MyNavbar";
|
||||||
|
import UserContext from "../contexts/UserContext";
|
||||||
|
|
||||||
const HomePage = () => {
|
const HomePage = () => {
|
||||||
{/*{current_user} = use context*/}
|
const { currentUser } = useContext(UserContext);
|
||||||
const current_user = null;
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!currentUser?.id) {
|
||||||
|
window.location.replace("/login");
|
||||||
|
}
|
||||||
|
}, [currentUser]);
|
||||||
|
|
||||||
useEffect(()=>{
|
|
||||||
if (!current_user) {
|
|
||||||
window.location.replace("/login");
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<MyNavbar />
|
<MyNavbar />
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
import { Button, Col, Container, Form, Row, Alert } from "react-bootstrap";
|
import { Button, Col, Container, Form, Row, Alert } from "react-bootstrap";
|
||||||
import MyNavbar from "../components/MyNavbar";
|
import MyNavbar from "../components/MyNavbar";
|
||||||
|
import UserContext from "../contexts/UserContext";
|
||||||
import { makeRequest } from "../utils.ts";
|
import { makeRequest } from "../utils.ts";
|
||||||
|
|
||||||
const LoginPage = () => {
|
const LoginPage = () => {
|
||||||
@@ -8,6 +9,19 @@ const LoginPage = () => {
|
|||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
|
const { currentUser, setCurrentUser } = useContext(UserContext);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log(currentUser);
|
||||||
|
if (currentUser?.id) {
|
||||||
|
gotoHome();
|
||||||
|
}
|
||||||
|
}, [currentUser]);
|
||||||
|
|
||||||
|
const gotoHome = () => {
|
||||||
|
window.location.href = "/";
|
||||||
|
};
|
||||||
|
|
||||||
const sendLoginRequest = async (e) => {
|
const sendLoginRequest = async (e) => {
|
||||||
e?.preventDefault();
|
e?.preventDefault();
|
||||||
await makeRequest({
|
await makeRequest({
|
||||||
@@ -21,8 +35,7 @@ const LoginPage = () => {
|
|||||||
setError(data);
|
setError(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(data);
|
setCurrentUser(data);
|
||||||
window.location.href = "/";
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user