feat: implement authentication context and hooks for user management
- 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.
This commit is contained in:
60
k-tv-frontend/hooks/use-auth.ts
Normal file
60
k-tv-frontend/hooks/use-auth.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
"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");
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user