diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts new file mode 100644 index 000000000..8a2917992 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.ts @@ -0,0 +1,53 @@ +import { HttpClient } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { ActivatedRoute, Router } from '@angular/router'; + +import * as _ from 'lodash'; +import { Observable } from 'rxjs'; +import { tap } from 'rxjs/operators'; + +import { Credentials } from '../models/credentials'; +import { LoginResponse } from '../models/login-response'; +import { AuthStorageService } from '../services/auth-storage.service'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthService { + constructor( + private authStorageService: AuthStorageService, + private http: HttpClient, + private router: Router, + private route: ActivatedRoute + ) {} + + check(token: string) { + return this.http.post('api/auth/check', { token: token }); + } + + login(credentials: Credentials): Observable<LoginResponse> { + return this.http.post('api/auth', credentials).pipe( + tap((resp: LoginResponse) => { + this.authStorageService.set( + resp.username, + resp.permissions, + resp.sso, + resp.pwdExpirationDate, + resp.pwdUpdateRequired + ); + }) + ); + } + + logout(callback: Function = null) { + return this.http.post('api/auth/logout', null).subscribe((resp: any) => { + this.authStorageService.remove(); + const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/login'); + this.router.navigate([url], { skipLocationChange: true }); + if (callback) { + callback(); + } + window.location.replace(resp.redirect_url); + }); + } +} |