Access to Pages in Ionic with Angular Route Guards

Ionic supports Angular 6.zero.0 and up. As part of their upgrade approach, Angular has 7 tools integrated to assist automate enhancements and provide comments to developers each time changes to an API came about. This reduces replace friction and built-in tooling the built-ings built-in an evergreen country.



Angular Tooling
With Ionic four+, the reliable Angular stack for integrated an app and rout built-ing are used, so your app can fall built-in tooling with the relaxation of the excellent Angular environment. In cases where integrated greater built-in tooling functions are needed, Ionic gives @ionic/angular-toolkit, which builds and built-integrates with the respectable Angular CLI and gives features that can be specific to @ionic/angular apps.

Integrated cowl the way to use path guards with Angular rout integrated to save you get entry to built-in routes if built-integrated situations have now not been met. A commonplace built-instance integrated of this is stop built integrated get entry to built-insure pages if the user isn't always logged integrated, and that's what we will be that specialize in.

Built-inbuilt integrated past, you could have used the Ionic navigation guards like ionViewCanEnter to built-midterm built integrated whether or not a user should navigate to a page. Now, we will use Angular’s direction guards to prevent access to positive pages built-in an Ionic/Angular software. Follow for more update at Angularjs Online Training Hyderabad

Angular path Guards
The basic concept is built integrated a course defend is that you connect a carrier that acts as the “path guard” to a particular path. That provider has a can activate approach with the built integrated go back both authentic or fake built-ing on whether or not the consumer needs to be allowed to go to that path or now not. If the can activate approach returns fake, then the person will not be able to get admission to the direction.



Direction guards make the procedure of built-in integrated built-in routes and redirect integrated the person quite easy, and built-in integrated, more conceivable than integrated navigation guards like ionViewCanEnter on character integrated components. The cease result looks built-integrated like this:

All we need to do is add one additional belong built integrated to the direction to built-in if the route can be activated. integrated routes integrated built-instance integrated above are lazy-loaded, we can also use can lead integrated can activate to entirely prevent the load integrated of the youngsters for that route.
const routes: Routes = [
  { path: "", redirectTo: "/login", pathMatch: "full" },
  { path: "login", loadChildren: "./login/login.module#LoginPageModule" },
  { path: "home", loadChildren: "./home/home.module#HomePageModule", canActivate: [AuthGuardService] }
];
It is crucial to be aware that the maximum integrated we put built integrated at the customer-facet is more for person built-in than security. patron-aspect code built-in/modifiable through the user, so that you ought to build integrated use path guards to shield records that you don’t want the consumer to look built-integrated your path guards as a pleasant security guard direct built-ing site visitors – they can preserve integrated humans away from built-in they may be no longer image built integrated to be, and direct them to where integrated they want to head, however anyone with malicious rationale may want to without difficulty run right by us built integrated the guard. integrated built-in for your utility that desires to be stored securely ought to best be available through a server that your software communicates with.

Built-integrated a course shield
Built-integrated a route guard is as easy as-built-integrated a provider that implements a can activate method. for built integrated, we ought to generate an AuthGuard provider with the integrated command:

Ionic g carrier built-in/AuthGuard
Then, all you want to do is have this can activate technique return built-in or false and you can do some integrated you want to built-midterm built integrated that value:
import { Injectable } from "@angular/core";
import { Router, CanActivate, ActivatedRouteSnapshot } from "@angular/router";

@Injectable({
  providedIn: "root"
})
export class AuthGuardService implements CanActivate {
  constructor(private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot): boolean {
    console.log(route);

    let authInfo = {
      authenticated: false
    };

    if (!authInfo.authenticated) {
      this.router.navigate(["login"]);
      return false;
    }

    return true;
  }
}

We've got simply built integrated a dummy item called authInfo that has an authenticated fee of fake. In an actual-life situation, we might just pull this authentication built-information from built integrated is responsible for authentic integrated the user. We then take a look at that price, and if the consumer isn't always authenticated we ship them returned to the log built-in page and return fake – otherwise, we simply go back built-in if you want to allow the navigation to proceed. Read more info at Angular Course

Even though we aren't built-in, we've also built-injected ActivatedRouteSnapshot on the way to assist you to get right of entry to built-information about the route that the user is navigating integrated to. you can want to build integrated the path, just like the parameters that had been provided, built-intention to built-midterm built integrated whether or now not to allow a consumer to continue. 

Attach the route guard built-in Routes
All that is left to do once you create the route defend is to import it integrated the document that built-integrated your routes, and connect it to any routes you need to shield with it:
import { NgModule } from "@angular/core";
import { PreloadAllModules, RouterModule, Routes } from "@angular/router";
import { AuthGuardService } from "./services/auth-guard.service";

const routes: Routes = [
  { path: "", redirectTo: "/login", pathMatch: "full" },
  { path: "login", loadChildren: "./login/login.module#LoginPageModule" },
  {
    path: "home",
    loadChildren: "./home/home.module#HomePageModule",
    canActivate: [AuthGuardService]
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
  exports: [RouterModule]
})
export class AppRoutingModule {}

Comments

Popular posts from this blog

Informatica Knowledge Base (IKB)

Learn About React Vs. Angular: Which one is easier

MicroStrategy Vs Tableau: 11 Thing You're Forgetting to Do