feat: add auth api
All checks were successful
Build and Deploy Angular App / build (push) Successful in 55s
All checks were successful
Build and Deploy Angular App / build (push) Successful in 55s
This commit is contained in:
parent
f4b25f428d
commit
95a593bdb6
54
src/api/v1/authApiService.ts
Normal file
54
src/api/v1/authApiService.ts
Normal 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);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
3
src/shared/structs/AuthRoles.ts
Normal file
3
src/shared/structs/AuthRoles.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export enum AuthRoles {
|
||||
Admin
|
||||
}
|
Loading…
Reference in New Issue
Block a user