Auth0 angular 7 login redirect loop

I am using auth0 to configure my angular 7 app with auth. I followed the quickstart guide to get up and running. Also this is running on S3 (not sure if that matters) The only thing I added in addition to the auth.service.ts file is:

  getTokenSilently$(options?): Observable<string> {
    return this.auth0Client$.pipe(
      concatMap((client: Auth0Client) => from(client.getTokenSilently(options)))
    );
  }

to support the HTTP interceptor:

import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable, throwError } from 'rxjs';
import { mergeMap, catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class InterceptorService implements HttpInterceptor {

  constructor(private auth: AuthService) { }

  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return this.auth.getTokenSilently$().pipe(
      mergeMap(token => {
        const tokenReq = req.clone({
          setHeaders: { Authorization: `Bearer ${token}` }
        });
        return next.handle(tokenReq);
      }),
      catchError(err => throwError(err))
    )
  }
}

The other thing I did differently is that my app does not have a login button, when the app starts up it trys to go to the home page which has an auth guard:

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, CanActivate } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from './auth.service';
import { tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(private auth: AuthService) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean|UrlTree> | boolean {
    return this.auth.isAuthenticated$.pipe(
      tap(loggedIn => {
        if (!loggedIn) {
          this.auth.login(state.url);
        }
      })
    );
  }

}

so it automatically calls the login so the user gets redirected to auth0. Once they enter their creds it then redirects them back to the home page. Then all of a sudden the auth.service.ts service is loaded again and the auth guard has fired again and it fires the login function again. This seems to just keep on looping.

Any idea why it just keeps on looping?

  • Which SDK does this apply to? auth-node
  • Which verison of the SDK you are using? “@auth0/auth0-spa-js”: “^1.8.2”,
  • Which version of the platform are you facing this error on? node v10.16.3, angular 7
  • Was this code working before? Have you made any changes in the dashboard recently? No never worked
  • Please capture and attach the stacktrace, it helps a lot!

Keep getting this error in browser console:
core.js:5847 ERROR TypeError: Cannot read property ‘querySelectorAll’ of null
at Object.Chartist.createSvg (chartist.js:333)
at constr.createChart (chartist.js:3377)
at constr.initialize (chartist.js:1930)
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:24328)
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push…/node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at push…/node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (zone.js:498)
at ZoneTask.invoke (zone.js:487)
at timer (zone.js:3070)

Encountered the same issue, wasted few hours debugging so leaving the answer in case someone else will have this problem as well. The root cause and solution here: Losing login credentials on refresh using @auth0/auth0-spa-js
Basically, don’t use an account created with Google Social Login (create a user in regular database connection) or don’t use Auth0 dev keys for the Google social connection.