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-08-28 11:07:10 +02:00
|
|
|
import { User } from '../../shared/models/users/user';
|
|
|
|
import { OpenSlidesService } from './openslides.service';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The data returned by a post request to the login route.
|
|
|
|
*/
|
|
|
|
interface LoginResponse {
|
|
|
|
user_id: number;
|
|
|
|
user: User;
|
|
|
|
}
|
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 {
|
|
|
|
/**
|
|
|
|
* Initializes the httpClient and the {@link OperatorService}.
|
|
|
|
*
|
|
|
|
* Calls `super()` from the parent class.
|
|
|
|
* @param http HttpClient
|
|
|
|
* @param operator who is using OpenSlides
|
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
constructor(private http: HttpClient, private operator: OperatorService, private OpenSlides: OpenSlidesService) {
|
2018-07-12 14:11:31 +02:00
|
|
|
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-08-28 11:07:10 +02:00
|
|
|
public login(username: string, password: string): Observable<LoginResponse> {
|
|
|
|
const user = {
|
2018-06-30 16:56:18 +02:00
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
};
|
2018-08-28 11:07:10 +02:00
|
|
|
return this.http.post<LoginResponse>(environment.urlPrefix + '/users/login/', user).pipe(
|
|
|
|
tap((response: LoginResponse) => {
|
|
|
|
this.operator.user = new User().deserialize(response.user);
|
|
|
|
}),
|
2018-06-19 16:55:50 +02:00
|
|
|
catchError(this.handleError())
|
2018-08-28 11:07:10 +02:00
|
|
|
) as Observable<LoginResponse>;
|
2018-06-19 16:55:50 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Logout function for both the client and the server.
|
|
|
|
*
|
|
|
|
* Will clear the current {@link OperatorService} and
|
2018-08-28 11:07:10 +02:00
|
|
|
* send a `post`-request to `/apps/users/logout/'`
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
public logout(): void {
|
|
|
|
this.operator.user = null;
|
|
|
|
this.http.post<any>(environment.urlPrefix + '/users/logout/', {}).subscribe(() => {
|
|
|
|
this.OpenSlides.reboot();
|
|
|
|
});
|
2018-06-19 16:55:50 +02:00
|
|
|
}
|
|
|
|
}
|