Is there a way to implement a life cycle hook that will be triggered whenever my component is active on the main page, specifically on the login page?
When my component is on the main page and active, I need to check if there is a login token from a previous session. Depending on the result, I want to redirect to a different page. Is there an `ngOnActive' hook available or how can I create it?
Alternatively, is there any other method to achieve this functionality?
Here is my current code:
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import {LoginService} from './login.service';
import {TokenService} from '../data/token.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [LoginService]
})
export class LoginComponent implements OnInit {
constructor(private loginService: LoginService, private tokenService: TokenService, private router: Router) { }
ngOnInit() {
if (this.tokenService.getToken() !== '') {
console.log(this.tokenService.tk);
this.router.navigate(['/admin']);
}
}
onSubmit(form: NgForm) {
this.loginService.login(form.value.email, form.value.password);
}
}