N
Nuxt2y 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: "...",
},
});
// 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);
// 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(...);
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)
2 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Julien
Julien2y ago
if you want to redirect your page, get the fetch response and THEN make the redirection within your page component
Want results from more Discord servers?
Add your server