mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-09-28 01:48:57 +03:00
CI / AMD64 Build (push) Canceled after 0s
CI / AMD64 Smoke Test (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
fix duplicated styles, move fonts to src/assets/fonts for vite to bundle (nginx already rewrites correctly), and fix fast refresh for PreviewController, auth context/provider, and statusbar context
84 lines
2.0 KiB
TypeScript
84 lines
2.0 KiB
TypeScript
import axios from "axios";
|
|
import { useEffect, useState } from "react";
|
|
import useSWR from "swr";
|
|
import { AuthContext, AuthState } from "./auth-context";
|
|
|
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
const [auth, setAuth] = useState<AuthState>({
|
|
user: null,
|
|
allowedCameras: [],
|
|
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
|
|
setAuth({
|
|
user: null,
|
|
allowedCameras: [],
|
|
isLoading: false,
|
|
isAuthenticated: true,
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (profile) {
|
|
if (profile.username && profile.username !== "anonymous") {
|
|
const newUser = {
|
|
username: profile.username,
|
|
role: profile.role || "viewer",
|
|
};
|
|
|
|
const allowedCameras = Array.isArray(profile.allowed_cameras)
|
|
? profile.allowed_cameras
|
|
: [];
|
|
setAuth({
|
|
user: newUser,
|
|
allowedCameras,
|
|
isLoading: false,
|
|
isAuthenticated: true,
|
|
});
|
|
} else {
|
|
// Unauthenticated mode (anonymous)
|
|
setAuth({
|
|
user: null,
|
|
allowedCameras: [],
|
|
isLoading: false,
|
|
isAuthenticated: false,
|
|
});
|
|
}
|
|
}
|
|
}, [profile, error]);
|
|
|
|
const login = (user: AuthState["user"]) => {
|
|
setAuth((current) => ({
|
|
...current,
|
|
user,
|
|
isLoading: false,
|
|
isAuthenticated: true,
|
|
}));
|
|
};
|
|
|
|
const logout = () => {
|
|
setAuth({
|
|
user: null,
|
|
allowedCameras: [],
|
|
isLoading: false,
|
|
isAuthenticated: true,
|
|
});
|
|
axios.get("/logout", { withCredentials: true });
|
|
};
|
|
|
|
return <AuthContext value={{ auth, login, logout }}>{children}</AuthContext>;
|
|
}
|