NuxtN
Nuxt3y ago
Gerbuuun

redirect after useFetch redirect response

When I use useFetch on a page to (for example) log in, I return a sendRedirect from the api route like so:
// login.vue
await useFetch("/api/auth/login, {
  method: "POST",
  body: {
    email: "...",
    password: "...",
  },
});

// server/api/auth/login.post.ts
export default defineEventHandler(async (event) => {

  // Do login stuff...

  return sendRedirect(event, "/", 302);

This redirect, however, does not redirect the app but returns the redirected page as the data in:
const { data } = await useFetch(...);

Are redirects supported by the useFetch function? Or do I need to catch the 302 http response and redirect manually?
(Same goes for $fetch. I might not quite understand how the function works)
Was this page helpful?