Some ui tmp changes
This commit is contained in:
parent
9b78fea02d
commit
fd7268f041
4 changed files with 28 additions and 21 deletions
|
|
@ -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 <Navigate to="/landing" replace />;
|
||||
}
|
||||
// if (!isAuthenticated) {
|
||||
// // Redirect to login page if user is not authenticated
|
||||
// return <Navigate to="/landing" replace />;
|
||||
// }
|
||||
|
||||
// If authenticated, render the protected component
|
||||
return <>{children}</>;
|
||||
|
|
|
|||
|
|
@ -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 <Button onPress={handleSignOut}>Se déconnecter</Button>;
|
||||
const { mutate: logout } = useLogout();
|
||||
return <Button onPress={() => logout()}>Se déconnecter</Button>;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue