import { Component, OnInit } from '@angular/core'; import { AuthService } from './auth.service'; import { CLIENT_ID } from '../../environments/environment'; const TOKEN_KEY = 'accessToken'; const CLIENT_ID_KEY = 'clientId'; @Component({ templateUrl: './auth.component.html', styleUrls: ['./auth.component.scss'] }) export class AuthComponent implements OnInit { public loggedIn = false; public user: string; constructor(private authService: AuthService) {} ngOnInit() { localStorage.setItem(CLIENT_ID_KEY, CLIENT_ID); this._getLoginData(); } private _getLoginData() { const token = localStorage.getItem(TOKEN_KEY); this.user = localStorage.getItem('email'); if (token) this.loggedIn = true; } public login(user: string, pass: string) { this.authService.login(user, pass).subscribe(() => { this._getLoginData(); }); } public logout() { this.user = null; this.loggedIn = false; localStorage.removeItem(TOKEN_KEY); localStorage.removeItem('email'); } }