2

This might be a weird situation but please bear with me - We generally incorporate a feature in Angular Apps that any unresolved path should go to Home Page. Is there a way to get the value of that unresolved path?

I am using routing to divert any unresolved path to Login. Once successfully logged in user is taken to Home page. But I want to also capture that wrong path and make some decision based upon this. How can I do this?

My router file looks like this:

export const ROUTES: Routes = [
  { path: '', redirectTo: 'app', pathMatch: 'full' },
  { path: 'app',   loadChildren: () => System.import('./layout/layout.module') },
  { path: 'login', loadChildren: () => System.import('./login/login.module') },
  { path: 'auth', loadChildren: () => System.import('./auth/auth.module')},
  { path: 'error', component: ErrorComponent },
  { path: '**',    redirectTo: 'login', canActivate: [AuthGuard] }
];

and AuthGuard service is:

import { Injectable }       from '@angular/core';
import {
  Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  CanActivate,
  CanActivateChild
}                           from '@angular/router';
import { AuthService }      from './auth.service';

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
  constructor(private authService: AuthService, private router: Router) {}

  // On clicking a route or entering a URL in the address bar, check
  // whether the user is logged in and can proceed to the route.
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;
    return this.checkLogin(url);
  }

  // On clicking a sub-route, check whether the user is logged in.
  canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    return this.canActivate(route, state);
  }

  // If the user is logged in, he can proceed to the intended route.
  // If he's not logged in, redirect him to the login page.
  checkLogin(url: string): boolean {
    if (this.authService.isLoggedIn()) {
      return true;
    }

    // Store the attempted URL for redirecting after logging in
    this.authService.redirectUrl = url;

    // Navigate to the login page with extras
    this.router.navigate(['/login']);

    return false;
  }
}
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
mehtak
  • 137
  • 1
  • 14
  • This is not `angularjs` or `angular-routing` question. Please, read the descriptions of tags before using them. – Estus Flask Apr 20 '17 at 12:51
  • @estus You may say that the browser url can be get using `window.location()` but I tagged it as angularjs and routing question because I think it has something to do with AuthGuard service. – mehtak Apr 20 '17 at 12:57
  • 1
    Angular 2 (aka Angular) is not AngularJS. They are different frameworks. That's the point. – Estus Flask Apr 20 '17 at 12:59
  • Do you suggest using `angular2` or `angularjs` – mehtak Apr 20 '17 at 13:03
  • 1
    angular2 (it is a synonym of angular). You can read tag descriptions by hovering them, there is information about what these tags are for. – Estus Flask Apr 20 '17 at 13:05
  • Possible duplicate of [Angular2: How to find out what was previous page url when using angular2 routing](http://stackoverflow.com/questions/35279291/angular2-how-to-find-out-what-was-previous-page-url-when-using-angular2-routing) – Estus Flask Apr 20 '17 at 13:37

0 Answers0