@angular/router#CanLoad TypeScript Examples

The following examples show how to use @angular/router#CanLoad. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: landing-redirect-guard.service.ts    From rubic-app with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Guard makes redirect from about route to rubic landing.
 */
@Injectable({
  providedIn: 'root'
})
export class LandingRedirectGuard implements CanActivate, CanLoad {
  private readonly redirectUrl = EXTERNAL_LINKS.LANDING;

  constructor(@Inject(WINDOW) private readonly window: RubicWindow) {}

  canActivate(): ActivationResult {
    this.window.location.assign(this.redirectUrl);
    return false;
  }

  canLoad(): LoadResult {
    this.window.location.assign(this.redirectUrl);
    return false;
  }
}
Example #2
Source File: auth.guard.ts    From App with MIT License 6 votes vote down vote up
@Injectable({
	providedIn: 'root'
})
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
	constructor(
		private restService: RestService
	) {}

	canActivate(): Observable<boolean> {
		return this.isAuth();
	}
	canActivateChild(): Observable<boolean> {
		return this.isAuth();
	}

	canLoad(): Observable<boolean> {
		return this.isAuth();
	}

	private isAuth(): Observable<boolean> {
		return this.restService.awaitAuth();
	}
}
Example #3
Source File: authorization.guard.ts    From alura_angular_rxjs_1 with MIT License 6 votes vote down vote up
@Injectable({
  providedIn: 'root',
})
export class AuthorizationGuard
  implements CanActivate, CanActivateChild, CanLoad {
  constructor(
    private authService: AuthorizationService,
    private router: Router
  ) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): canActivateReturn {
    const url: string = state.url;
    return this.checkLogin(url);
  }

  checkLogin(url: string): canActivateReturn {
    if (this.authService.isUserAuthenticated()) {
      return true;
    }
    this.authService.redirectUrl = url;
    this.router.navigate(['/login']);
    return false;
  }

  canActivateChild(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): canActivateReturn {
    return this.canActivate(route, state);
  }

  canLoad(route: Route): canActivateReturn {
    const url = `/${route.path}`;
    return this.checkLogin(url);
  }
}
Example #4
Source File: auth.guard.ts    From auth0-angular with MIT License 6 votes vote down vote up
@Injectable({
  providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanLoad, CanActivateChild {
  constructor(private auth: AuthService) {}

  canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> {
    return this.auth.isAuthenticated$.pipe(take(1));
  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> {
    return this.redirectIfUnauthenticated(state);
  }

  canActivateChild(
    childRoute: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> {
    return this.redirectIfUnauthenticated(state);
  }

  private redirectIfUnauthenticated(
    state: RouterStateSnapshot
  ): Observable<boolean> {
    return this.auth.isAuthenticated$.pipe(
      tap((loggedIn) => {
        if (!loggedIn) {
          this.auth.loginWithRedirect({
            appState: { target: state.url },
          });
        }
      })
    );
  }
}
Example #5
Source File: app.guard.ts    From budget-angular with GNU General Public License v3.0 6 votes vote down vote up
@Injectable({
  providedIn: "root",
})
export class AppGuard implements CanActivate, CanLoad {
  constructor(private authService: AuthService, private router: Router) {}

  canActivate(): Observable<boolean> {
    return this.canLoad();
  }

  canLoad(): Observable<boolean> {
    return this.authService.isLoggedIn$().pipe(
      tap((isLoggedIn) => {
        if (!isLoggedIn) {
          this.router.navigate(["/login"]);
        }
      })
    );
  }
}
Example #6
Source File: auth.guard.ts    From mns with MIT License 6 votes vote down vote up
@Injectable({
  providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
  constructor(private authService: AuthService, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    const url: string = state.url;

    return this.checkLogin(url);
  }

  canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    return this.canActivate(route, state);
  }

  canLoad(route: Route): boolean {
    // Store the entry url i.e. /admin-- > @Idrice am i not right?
    const url = `/${route.path}`;

    return this.checkLogin(url);
  }

  checkLogin(url: string): boolean {
    if (this.authService.isLoggedIn()) {
      return true;
    }

    this.authService.redirectUrl = url;
    this.router.navigate(['/login'], { queryParams: { returnUrl: url } });
    // Store the attempted URL for redirecting
    this.authService.redirectUrl = url;
    return false;


  }
}
Example #7
Source File: check-tutorial.service.ts    From actions-test with Apache License 2.0 6 votes vote down vote up
@Injectable({
  providedIn: 'root'
})
export class CheckTutorial implements CanLoad {
  constructor(private storage: Storage, private router: Router) {}

  canLoad() {
    return this.storage.get('ion_did_tutorial').then(res => {
      if (res) {
        this.router.navigate(['/app', 'tabs', 'schedule']);
        return false;
      } else {
        return true;
      }
    });
  }
}
Example #8
Source File: auth.guard.ts    From onchat-web with Apache License 2.0 6 votes vote down vote up
@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate, CanLoad {
  constructor(
    private authService: AuthService,
    private socket: Socket,
    private globalData: GlobalData,
  ) { }

  private handle(): boolean | Observable<boolean> {
    if (this.globalData.user) { return true; }

    return this.authService.info().pipe(
      catchError(() => of(false)),
      map(({ data }: Result<User>) => {
        if (data) {
          this.globalData.user = data;
          this.socket.connect();
        }
        return true;
      })
    );
  }

  canLoad(route: Route, segments: UrlSegment[]): boolean | Promise<boolean> | Observable<boolean> {
    return this.handle();
  }

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.handle();
  }

}
Example #9
Source File: not-auth.guard.ts    From onchat-web with Apache License 2.0 6 votes vote down vote up
@Injectable({
  providedIn: 'root'
})
export class NotAuthGuard implements CanActivate, CanLoad {
  constructor(
    private globalData: GlobalData,
    private router: Router
  ) { }

  private handle(): boolean | Observable<boolean> {
    const { user } = this.globalData;

    user && this.router.navigateByUrl('/');

    return !user;
  }

  canLoad(route: Route, segments: UrlSegment[]): boolean | Promise<boolean> | Observable<boolean> {
    return this.handle();
  }

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return this.handle();
  }

}
Example #10
Source File: auth-guard.service.ts    From nestjs-angular-starter with MIT License 6 votes vote down vote up
@Injectable()
export class AuthGuardService implements CanActivate, CanLoad {
  constructor(public router: Router, private authService: AuthService) {}

  canActivate(route: ActivatedRouteSnapshot): boolean | Observable<boolean> {
    return this.checkAuthentication(route.data && route.data['roles']);
  }

  canLoad(route: Route): boolean | Observable<boolean> {
    return this.checkAuthentication(route.data && route.data['roles']);
  }

  checkAuthentication(roles?: string[]): boolean | Observable<boolean> {
    if (roles) {
      return this.authService.hasRolesAsync(roles);
    }

    return this.authService.isLoggedInAsync;
  }
}
Example #11
Source File: feature-toggle.guard.ts    From canopy with Apache License 2.0 5 votes vote down vote up
@Injectable({
  providedIn: 'root',
})
export class FeatureToggleGuard implements CanActivate, CanLoad {
  constructor(
    private featureToggleService: LgFeatureToggleService,
    private router: Router,
  ) {}

  canLoad(route: Route) {
    return this.isActive(route);
  }

  canActivate(route: ActivatedRouteSnapshot) {
    return this.isActive(route);
  }

  canActivateChild(childRoute: ActivatedRouteSnapshot) {
    return this.isActive(childRoute);
  }

  isActive(route: Route | ActivatedRouteSnapshot) {
    return this.featureToggleService.toggles$.pipe(
      first(),
      map(configToggles => {
        const active = getDataPropertyValues(route, 'featureToggle')
          .map(t => {
            const value = configToggles[t];

            return value === undefined || value;
          })
          .reduce((acc, current) => acc && current, true);

        if (!active) {
          this.router.navigate([ '/' ], { queryParamsHandling: 'merge' });
        }

        return active;
      }),
    );
  }
}