OpenSlides/client/src/app/core/core-services/auth.service.ts

99 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
2019-03-07 10:47:03 +01:00
import { OperatorService, WhoAmI } from 'app/core/core-services/operator.service';
import { environment } from 'environments/environment';
import { OpenSlidesService } from './openslides.service';
2018-10-26 10:23:14 +02:00
import { HttpService } from './http.service';
import { DataStoreService } from './data-store.service';
/**
* Authenticates an OpenSlides user with username and password
*/
@Injectable({
providedIn: 'root'
})
2019-02-08 17:24:32 +01:00
export class AuthService {
/**
* Initializes the httpClient and the {@link OperatorService}.
*
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-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,
private DS: DataStoreService
2019-02-08 17:24:32 +01:00
) {}
/**
* Try to log in a user.
*
2019-03-04 11:45:15 +01:00
* Returns an observable with the correct login information or an error.
* 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.
*/
2019-03-07 10:47:03 +01:00
public async login(username: string, password: string, earlySuccessCallback: () => void): Promise<WhoAmI> {
const user = {
2018-06-30 16:56:18 +02:00
username: username,
password: password
};
2019-03-07 10:47:03 +01:00
const response = await this.http.post<WhoAmI>(environment.urlPrefix + '/users/login/', user);
earlySuccessCallback();
await this.operator.setWhoAmI(response);
await this.redirectUser(response.user_id);
2018-10-26 10:23:14 +02:00
return response;
}
2019-03-07 10:47:03 +01:00
/**
* Redirects the user to the page where he came from. Boots OpenSlides,
* if it wasn't done before.
*/
public async redirectUser(userId: number): Promise<void> {
if (!this.OpenSlides.booted) {
await this.OpenSlides.afterLoginBootup(userId);
}
let redirect = this.OpenSlides.redirectUrl ? this.OpenSlides.redirectUrl : '/';
const excludedUrls = ['login'];
if (excludedUrls.some(url => redirect.includes(url))) {
2019-03-07 10:47:03 +01:00
redirect = '/';
}
this.router.navigate([redirect]);
}
/**
* Login for guests.
*/
public async guestLogin(): Promise<void> {
this.redirectUser(null);
}
/**
* Logout function for both the client and the server.
*
2019-03-07 10:47:03 +01:00
* Will clear the datastore, update the current operator and
2019-03-04 11:45:15 +01:00
* send a `post`-request to `/apps/users/logout/'`. Restarts OpenSlides.
*/
2018-10-26 10:23:14 +02:00
public async logout(): Promise<void> {
2019-03-07 10:47:03 +01:00
let response = null;
2018-10-26 10:23:14 +02:00
try {
2019-03-07 10:47:03 +01:00
response = await this.http.post<WhoAmI>(environment.urlPrefix + '/users/logout/', {});
2018-10-26 10:23:14 +02:00
} catch (e) {
// We do nothing on failures. Reboot OpenSlides anyway.
}
this.router.navigate(['/']);
2019-03-07 10:47:03 +01:00
await this.DS.clear();
await this.operator.setWhoAmI(response);
await this.OpenSlides.reboot();
}
}