Hi Leute,
ich habe in Angular 8 folgende Login Komponente (Ist nur ein Test, das Hardcodet Login kommt später weg), welche unter / angezeigt wird:
export class HomeComponent implements OnInit {
model: ILogin = { userid: "admin", password: "admin@123" }
loginForm: FormGroup;
message: string;
returnUrl: string;
constructor(
private formBuilder: FormBuilder,
private router: Router,
private authService: AuthService
) { }
ngOnInit() {
this.loginForm = this.formBuilder.group({
userid: ['', Validators.required],
password: ['', Validators.required]
});
this.returnUrl = '/dashboard';
this.authService.logout();
}
// convenience getter for easy access to form fields
get f() { return this.loginForm.controls; }
login() {
// stop here if form is invalid
if (this.loginForm.invalid) {
return;
}
else {
if (this.f.userid.value == this.model.userid && this.f.password.value == this.model.password) {
console.log("Login successful");
//this.authService.authLogin(this.model);
localStorage.setItem('isLoggedIn', "true");
localStorage.setItem('token', this.f.userid.value);
this.router.navigate([this.returnUrl]);
}
else {
this.message = "Please check your userid and password";
}
}
}
}
Nach Login soll folgende Komponente angezeigt werden:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
id: string;
constructor(private router: Router, private authService: AuthService) { }
ngOnInit() {
this.id = localStorage.getItem('token');
//console.log(this.id);
}
logout() {
console.log('logout');
this.authService.logout();
this.router.navigate(['/']);
}
}
Login Managment:
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, RouterStateSnapshot, CanActivate, Router} from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.isLoggedIn()) {
return true;
}
this.router.navigate(['/login']);
return false;
}
public isLoggedIn(): boolean {
let status = false;
if (localStorage.getItem('isLoggedIn') === 'true') {
status = true;
} else {
status = false;
}
return status;
}
}
Hier mein Routing:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {APP_BASE_HREF} from '@angular/common';
import {DashboardComponent} from "./dashboard/dashboard.component";
import {AuthGuard} from "./guards/auth.guard";
import {HomeComponent} from "./home/home.component";
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate : [AuthGuard] }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [{provide: APP_BASE_HREF, useValue : '/' }]
})
export class AppRoutingModule { }
Problem: Nach erfolgreichem Login lande ich zwar unter der URl /dashboard, aber leiderist die HomeKomponente mit dem Login dort noch sichtbar. Wie ändere ich das?