@RouteConfig([
{
path: '/login',
name: 'Login',
component: LoginComponent
},
{
path: '/search',
name: 'Search',
component: SearchComponent,
needAuth: true
},
{
path: '/result/:searchString',
name: 'Result',
component: ResultComponent,
needAuth: true
},
{path: '/**', redirectTo: ['Login']}
])
I have defined a configuration like the one above. Now I am trying to figure out how to identify the current route object, specifically for routes that require authentication before access.
{
path: '/result/:searchString',
name: 'Result',
component: ResultComponent,
needAuth: true
}
My goal is to create functionality in my app where if a user is not logged in and the current route object has the needAuth property set to true, they will be redirected to the login page; otherwise, they should be directed to the search page.
After some research, I came across this solution:
this._router.subscribe((url) => {
this._router.recognize(url).then((instruction) => {
if(this._authService.getUser() == null && instruction.component.routeData.data.needAuth) {
this._router.navigate(['Login'])
}
if(this._authService.getUser() !=null && !instruction.component.routeData.data.needAuth) {
this._router.navigate(['Search'])
}
});
});