mirror of
https://github.com/TurTaskProject/TurTaskWeb.git
synced 2025-12-19 22:14:07 +01:00
Add Auth Context
This commit is contained in:
parent
4be0efc0bb
commit
b47e83e5cf
@ -1,9 +1,10 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Navigate, Outlet } from "react-router-dom";
|
import { Navigate, Outlet } from "react-router-dom";
|
||||||
import IsAuthenticated from "./hooks/authentication/IsAuthenticated";
|
import { useAuth } from "./hooks/authentication/IsAuthenticated";
|
||||||
|
|
||||||
const PrivateRoute = () => {
|
const PrivateRoute = () => {
|
||||||
const auth = IsAuthenticated();
|
const { isAuthenticated, setIsAuthenticated } = useAuth();
|
||||||
|
const auth = isAuthenticated;
|
||||||
return auth ? <Outlet /> : <Navigate to="/login" />;
|
return auth ? <Outlet /> : <Navigate to="/login" />;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -5,9 +5,13 @@ import { useGoogleLogin } from "@react-oauth/google";
|
|||||||
import refreshAccessToken from "./refreshAcesstoken";
|
import refreshAccessToken from "./refreshAcesstoken";
|
||||||
import axiosapi from "../../api/AuthenticationApi";
|
import axiosapi from "../../api/AuthenticationApi";
|
||||||
|
|
||||||
|
import { useAuth } from "../../hooks/authentication/IsAuthenticated";
|
||||||
|
|
||||||
function LoginPage() {
|
function LoginPage() {
|
||||||
const Navigate = useNavigate();
|
const Navigate = useNavigate();
|
||||||
|
|
||||||
|
const { isAuthenticated, setIsAuthenticated } = useAuth();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!refreshAccessToken()) {
|
if (!refreshAccessToken()) {
|
||||||
Navigate("/");
|
Navigate("/");
|
||||||
@ -39,11 +43,13 @@ function LoginPage() {
|
|||||||
localStorage.setItem("access_token", res.data.access);
|
localStorage.setItem("access_token", res.data.access);
|
||||||
localStorage.setItem("refresh_token", res.data.refresh);
|
localStorage.setItem("refresh_token", res.data.refresh);
|
||||||
axiosapi.axiosInstance.defaults.headers["Authorization"] = "Bearer " + res.data.access;
|
axiosapi.axiosInstance.defaults.headers["Authorization"] = "Bearer " + res.data.access;
|
||||||
|
setIsAuthenticated(true);
|
||||||
Navigate("/");
|
Navigate("/");
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log("Login failed");
|
console.log("Login failed");
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
setIsAuthenticated(false);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -58,10 +64,12 @@ function LoginPage() {
|
|||||||
|
|
||||||
localStorage.setItem("access_token", access_token);
|
localStorage.setItem("access_token", access_token);
|
||||||
localStorage.setItem("refresh_token", refresh_token);
|
localStorage.setItem("refresh_token", refresh_token);
|
||||||
|
setIsAuthenticated(true);
|
||||||
Navigate("/");
|
Navigate("/");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error with the POST request:", error);
|
console.error("Error with the POST request:", error);
|
||||||
|
setIsAuthenticated(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onError: errorResponse => console.log(errorResponse),
|
onError: errorResponse => console.log(errorResponse),
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import IsAuthenticated from "../../hooks/authentication/IsAuthenticated";
|
|
||||||
import axiosapi from "../../api/AuthenticationApi";
|
import axiosapi from "../../api/AuthenticationApi";
|
||||||
|
import { useAuth } from "../../hooks/authentication/IsAuthenticated";
|
||||||
|
|
||||||
const settings = {
|
const settings = {
|
||||||
Profile: "/update_profile",
|
Profile: "/update_profile",
|
||||||
@ -11,10 +11,11 @@ const settings = {
|
|||||||
function NavBar() {
|
function NavBar() {
|
||||||
const Navigate = useNavigate();
|
const Navigate = useNavigate();
|
||||||
|
|
||||||
const isAuthenticated = IsAuthenticated();
|
const { isAuthenticated, setIsAuthenticated } = useAuth();
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
axiosapi.apiUserLogout();
|
axiosapi.apiUserLogout();
|
||||||
|
setIsAuthenticated(false);
|
||||||
Navigate("/");
|
Navigate("/");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import { useEffect, useState } from "react";
|
import React, { createContext, useContext, useState, useEffect } from "react";
|
||||||
|
|
||||||
function IsAuthenticated() {
|
const AuthContext = createContext();
|
||||||
|
|
||||||
|
export const AuthProvider = ({ children }) => {
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState(() => {
|
const [isAuthenticated, setIsAuthenticated] = useState(() => {
|
||||||
const access_token = localStorage.getItem("access_token");
|
const access_token = localStorage.getItem("access_token");
|
||||||
return !!access_token;
|
return !!access_token;
|
||||||
@ -12,6 +14,8 @@ function IsAuthenticated() {
|
|||||||
setIsAuthenticated(!!newAccessToken);
|
setIsAuthenticated(!!newAccessToken);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
handleTokenChange();
|
||||||
|
|
||||||
window.addEventListener("storage", handleTokenChange);
|
window.addEventListener("storage", handleTokenChange);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@ -19,7 +23,17 @@ function IsAuthenticated() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return isAuthenticated;
|
return (
|
||||||
}
|
<AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
|
||||||
|
{children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default IsAuthenticated;
|
export const useAuth = () => {
|
||||||
|
const context = useContext(AuthContext);
|
||||||
|
if (!context) {
|
||||||
|
throw new Error("useAuth must be used within an AuthProvider");
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
@ -3,6 +3,7 @@ import ReactDOM from "react-dom/client";
|
|||||||
import App from "./App";
|
import App from "./App";
|
||||||
import { GoogleOAuthProvider } from "@react-oauth/google";
|
import { GoogleOAuthProvider } from "@react-oauth/google";
|
||||||
import { BrowserRouter } from "react-router-dom";
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import { AuthProvider } from "./hooks/authentication/IsAuthenticated";
|
||||||
|
|
||||||
const GOOGLE_CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
|
const GOOGLE_CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
|
||||||
|
|
||||||
@ -10,7 +11,9 @@ ReactDOM.createRoot(document.getElementById("root")).render(
|
|||||||
<GoogleOAuthProvider clientId={GOOGLE_CLIENT_ID}>
|
<GoogleOAuthProvider clientId={GOOGLE_CLIENT_ID}>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Fragment>
|
<Fragment>
|
||||||
|
<AuthProvider>
|
||||||
<App />
|
<App />
|
||||||
|
</AuthProvider>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
</GoogleOAuthProvider>
|
</GoogleOAuthProvider>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user