Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
Is it possible to call an API based on user roles with useSWR?

I'm wondering if it is possible to use useSWR from Vercel if a condition is true? I.e., if RoleOne is true, then call:

let { data: roleOneData, error: roleOneError } = useSWR(
    "/api/getRoleOneInfo",
    fetcher
  )

My Next.js app is dealing with different user roles, and all user roles are sharing the same Application Shell, which means I call all user role specific API's in the Application Shell. I would like to only call the API I need if it can see a specific user role is true. I'm getting the roles from the userData API.

Here are my fetchers which are used in various functions:

let { data: userData, error: userError } = useSWR("/api/getUserInfo", fetcher)

let { data: roleOneData, error: roleOneError } = useSWR(
    "/api/getRoleOneInfo",
    fetcher
  )

let { data: roleTwoData, error: roleTwoError } = useSWR(
    "/api/getRoleTwoInfo",
    fetcher
  )

If I'm logging in as RoleOne, I'm getting API errors for getRoleTwoInfo and vice versa. I would like to avoid calling RoleTwo API if I'm logged in as RoleOne and the other way around. How can I accomplish that?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Definitely possible and described in this page in the documentation.

You just have to make your key (the url of the request in your case) to be falsy when you don't want the request to happen.

let { data: userData, error: userError } = useSWR("/api/getUserInfo", fetcher)

let { data: roleOneData, error: roleOneError } = useSWR(
    userData?.role === "one" ? "/api/getRoleOneInfo" : null,
    fetcher
  )

let { data: roleTwoData, error: roleTwoError } = useSWR(
    userData?.role === "two" ? "/api/getRoleTwoInfo" : null,
    fetcher
  )
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda