Advertisement
devraselmiah

admin

Mar 22nd, 2025
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.14 KB | Source Code | 0 0
  1. 'use server';
  2.  
  3. import { getServerSession } from 'next-auth';
  4. import { authOptions } from '@/app/api/auth/[...nextauth]/route';
  5.  
  6. export async function getPermissions() {
  7.   const session = await getServerSession(authOptions);
  8.  
  9.   if (!session?.access_token) {
  10.     throw new Error('Unauthorized');
  11.   }
  12.  
  13.   const response = await fetch(
  14.     `${process.env.INTERNAL_API_URL}/v3/admin/permissions`,
  15.     {
  16.       headers: {
  17.         Authorization: `Bearer ${session.access_token}`,
  18.         'Content-Type': 'application/json'
  19.       },
  20.       next: { tags: ['permissions'] }
  21.     }
  22.   );
  23.  
  24.   if (!response.ok) {
  25.     throw new Error('Failed to fetch permissions');
  26.   }
  27.  
  28.   return response.json();
  29. }
  30.  
  31. 'use client';
  32.  
  33. import { useQuery } from '@tanstack/react-query';
  34. import { getPermissions } from '@/actions/get-permissions';
  35.  
  36. export const usePermissions = () => {
  37.   return useQuery({
  38.     queryKey: ['permissions'],
  39.     queryFn: () => getPermissions(),
  40.     staleTime: 1000 * 60 * 5, // 5 minutes
  41.     retry: (failureCount, error) => {
  42.       if (error.message === 'Unauthorized') return false;
  43.       return failureCount < 2;
  44.     }
  45.   });
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement