]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts
import 15.2.9
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / auth.service.ts
1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3 import { Router } from '@angular/router';
4
5 import { Observable } from 'rxjs';
6 import { tap } from 'rxjs/operators';
7
8 import { Credentials } from '../models/credentials';
9 import { LoginResponse } from '../models/login-response';
10 import { AuthStorageService } from '../services/auth-storage.service';
11 import { ApiModule } from './api.module';
12
13 @Injectable({
14 providedIn: ApiModule
15 })
16 export class AuthService {
17 constructor(
18 private authStorageService: AuthStorageService,
19 private http: HttpClient,
20 private router: Router
21 ) {}
22
23 check(token: string) {
24 return this.http.post('api/auth/check', { token: token });
25 }
26
27 login(credentials: Credentials): Observable<LoginResponse> {
28 return this.http.post('api/auth', credentials).pipe(
29 tap((resp: LoginResponse) => {
30 this.authStorageService.set(
31 resp.username,
32 resp.permissions,
33 resp.sso,
34 resp.pwdExpirationDate,
35 resp.pwdUpdateRequired
36 );
37 })
38 );
39 }
40
41 logout(callback: Function = null) {
42 return this.http.post('api/auth/logout', null).subscribe((resp: any) => {
43 this.authStorageService.remove();
44 this.router.navigate(['/login'], { skipLocationChange: true });
45 if (callback) {
46 callback();
47 }
48 window.location.replace(resp.redirect_url);
49 });
50 }
51 }