- Add AuthContext to manage user authentication state and token storage. - Create hooks for login, registration, and logout functionalities. - Implement dashboard layout with authentication check and loading state. - Enhance dashboard page with channel management features including create, edit, and delete channels. - Integrate API calls for channel operations and current broadcast retrieval. - Add stream URL resolution via server-side API route to handle redirects. - Update TV page to utilize new hooks for channel and broadcast management. - Refactor components for better organization and user experience. - Update application metadata for improved branding.
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { useRouter } from "next/navigation";
|
|
import { api } from "@/lib/api";
|
|
import { useAuthContext } from "@/context/auth-context";
|
|
|
|
export function useCurrentUser() {
|
|
const { token } = useAuthContext();
|
|
return useQuery({
|
|
queryKey: ["me"],
|
|
queryFn: () => api.auth.me(token!),
|
|
enabled: !!token,
|
|
retry: false,
|
|
});
|
|
}
|
|
|
|
export function useLogin() {
|
|
const { setToken } = useAuthContext();
|
|
const router = useRouter();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ email, password }: { email: string; password: string }) =>
|
|
api.auth.login(email, password),
|
|
onSuccess: (data) => {
|
|
setToken(data.access_token);
|
|
queryClient.invalidateQueries({ queryKey: ["me"] });
|
|
router.push("/dashboard");
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useRegister() {
|
|
const { setToken } = useAuthContext();
|
|
const router = useRouter();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ email, password }: { email: string; password: string }) =>
|
|
api.auth.register(email, password),
|
|
onSuccess: (data) => {
|
|
setToken(data.access_token);
|
|
queryClient.invalidateQueries({ queryKey: ["me"] });
|
|
router.push("/dashboard");
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useLogout() {
|
|
const { token, setToken } = useAuthContext();
|
|
const router = useRouter();
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: () => (token ? api.auth.logout(token) : Promise.resolve()),
|
|
onSettled: () => {
|
|
setToken(null);
|
|
queryClient.clear();
|
|
router.push("/login");
|
|
},
|
|
});
|
|
}
|