[[oktatas:web:angular:angular httpclient|< Angular httpclient]]
====== Angular HttpClient - Azonosítás ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2023
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* 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(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(url, '', httpOption)
.subscribe(res => {
console.log(res);
this.router.navigate(['login']);
})
}
Teljeskód:
* https://github.com/oktat/studal/blob/master/web/studal/src/app/shared/auth.service.ts