← Back to library
Playwrightauthintermediatev1.20+
Assert redirect for unauthenticated user
Verify that a protected route redirects to /login when no session is present.
1test('redirects unauthenticated user to /login', async ({ page }) => {
2 // Navigate without any auth cookies or localStorage session
3 await page.goto('/dashboard');
4
5 // Playwright waits for navigation to settle before asserting
6 await expect(page).toHaveURL(//login/);
7 await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible();
8});#redirect#protected route#unauthenticated#url