From 95a593bdb6ca6108f39639daa81bc9b79655fe80 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 4 Aug 2024 23:15:38 +0300 Subject: [PATCH] feat: add auth api --- src/api/v1/authApiService.ts | 54 +++++++++++++++++++++++++++++++++ src/shared/structs/AuthRoles.ts | 3 ++ 2 files changed, 57 insertions(+) create mode 100644 src/api/v1/authApiService.ts create mode 100644 src/shared/structs/AuthRoles.ts diff --git a/src/api/v1/authApiService.ts b/src/api/v1/authApiService.ts new file mode 100644 index 0000000..8266118 --- /dev/null +++ b/src/api/v1/authApiService.ts @@ -0,0 +1,54 @@ +import {Injectable} from "@angular/core"; +import ApiService, {AvailableVersion} from "@api/api.service"; +import {LoginRequest} from "@api/v1/loginRequest"; +import {TokenResponse} from "@api/v1/tokenResponse"; +import {catchError, of, tap} from "rxjs"; +import {AuthRoles} from "@model/AuthRoles"; +import {AuthService, AvailableAuthenticationProvider} from "@service/auth.service"; + +@Injectable() +export default class AuthApiService extends ApiService { + public readonly basePath = 'Auth/'; + public readonly version = AvailableVersion.v1; + + public login(login: LoginRequest) { + return this.createRequestBuilder() + .setEndpoint('Login') + .setData(login) + .build() + .post() + .pipe( + tap(response => { + AuthService.setToken(response, AvailableAuthenticationProvider.Bearer, this.createRequestBuilder().setEndpoint('ReLogin').build().combinedUrl); + this.tokenRefreshService.startTokenRefresh(response.expiresIn); + }) + ); + } + + public logout() { + return this.createRequestBuilder() + .setWithCredentials() + .setEndpoint('Logout') + .build() + .addAuth() + .get() + .pipe( + tap(_ => { + localStorage.removeItem(ApiService.tokenKey); + }) + ); + } + + public getRole(isSilence: boolean = true) { + return this.createRequestBuilder() + .setSilenceMode(isSilence) + .build() + .addAuth() + .get('GetRole') + .pipe( + catchError(_ => { + return of(null); + }) + ); + } +} diff --git a/src/shared/structs/AuthRoles.ts b/src/shared/structs/AuthRoles.ts new file mode 100644 index 0000000..efd9743 --- /dev/null +++ b/src/shared/structs/AuthRoles.ts @@ -0,0 +1,3 @@ +export enum AuthRoles { + Admin +}