Angular login redirects to login repeatedly
Complete Fix Guide
“Too many redirects” — You’re stuck in an infinite login loop. This guide explains why it happens and provides concrete solutions with AuthGuard, interceptors, and route configuration. Fix it once, fix it right.
📑 Table of Contents
1. Introduction
You’ve built an Angular app with authentication. You log in, and instead of going to the dashboard, you get bounced back to the login page. Or worse, the login page itself keeps reloading in an endless loop. The browser eventually shows: “This page isn’t working – too many redirects”.
This infinite redirect loop typically arises from a misconfiguration in your route guards, authentication service, or HTTP interceptors. The good news: it’s almost always an easy fix once you know where to look.
2. Common Causes
- Login route is guarded – You applied
canActivate: [AuthGuard]to the login route, so the guard checks authentication on the login page itself, fails, and redirects back to login. - AuthGuard always returns false – The guard’s logic is broken and always redirects to login, even for authenticated users.
- Interceptor catches 401 on login API – The login endpoint itself returns 401 (or any error), and the interceptor redirects to login, which then retries the call, causing a loop.
- Infinite loop due to
router.navigateinside guard – Navigating to login from inside the guard for the login route triggers the guard again. - Authentication state not properly stored – The token is not saved or is immediately invalid, so each page load sees the user as unauthenticated.
3. Fixing AuthGuard
The AuthGuard (CanActivate) is the primary gatekeeper. Here’s how to fix the most common mistakes.
✅ Correct AuthGuard Implementation
Critical point: The guard should not be applied to the login route. That’s the root cause of the loop.
🚫 Bad Route Configuration (Causes Loop)
✅ Correct Route Configuration
4. HTTP Interceptor Pitfalls
An interceptor that redirects to login on 401 can also cause a loop if the login API itself returns 401.
🚫 Bad Interceptor
If the login endpoint returns 401 (e.g., wrong credentials), the interceptor redirects to login, which tries the login API again, causing a loop.
✅ Fixed Interceptor
5. Route Configuration
Sometimes the loop is caused by a redirect rule that sends every route to login, but the login route itself is also protected.
- Fallback route: If you have
{ path: '**', redirectTo: '/login' }, make sure login is unguarded. - Empty path redirect: Ensure
redirectTodoesn’t point to a guarded route if the user is not authenticated. - Child routes: Guards applied to parent routes also affect children. If the login page is a child of a guarded route, it will be protected.
✅ Recommended Route Structure
This ensures that only authenticated users can access the child routes, while the login route remains open.
6. Other Causes
- AuthService.isLoggedIn() always returns false – Check that the token is stored and retrieved correctly. Use
localStorageor a service. - Token expired immediately – If the token is generated with zero expiration, it will be invalid right away.
- Async guard returns false before token is set – If your guard uses an observable and you haven’t handled the asynchronous flow properly, it may return false prematurely.
- Logout triggered on page load – If your app clears the token on initialization (e.g., in AppComponent), it will cause a loop.
- Browser caching of redirects – Sometimes the browser caches the redirect, causing a loop even after fixing the code. Clear browser cache or use incognito mode.
7. Debugging
- Add console logs in the AuthGuard, AuthService, and Interceptor to see the flow.
- Use browser DevTools – Network tab shows the requests; look for 401 responses. The Console tab shows navigation events.
- Check route configuration – Use Angular Router DevTools extension to visualize the route tree.
- Temporarily disable the guard to confirm that the loop stops, then re-enable and fix.
- Inspect token storage – In DevTools Application tab, check localStorage/sessionStorage to see if the token is present after login.
- Use
router.eventsto log navigation events and see the cycle.
9. Best Practices
- Never guard the login route – It must be publicly accessible.
- Exclude auth endpoints from interceptors – Avoid redirect loops on login/refresh.
- Store token immediately after login – And update the authentication state before navigating.
- Use a central AuthService – Manage token storage, expiration checks, and login status.
- Implement refresh token logic – Avoid 401 errors by refreshing tokens before they expire.
- Add a loading state – Prevent the guard from redirecting while authentication is being checked.
- Test with both logged-in and logged-out states – Ensure the guard works correctly in both scenarios.
- Use
canActivatewith observables properly – Return anObservable<boolean>with proper mapping.

0 Comments
thanks for your comments!