2025-03-08 19:01:08 +03:00
|
|
|
import axios from "axios";
|
|
|
|
|
import { createContext, useEffect, useState } from "react";
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
|
|
interface AuthState {
|
2025-09-12 14:19:29 +03:00
|
|
|
user: { username: string; role: string | null } | null;
|
|
|
|
|
allowedCameras: string[];
|
2025-03-08 19:01:08 +03:00
|
|
|
isLoading: boolean;
|
|
|
|
|
isAuthenticated: boolean; // true if auth is required
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AuthContextType {
|
|
|
|
|
auth: AuthState;
|
|
|
|
|
login: (user: AuthState["user"]) => void;
|
|
|
|
|
logout: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const AuthContext = createContext<AuthContextType>({
|
2025-09-12 14:19:29 +03:00
|
|
|
auth: {
|
|
|
|
|
user: null,
|
|
|
|
|
allowedCameras: [],
|
|
|
|
|
isLoading: true,
|
|
|
|
|
isAuthenticated: false,
|
|
|
|
|
},
|
2025-03-08 19:01:08 +03:00
|
|
|
login: () => {},
|
|
|
|
|
logout: () => {},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
|
const [auth, setAuth] = useState<AuthState>({
|
|
|
|
|
user: null,
|
2025-09-12 14:19:29 +03:00
|
|
|
allowedCameras: [],
|
2025-03-08 19:01:08 +03:00
|
|
|
isLoading: true,
|
|
|
|
|
isAuthenticated: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { data: profile, error } = useSWR("/profile", {
|
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
|
revalidateOnReconnect: true,
|
|
|
|
|
fetcher: (url) =>
|
|
|
|
|
axios.get(url, { withCredentials: true }).then((res) => res.data),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (error) {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
|
|
|
|
// auth required but not logged in
|
2025-09-12 14:19:29 +03:00
|
|
|
setAuth({
|
|
|
|
|
user: null,
|
|
|
|
|
allowedCameras: [],
|
|
|
|
|
isLoading: false,
|
|
|
|
|
isAuthenticated: true,
|
|
|
|
|
});
|
2025-03-08 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (profile) {
|
|
|
|
|
if (profile.username && profile.username !== "anonymous") {
|
|
|
|
|
const newUser = {
|
|
|
|
|
username: profile.username,
|
|
|
|
|
role: profile.role || "viewer",
|
|
|
|
|
};
|
2025-09-12 14:19:29 +03:00
|
|
|
|
|
|
|
|
const allowedCameras = Array.isArray(profile.allowed_cameras)
|
|
|
|
|
? profile.allowed_cameras
|
|
|
|
|
: [];
|
|
|
|
|
setAuth({
|
|
|
|
|
user: newUser,
|
|
|
|
|
allowedCameras,
|
|
|
|
|
isLoading: false,
|
|
|
|
|
isAuthenticated: true,
|
|
|
|
|
});
|
2025-03-08 19:01:08 +03:00
|
|
|
} else {
|
|
|
|
|
// Unauthenticated mode (anonymous)
|
2025-09-12 14:19:29 +03:00
|
|
|
setAuth({
|
|
|
|
|
user: null,
|
|
|
|
|
allowedCameras: [],
|
|
|
|
|
isLoading: false,
|
|
|
|
|
isAuthenticated: false,
|
|
|
|
|
});
|
2025-03-08 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [profile, error]);
|
|
|
|
|
|
|
|
|
|
const login = (user: AuthState["user"]) => {
|
2025-09-12 14:19:29 +03:00
|
|
|
setAuth((current) => ({
|
|
|
|
|
...current,
|
|
|
|
|
user,
|
|
|
|
|
isLoading: false,
|
|
|
|
|
isAuthenticated: true,
|
|
|
|
|
}));
|
2025-03-08 19:01:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const logout = () => {
|
2025-09-12 14:19:29 +03:00
|
|
|
setAuth({
|
|
|
|
|
user: null,
|
|
|
|
|
allowedCameras: [],
|
|
|
|
|
isLoading: false,
|
|
|
|
|
isAuthenticated: true,
|
|
|
|
|
});
|
2025-03-08 19:01:08 +03:00
|
|
|
axios.get("/logout", { withCredentials: true });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AuthContext.Provider value={{ auth, login, logout }}>
|
|
|
|
|
{children}
|
|
|
|
|
</AuthContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|