I have a login form which should redirect to the main applications page:
I am unsure how to navigate to this page: app/analytics and think that it's due to the set up of my routes that I am unable to navigate to the correct page upon submission of a login form.
This is what I have attempted so far.
app.routes
export const ROUTES: Routes = [
{
path: 'login', loadChildren: './pages/login/login.module#LoginModule'
},
{
path: '', redirectTo: 'app/analytics', pathMatch: 'full'
},
{
path: 'app', loadChildren: './layout/layout.module#LayoutModule', canActivate: [AuthGuard]
},
{
path: 'error', component: ErrorComponent
},
{
path: '**', component: ErrorComponent
}
];
layout.routes
const routes: Routes = [
{ path: '', component: LayoutComponent, children: [
{ path: 'analytics', loadChildren: '../pages/analytics/analytics.module#AnalyticsModule' },
{ path: 'profile', loadChildren: '../pages/profile/profile.module#ProfileModule' },
{ path: 'widgets', loadChildren: '../pages/widgets/widgets.module#WidgetsModule' },
]}
];
export const ROUTES = RouterModule.forChild(routes);
My router-outlet exists in layout.template and app.component
I have a login component that calls
this.router.navigate['app/analytics']
export class LoginComponent {
@HostBinding('class') classes = 'login-page app';
constructor(private auth: AuthService, private router: Router) {}
loginUser(event) {
event.preventDefault();
const target = event.target;
console.log(target)
const username = target.querySelector('#username').value;
const password = target.querySelector('#password').value;
this.auth.getUserDetails(username, password).subscribe(data => {
console.log(data.status)
if(data.status == "success") {
this.router.navigate['app/analytics'];
this.auth.setLoggedIn(true);
console.log(this.auth.isLoggedIn);
}
else {
window.alert(data.message)
}
});
}
}
This is called on this my login.template:
<form class="login-form mt-lg" (submit)="loginUser($event)">
<div class="form-group">
<input type="text" class="form-control" id="username" value="USERNAME" placeholder="Username">
</div>
<div class="form-group">
<input class="form-control" id="password" type="password" value="PASSWORD" placeholder="Password">
</div>
<div class="clearfix">
<div class="btn-toolbar float-right m-t-1">
<button type="button" class="btn btn-default btn-sm">Create an Account</button>
<button type="submit" class="btn btn-inverse btn-sm" >Login</button>
</div>
</div>
<div class="row m-t-1">
<div class="col-md-6 order-md-2">
<div class="clearfix">
<div class="form-check abc-checkbox widget-login-info float-right pl-0">
<input class="form-check-input" type="checkbox" id="checkbox1" value="1">
<label class="form-check-label" for="checkbox1">Keep me signed in </label>
</div>
</div>
</div>
<div class="col-md-6 order-md-1">
<a class="mr-n-lg" href="#">Trouble with account?</a>
</div>
</div>
</form>