@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 a config as this, how can I detect current route object like;
{
path: '/result/:searchString',
name: 'Result',
component: ResultComponent,
needAuth: true
}
for route restriction when user not logged in.
I want to make like this;
export class AppComponent {
constructor(private _authService:AuthenticationService) {
if (this._authService.getUser() != null && CURRENT ROUTE OBJECT's needAuth PROPERTY is TRUE) {
this._router.navigate(['Search'])
} else {
this._router.navigate(['Login'])
}
}
}
All kind of helps are welcome.
**************UPDATE********************
I found 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'])
}
});
});