2018-06-19 16:55:50 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
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';
|
2018-10-26 10:23:14 +02:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { HttpService } from './http.service';
|
2018-08-28 11:07:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2018-10-26 10:23:14 +02:00
|
|
|
* @param http HttpService to send requests to the server
|
|
|
|
* @param operator Who is using OpenSlides
|
|
|
|
* @param OpenSlides The openslides service
|
|
|
|
* @param router To navigate
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public constructor(
|
2018-10-26 10:23:14 +02:00
|
|
|
private http: HttpService,
|
2018-08-29 13:21:25 +02:00
|
|
|
private operator: OperatorService,
|
2018-10-26 10:23:14 +02:00
|
|
|
private OpenSlides: OpenSlidesService,
|
|
|
|
private router: Router
|
2018-08-29 13:21:25 +02:00
|
|
|
) {
|
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-10-26 10:23:14 +02:00
|
|
|
* @returns The login response.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-10-26 10:23:14 +02:00
|
|
|
public async login(username: string, password: string): Promise<LoginResponse> {
|
2018-08-28 11:07:10 +02:00
|
|
|
const user = {
|
2018-06-30 16:56:18 +02:00
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
};
|
2018-10-26 10:23:14 +02:00
|
|
|
const response = await this.http.post<LoginResponse>(environment.urlPrefix + '/users/login/', user);
|
|
|
|
this.operator.user = new User(response.user);
|
|
|
|
return response;
|
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-10-26 10:23:14 +02:00
|
|
|
public async logout(): Promise<void> {
|
2018-08-28 11:07:10 +02:00
|
|
|
this.operator.user = null;
|
2018-10-26 10:23:14 +02:00
|
|
|
try {
|
|
|
|
await this.http.post(environment.urlPrefix + '/users/logout/', {});
|
|
|
|
} catch (e) {
|
|
|
|
// We do nothing on failures. Reboot OpenSlides anyway.
|
|
|
|
}
|
|
|
|
this.router.navigate(['/']);
|
|
|
|
this.OpenSlides.reboot();
|
2018-06-19 16:55:50 +02:00
|
|
|
}
|
|
|
|
}
|