mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-04-15 03:22:10 +03:00
update auth hook for allowed cameras
This commit is contained in:
parent
909e8d2825
commit
953e467b56
@ -3,7 +3,8 @@ import { createContext, useEffect, useState } from "react";
|
|||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
|
|
||||||
interface AuthState {
|
interface AuthState {
|
||||||
user: { username: string; role: "admin" | "viewer" | null } | null;
|
user: { username: string; role: string | null } | null;
|
||||||
|
allowedCameras: string[];
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
isAuthenticated: boolean; // true if auth is required
|
isAuthenticated: boolean; // true if auth is required
|
||||||
}
|
}
|
||||||
@ -15,7 +16,12 @@ interface AuthContextType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthContextType>({
|
export const AuthContext = createContext<AuthContextType>({
|
||||||
auth: { user: null, isLoading: true, isAuthenticated: false },
|
auth: {
|
||||||
|
user: null,
|
||||||
|
allowedCameras: [],
|
||||||
|
isLoading: true,
|
||||||
|
isAuthenticated: false,
|
||||||
|
},
|
||||||
login: () => {},
|
login: () => {},
|
||||||
logout: () => {},
|
logout: () => {},
|
||||||
});
|
});
|
||||||
@ -23,6 +29,7 @@ export const AuthContext = createContext<AuthContextType>({
|
|||||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||||
const [auth, setAuth] = useState<AuthState>({
|
const [auth, setAuth] = useState<AuthState>({
|
||||||
user: null,
|
user: null,
|
||||||
|
allowedCameras: [],
|
||||||
isLoading: true,
|
isLoading: true,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
});
|
});
|
||||||
@ -38,7 +45,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
if (error) {
|
if (error) {
|
||||||
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
||||||
// auth required but not logged in
|
// auth required but not logged in
|
||||||
setAuth({ user: null, isLoading: false, isAuthenticated: true });
|
setAuth({
|
||||||
|
user: null,
|
||||||
|
allowedCameras: [],
|
||||||
|
isLoading: false,
|
||||||
|
isAuthenticated: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -49,20 +61,44 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|||||||
username: profile.username,
|
username: profile.username,
|
||||||
role: profile.role || "viewer",
|
role: profile.role || "viewer",
|
||||||
};
|
};
|
||||||
setAuth({ user: newUser, isLoading: false, isAuthenticated: true });
|
|
||||||
|
const allowedCameras = Array.isArray(profile.allowed_cameras)
|
||||||
|
? profile.allowed_cameras
|
||||||
|
: [];
|
||||||
|
setAuth({
|
||||||
|
user: newUser,
|
||||||
|
allowedCameras,
|
||||||
|
isLoading: false,
|
||||||
|
isAuthenticated: true,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Unauthenticated mode (anonymous)
|
// Unauthenticated mode (anonymous)
|
||||||
setAuth({ user: null, isLoading: false, isAuthenticated: false });
|
setAuth({
|
||||||
|
user: null,
|
||||||
|
allowedCameras: [],
|
||||||
|
isLoading: false,
|
||||||
|
isAuthenticated: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [profile, error]);
|
}, [profile, error]);
|
||||||
|
|
||||||
const login = (user: AuthState["user"]) => {
|
const login = (user: AuthState["user"]) => {
|
||||||
setAuth({ user, isLoading: false, isAuthenticated: true });
|
setAuth((current) => ({
|
||||||
|
...current,
|
||||||
|
user,
|
||||||
|
isLoading: false,
|
||||||
|
isAuthenticated: true,
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
setAuth({ user: null, isLoading: false, isAuthenticated: true });
|
setAuth({
|
||||||
|
user: null,
|
||||||
|
allowedCameras: [],
|
||||||
|
isLoading: false,
|
||||||
|
isAuthenticated: true,
|
||||||
|
});
|
||||||
axios.get("/logout", { withCredentials: true });
|
axios.get("/logout", { withCredentials: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
7
web/src/hooks/use-allowed-cameras.ts
Normal file
7
web/src/hooks/use-allowed-cameras.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { useContext } from "react";
|
||||||
|
import { AuthContext } from "@/context/auth-context";
|
||||||
|
|
||||||
|
export function useAllowedCameras() {
|
||||||
|
const { auth } = useContext(AuthContext);
|
||||||
|
return auth.allowedCameras;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user