skip to Main Content

i have a project in ionic 5 .
after successful login and close the app
after again opening the app shows a 1 sec login page and then redirect to routed page ,which my case is home
how to prevent it from showing the page using guard

i have done these and getting device fireup issue

route page

path: '',
canActivate:[LoginGuard],
children:[
  
]

LoginGuard page

  public async canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Promise<any> {
      let res = await this.storage.get('loggedIn');
      console.log(res)
      if (res == null) res = 0
      if(res == 0){
        return this.route.parseUrl('/login');
      }else if (res == 1) {
        return this.route.parseUrl('/home');
      }
      
  }

2

Answers


  1. Chosen as BEST ANSWER

    Fixed

    just removed/commented

    path: '',
    canActivate:[LoginGuard],
    children:[
    
     ]
    

    and added athentication check and redirection in app.components.ts....as i done in ionic 3....which works i guess for now


  2. Just a wild guess, but could you try the below code?

    public canActivate(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): any {
          let res = this.storage.get('loggedIn');
          console.log(res);
          if (res == null) {
               res = 0;
          }
          if(res == 0){
            return this.route.navigate(['/login']);
          }else if (res == 1) {
            return this.route.navigate(['/home']);
          }          
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search