Created
November 9, 2020 21:56
-
-
Save blogcacanid/b2c1bbcc8961ff55bebcd972cf3fbd8b to your computer and use it in GitHub Desktop.
login.component.ts Authentication JWT Angular 9 Lumen 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { AuthService } from '../_services/auth.service'; | |
import { TokenStorageService } from '../_services/token-storage.service'; | |
@Component({ | |
selector: 'app-login', | |
templateUrl: './login.component.html', | |
styleUrls: ['./login.component.css'] | |
}) | |
export class LoginComponent implements OnInit { | |
form: any = {}; | |
isLoggedIn = false; | |
isLoginFailed = false; | |
errorMessage = ''; | |
constructor(private authService: AuthService, private tokenStorage: TokenStorageService) { } | |
ngOnInit(): void { | |
if (this.tokenStorage.getToken()) { | |
this.isLoggedIn = true; | |
} | |
} | |
onSubmit(): void { | |
this.authService.login(this.form).subscribe( | |
data => { | |
this.tokenStorage.saveToken(data.accessToken); | |
this.tokenStorage.saveUser(data); | |
this.isLoginFailed = false; | |
this.isLoggedIn = true; | |
// this.reloadPage(); | |
window.location.pathname = '/profile'; | |
}, | |
err => { | |
this.errorMessage = err.error.message; | |
this.isLoginFailed = true; | |
} | |
); | |
} | |
reloadPage(): void { | |
window.location.reload(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment