Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use server';
- import { getServerSession } from 'next-auth';
- import { authOptions } from '@/app/api/auth/[...nextauth]/route';
- export async function getPermissions() {
- const session = await getServerSession(authOptions);
- if (!session?.access_token) {
- throw new Error('Unauthorized');
- }
- const response = await fetch(
- `${process.env.INTERNAL_API_URL}/v3/admin/permissions`,
- {
- headers: {
- Authorization: `Bearer ${session.access_token}`,
- 'Content-Type': 'application/json'
- },
- next: { tags: ['permissions'] }
- }
- );
- if (!response.ok) {
- throw new Error('Failed to fetch permissions');
- }
- return response.json();
- }
- 'use client';
- import { useQuery } from '@tanstack/react-query';
- import { getPermissions } from '@/actions/get-permissions';
- export const usePermissions = () => {
- return useQuery({
- queryKey: ['permissions'],
- queryFn: () => getPermissions(),
- staleTime: 1000 * 60 * 5, // 5 minutes
- retry: (failureCount, error) => {
- if (error.message === 'Unauthorized') return false;
- return failureCount < 2;
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement