Here is my angular code for routing in app.module.ts file.
const appRoutes: Routes = [
{path: '', component: LoginformComponent},
{path: 'dashboard', component: DashboardComponent, canActivate: [AuthenticationGuard]},
{path: 'user', children: [
{path: '', component: UserComponent},
{path: ':id', component: UserdetailComponent}
], canActivate: [AuthenticationGuard]
},
{path: '**', component: NotfoundComponent}
];
And following code represent the guard file.
export class AuthenticationGuard implements CanActivate {
constructor(private user: UserService, private router: Router) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (! this.user.getUserLoggedIn()) {
this.router.navigate(['/']);
}
return this.user.getUserLoggedIn();
}
}
Guard works fine when Im logged in as a user. But in case of refreshing it log me out. I want a method to keep me logged in the system even after refreshing the page?
This is the user service.ts file,
@Injectable()
export class UserService {
private isUserLoggedIn: boolean;
private username: string;
constructor() {
this.isUserLoggedIn = false;
}
setUserLoggedIn(username) {
this.isUserLoggedIn = true;
this.username = username;
}
getUserLoggedIn() {
return this.isUserLoggedIn;
}
getUserName() {
return this.username;
}
}
Can someone help me..........