#Astro.redirect doesn't work

2 messages · Page 1 of 1 (latest)

lime yarrowBOT
#

Hi @storm forge. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I'm sorry, but I can't access the link you provided. However, based on the information available in the extracts, here's a general guide on how to use Astro.redirect() in your Astro project:

In Astro, you can use the Astro.redirect() method to redirect to another page. This method should be used in a page, not a child component, and the result of Astro.redirect() must be returned for the redirect to occur. Here's an example:

---
import { isLoggedIn } from '../utils';

const cookie = Astro.request.headers.get('cookie');

// If the user is not logged in, redirect them to the login page
if (!isLoggedIn(cookie)) {
 return Astro.redirect('/login');
}
---

In this example, the isLoggedIn function checks if the user is logged in. If the user is not logged in, the Astro.redirect() function is called to redirect the user to the login page.

If your redirect is not working, make sure that you are returning the result of Astro.redirect(). Also, ensure that the condition for the redirect is being met.

For more information, you can refer to the Astro documentation on routing (https://docs.astro.build/en/core-concepts/routing) and the Astro API reference (https://docs.astro.build/en/reference/api-reference#astroredirect).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: