I have a unique condition in my AuthGuard code. If the resetpasss variable is not null, I want to navigate to the ResetPassIdComponent; otherwise, I want to navigate to the LoginFirstComponent.
Here is my implementation of AuthGuard:
export class AuthGuard implements CanActivate {
myappurl: any;
resetpasss: any;
constructor(private router: Router, private auth: LoginService) {
}
geturl() {
handleOpenURL((appURL: AppURL) => {
console.log('Got the following appURL1', appURL);
this.myappurl = appURL
let url_1 = this.myappurl.toString();
let url_id = url_1.split("/").reverse()[0];
this.resetpasss = url_id
let LS = require("nativescript-localstorage");
LS.setItem(this.resetpasss)
// this.router.navigateByUrl('/resetPasswordRequest/' + this.resetpasss);
console.log('this.resetpass1', this.resetpasss)
});
return true;
}
canActivate(): boolean {
this.geturl();
if (this.auth.isAuthenticated()) {
return true;
}
console.log('this.resetpasss2', this.resetpasss)
this.router.navigate(['/outsidelogin/login']);
return false;
}
}
When clicking a link from an email, the geturl()
function is triggered. This function retrieves an id and saves it in local storage. In the canActivate()
method, I create a condition for navigating to the desired component based on the saved id.
Do you have any suggestions on how to improve this condition?
This is my routing configuration:
const routes: Routes = [
{
path: 'home',
component: HomeComponent,
canActivate: [AuthGuard],
children: [
{
path: 'fp', component: FirstPageComponent
}
]
},
{
path: 'outsidelogin',
component: outsideloginComponent,
children: [
{ path: 'login', component: LoginFirstComponent },
{ path: 'register', component: RegisterComponent },
]
},
{ path: '', redirectTo: '/home/fp', pathMatch: 'full' },
{ path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
];