Skip to content

Commit

Permalink
Merge pull request #32 from PiotrNakonowski/AdamPlewa
Browse files Browse the repository at this point in the history
15.  Dopracowanie logowania do aplikacji webowej
  • Loading branch information
PiotrNakonowski authored Jun 1, 2024
2 parents 9f593a7 + dfb5058 commit 6efa8b2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
25 changes: 20 additions & 5 deletions WebApp/src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import Login from "./pages/login/Login";
import Home from "./pages/home/Home";
import { BrowserRouter, Routes, Route} from "react-router-dom";
import { BrowserRouter, Routes, Route, Navigate} from "react-router-dom";
import { useContext } from "react";
import { AuthContext } from "./context/AuthContext";

function App() {

const {currentUser} = useContext(AuthContext)

const RequireAuth = ({ children }) => {
return currentUser ? children : <Navigate to="/login" />;
};

return (
<BrowserRouter>
<Routes>
<Route path="/">
<Route index element={<Home />} />
<Route path="login" element={<Login />} />
</Route>
<Route path="/">
<Route path="login" element={<Login />} />
<Route
index
element={
<RequireAuth>
<Home />
</RequireAuth>
}
/>
</Route>
</Routes>
</BrowserRouter>
);
Expand Down
14 changes: 12 additions & 2 deletions WebApp/src/components/navbar/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { useContext } from "react";
import "./navbar.scss";
import LogoutIcon from '@mui/icons-material/Logout';
import { AuthContext } from "../../context/AuthContext";
import { useNavigate } from "react-router-dom";

const Navbar = () => {
const { dispatch } = useContext(AuthContext);
const navigate = useNavigate();

const handleLogout = () => {
dispatch({ type: "LOGOUT" });
navigate("/login");
};

return (
<div className="navbar">
<div className="wrapper">
<img className="logo-image" src="https://i.imgur.com/3U6VsFH.png" alt="logo"/>
<div className="items">
<div className="item">
<LogoutIcon className="icon" />
<div className="item" onClick={handleLogout}>
<LogoutIcon className="icon"/>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion WebApp/src/components/navbar/navbar.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.navbar {
width: 100vw;
min-height: 100px;
//border-bottom: 0.5px solid rgb(231, 228, 228);
display: flex;
Expand Down Expand Up @@ -34,6 +33,7 @@
.icon {
font-size: 50px;
color: white;
cursor: pointer;
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions WebApp/src/context/AuthContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createContext, useEffect, useReducer } from "react";
import AuthReducer from "./AuthReducer";

const INITIAL_STATE = {
currentUser: JSON.parse(localStorage.getItem("user")) || null,
};

export const AuthContext = createContext(INITIAL_STATE);

export const AuthContextProvider = ({ children }) => {
const [state, dispatch] = useReducer(AuthReducer, INITIAL_STATE);

useEffect(() => {
localStorage.setItem("user", JSON.stringify(state.currentUser));
}, [state.currentUser]);

return (
<AuthContext.Provider value={{ currentUser: state.currentUser, dispatch }}>
{children}
</AuthContext.Provider>
);
};
18 changes: 18 additions & 0 deletions WebApp/src/context/AuthReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const AuthReducer = (state, action) => {
switch (action.type) {
case "LOGIN": {
return {
currentUser: action.payload,
};
}
case "LOGOUT": {
return {
currentUser: null,
};
}
default:
return state;
}
};

export default AuthReducer;
5 changes: 4 additions & 1 deletion WebApp/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { AuthContextProvider } from "./context/AuthContext";

ReactDOM.render(
<App />,
<AuthContextProvider>
<App />
</AuthContextProvider>,
document.getElementById("root")
);
1 change: 0 additions & 1 deletion WebApp/src/pages/home/home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
//overflow: hidden;

.main
Expand Down
6 changes: 5 additions & 1 deletion WebApp/src/pages/login/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState } from "react";
import { useContext, useState } from "react";
import "./login.scss";
import { signInWithEmailAndPassword } from "firebase/auth";
import { auth } from "../../firebase";
import { useNavigate } from "react-router-dom";
import { AuthContext } from "../../context/AuthContext"

const Login = () => {
const [error, setError] = useState(false);
Expand All @@ -11,13 +12,16 @@ const Login = () => {

const navigate = useNavigate();

const {dispatch} = useContext(AuthContext)

const handleLogin = (e) => {
e.preventDefault();

signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
dispatch({type:"LOGIN", payload:user})
navigate("/");
})
.catch((error) => {
Expand Down

0 comments on commit 6efa8b2

Please sign in to comment.