oktatas:web:angular:angular_httpclient:azonositas
Angular HttpClient - Azonosítás
- Szerző: Sallai András
- Copyright © Sallai András, 2023
- Web: https://szit.hu
auth.service.ts
login(user: string, pass: string) { let authData = { name: user, password: pass } let data = JSON.stringify(authData); let headerObj = new HttpHeaders({ 'Content-Type': 'application/json', }); const httpOption = { headers: headerObj }; let endpoint = 'login'; let url = this.host + endpoint; return this.http.post<any>(url, data, httpOption); }
logout() { if (localStorage.getItem('currentUser') === null) { return; } let data:any = localStorage.getItem('currentUser'); localStorage.removeItem('currentUser'); localStorage.removeItem('selectedClassgroup'); let currentUser = JSON.parse(data); let token = currentUser.token; let headerObj = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }); const httpOption = { headers: headerObj }; let endpoint = 'logout'; let url = this.host + endpoint; return this.http.post<any>(url, '', httpOption) .subscribe(res => { console.log(res); this.router.navigate(['login']); }) }
Teljeskód:
oktatas/web/angular/angular_httpclient/azonositas.txt · Utolsó módosítás: 2023/01/06 22:10 szerkesztette: admin