feat: add auth api
All checks were successful
Build and Deploy Angular App / build (push) Successful in 55s

This commit is contained in:
Polianin Nikita 2024-08-04 23:15:38 +03:00
parent f4b25f428d
commit 95a593bdb6
2 changed files with 57 additions and 0 deletions

View File

@ -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<ApiService>()
.post<TokenResponse>()
.pipe(
tap(response => {
AuthService.setToken(response, AvailableAuthenticationProvider.Bearer, this.createRequestBuilder().setEndpoint('ReLogin').build<AuthApiService>().combinedUrl);
this.tokenRefreshService.startTokenRefresh(response.expiresIn);
})
);
}
public logout() {
return this.createRequestBuilder()
.setWithCredentials()
.setEndpoint('Logout')
.build<ApiService>()
.addAuth()
.get()
.pipe(
tap(_ => {
localStorage.removeItem(ApiService.tokenKey);
})
);
}
public getRole(isSilence: boolean = true) {
return this.createRequestBuilder()
.setSilenceMode(isSilence)
.build<ApiService>()
.addAuth()
.get<AuthRoles>('GetRole')
.pipe(
catchError(_ => {
return of(null);
})
);
}
}

View File

@ -0,0 +1,3 @@
export enum AuthRoles {
Admin
}