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-07-12 14:11:31 +02:00
|
|
|
import { OpenSlidesComponent } from '../../openslides.component';
|
2018-08-23 16:49:51 +02:00
|
|
|
import { environment } from 'environments/environment';
|
2018-06-19 16:55:50 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Authenticates an OpenSlides user with username and password
|
|
|
|
*/
|
2018-06-19 16:55:50 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2018-07-12 14:11:31 +02:00
|
|
|
export class AuthService extends OpenSlidesComponent {
|
|
|
|
/**
|
|
|
|
* if the user tries to access a certain URL without being authenticated, the URL will be stored here
|
|
|
|
*/
|
2018-06-19 16:55:50 +02:00
|
|
|
redirectUrl: string;
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Initializes the httpClient and the {@link OperatorService}.
|
|
|
|
*
|
|
|
|
* Calls `super()` from the parent class.
|
|
|
|
* @param http HttpClient
|
|
|
|
* @param operator who is using OpenSlides
|
|
|
|
*/
|
|
|
|
constructor(private http: HttpClient, private operator: OperatorService) {
|
|
|
|
super();
|
|
|
|
}
|
2018-06-19 16:55:50 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Try to log in a user.
|
|
|
|
*
|
|
|
|
* Returns an observable 'user' with the correct login information or an error.
|
|
|
|
* The user will then be stored in the {@link OperatorService},
|
|
|
|
* errors will be forwarded to the parents error function.
|
|
|
|
*
|
|
|
|
* @param username
|
|
|
|
* @param password
|
|
|
|
*/
|
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-08-23 16:49:51 +02:00
|
|
|
return this.http.post<any>(environment.urlPrefix + '/users/login/', user).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())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Logout function for both the client and the server.
|
|
|
|
*
|
|
|
|
* Will clear the current {@link OperatorService} and
|
|
|
|
* send a `post`-requiest to `/users/logout/'`
|
|
|
|
*/
|
2018-06-19 16:55:50 +02:00
|
|
|
//logout the user
|
|
|
|
//TODO not yet used
|
2018-07-06 09:38:25 +02:00
|
|
|
logout(): Observable<any> {
|
|
|
|
this.operator.clear();
|
2018-08-23 16:49:51 +02:00
|
|
|
return this.http.post<any>(environment.urlPrefix + '/users/logout/', {});
|
2018-06-19 16:55:50 +02:00
|
|
|
}
|
|
|
|
}
|