How is the recommended way to implement secure routes (one route that is secure has, initially, a redirect to a login page that is erased from the navigation stack as soon as the user is logged in).
Check here for the angular docs on CanActivate:
Angular CanActivate
For Ionic v4, you will want to add
canActivate: [AuthGuard]
to the specific route you want to protect
then you want to create an auth.guard.ts
file that interacts with your authentication service/provider and implements the ‘canActivate’ method to return a boolean value whether the user can navigate to the intended route:
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable() // guards are services
export class AuthGuardService implements CanActivate { // needs to implement CanActivate
constructor(public auth: AuthService, public router: Router) {}
canActivate(): boolean {
if (!this.auth.isAuthenticated()) { // method in auth service to determine user status
this.router.navigate(['login']); // redirects to login page
return false; // desired page 'cannot activate'
}
return true; // desired page 'can activate' if user is logged
}
Thank you, but I’m using @ionic/core (with StencilJS), not Angular.
Ah sorry about the misunderstanding
Is this what you’re looking for?
StencilJS - Router redirect