From fd7268f041d3042bbdbb6b9bdbfc609e26fae97c Mon Sep 17 00:00:00 2001 From: Arthur Belleville Date: Mon, 24 Mar 2025 21:21:30 +0100 Subject: [PATCH] Some ui tmp changes --- ui/src/components/ProtectedRoute.tsx | 12 ++++-------- ui/src/components/SignOutButton.tsx | 14 +++----------- ui/src/contexts/AuthContext.tsx | 5 +++++ ui/src/hooks/auth.ts | 18 ++++++++++++++++-- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/ui/src/components/ProtectedRoute.tsx b/ui/src/components/ProtectedRoute.tsx index 9c5d0a7..fc90e7c 100644 --- a/ui/src/components/ProtectedRoute.tsx +++ b/ui/src/components/ProtectedRoute.tsx @@ -1,17 +1,13 @@ -import { Navigate } from "react-router-dom"; import { ReactNode } from "react"; -import { useAuth } from "../contexts/AuthContext"; interface ProtectedRouteProps { children: ReactNode; } export const ProtectedRoute = ({ children }: ProtectedRouteProps) => { - const { isAuthenticated } = useAuth(); - - if (!isAuthenticated) { - // Redirect to login page if user is not authenticated - return ; - } + // if (!isAuthenticated) { + // // Redirect to login page if user is not authenticated + // return ; + // } // If authenticated, render the protected component return <>{children}; diff --git a/ui/src/components/SignOutButton.tsx b/ui/src/components/SignOutButton.tsx index 7d38512..058ee9f 100644 --- a/ui/src/components/SignOutButton.tsx +++ b/ui/src/components/SignOutButton.tsx @@ -1,15 +1,7 @@ -import { useAuth } from "../contexts/AuthContext"; -import { useNavigate } from "react-router-dom"; import { Button } from "../ui-library/button"; +import { useLogout } from "../hooks/auth"; export const SignOutButton = () => { - const { logout } = useAuth(); - const navigate = useNavigate(); - - const handleSignOut = () => { - logout(); - navigate("/landing"); - }; - - return ; + const { mutate: logout } = useLogout(); + return ; }; diff --git a/ui/src/contexts/AuthContext.tsx b/ui/src/contexts/AuthContext.tsx index 5c5f548..0a41247 100644 --- a/ui/src/contexts/AuthContext.tsx +++ b/ui/src/contexts/AuthContext.tsx @@ -1,5 +1,6 @@ import { jwtDecode } from "jwt-decode"; import { createContext, useContext, useState, ReactNode } from "react"; +import { api } from "../lib/api"; interface UserMetadata { email: string; @@ -62,6 +63,10 @@ export const AuthProvider = ({ children }: AuthProviderProps) => { const login = (token: string) => { localStorage.setItem("auth_token", token); setIsAuthenticated(true); + api.interceptors.request.use(function (config) { + config.headers.Authorization = `Bearer ${token}`; + return config; + }); const dcdToken = decodeToken(token); if (dcdToken) { setUser(dcdToken.user_metadata); diff --git a/ui/src/hooks/auth.ts b/ui/src/hooks/auth.ts index 8d5ee01..4a9b0f7 100644 --- a/ui/src/hooks/auth.ts +++ b/ui/src/hooks/auth.ts @@ -128,8 +128,22 @@ export function useLoginWithGoogle() { const { auth_url } = response.data; return auth_url; }, - onSuccess: (data) => { - window.location.href = data; + onSuccess: (url) => { + window.location.href = url; + console.log("url", url); + }, + }); +} + +export function useLogout() { + const { logout } = useAuth(); + return useMutation({ + mutationFn: async () => { + const response = await api.post("/auth/logout"); + return response.data; + }, + onSuccess: () => { + logout(); }, }); }