2018-06-19 16:55:50 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient, HttpResponse, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
|
|
|
|
import { Observable, of, throwError } from 'rxjs';
|
2018-06-28 17:11:04 +02:00
|
|
|
import { catchError, tap } from 'rxjs/operators';
|
2018-06-19 16:55:50 +02:00
|
|
|
|
2018-07-06 09:38:25 +02:00
|
|
|
import { OperatorService } from 'app/core/services/operator.service';
|
2018-06-19 16:55:50 +02:00
|
|
|
|
|
|
|
const httpOptions = {
|
|
|
|
withCredentials: true,
|
|
|
|
headers: new HttpHeaders({
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthService {
|
|
|
|
redirectUrl: string;
|
|
|
|
|
2018-07-06 09:38:25 +02:00
|
|
|
constructor(private http: HttpClient, private operator: OperatorService) {}
|
2018-06-19 16:55:50 +02:00
|
|
|
|
|
|
|
//loggins a users. expects a user model
|
2018-07-06 09:38:25 +02:00
|
|
|
login(username: string, password: string): Observable<any> {
|
2018-06-30 16:56:18 +02:00
|
|
|
const user: any = {
|
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
};
|
2018-07-03 11:52:16 +02:00
|
|
|
return this.http.post<any>('/users/login/', user, httpOptions).pipe(
|
2018-07-06 09:38:25 +02:00
|
|
|
tap(resp => this.operator.storeUser(resp.user)),
|
2018-06-19 16:55:50 +02:00
|
|
|
catchError(this.handleError())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//logout the user
|
|
|
|
//TODO not yet used
|
2018-07-06 09:38:25 +02:00
|
|
|
logout(): Observable<any> {
|
|
|
|
this.operator.clear();
|
|
|
|
return this.http.post<any>('/users/logout/', {}, httpOptions);
|
2018-06-19 16:55:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//very generic error handling function.
|
|
|
|
//implicitly returns an observable that will display an error message
|
|
|
|
private handleError<T>() {
|
|
|
|
return (error: any): Observable<T> => {
|
|
|
|
console.error(error);
|
|
|
|
return of(error);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|