added authservice and interceptor file for user gestion

This commit is contained in:
2025-12-08 12:02:14 +01:00
parent 0be745dc61
commit 3bc368ecb3
5 changed files with 179 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AuthService } from '../services/auth.service';
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const authService = inject(AuthService);
const token = authService.getToken();
if (token) {
req = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
}
return next(req);
};

View File

@@ -0,0 +1,104 @@
/**
* ApiEfCoreLibrary
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
/* tslint:disable:no-unused-variable member-ordering */
import { Inject, Injectable, Optional } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams,
HttpResponse, HttpEvent, HttpParameterCodec, HttpContext
} from '@angular/common/http';
import { CustomHttpParameterCodec } from '../encoder';
import { Observable } from 'rxjs';
// @ts-ignore
import { GetRefreshDto } from '../model/get-refresh-dto';
// @ts-ignore
import { RefreshTokenDto } from '../model/refresh-token-dto';
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { BaseService } from '../api.base.service';
@Injectable({
providedIn: 'root'
})
export class RefreshService extends BaseService {
constructor(protected httpClient: HttpClient, @Optional() @Inject(BASE_PATH) basePath: string|string[], @Optional() configuration?: Configuration) {
super(basePath, configuration);
}
/**
* @endpoint post /API/refresh
* @param refreshTokenDto
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<GetRefreshDto>;
public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpResponse<GetRefreshDto>>;
public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpEvent<GetRefreshDto>>;
public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<any> {
if (refreshTokenDto === null || refreshTokenDto === undefined) {
throw new Error('Required parameter refreshTokenDto was null or undefined when calling refreshTokenEndpoint.');
}
let localVarHeaders = this.defaultHeaders;
const localVarHttpHeaderAcceptSelected: string | undefined = options?.httpHeaderAccept ?? this.configuration.selectHeaderAccept([
'application/json'
]);
if (localVarHttpHeaderAcceptSelected !== undefined) {
localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected);
}
const localVarHttpContext: HttpContext = options?.context ?? new HttpContext();
const localVarTransferCache: boolean = options?.transferCache ?? true;
// to determine the Content-Type header
const consumes: string[] = [
'application/json'
];
const httpContentTypeSelected: string | undefined = this.configuration.selectHeaderContentType(consumes);
if (httpContentTypeSelected !== undefined) {
localVarHeaders = localVarHeaders.set('Content-Type', httpContentTypeSelected);
}
let responseType_: 'text' | 'json' | 'blob' = 'json';
if (localVarHttpHeaderAcceptSelected) {
if (localVarHttpHeaderAcceptSelected.startsWith('text')) {
responseType_ = 'text';
} else if (this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)) {
responseType_ = 'json';
} else {
responseType_ = 'blob';
}
}
let localVarPath = `/API/refresh`;
const { basePath, withCredentials } = this.configuration;
return this.httpClient.request<GetRefreshDto>('post', `${basePath}${localVarPath}`,
{
context: localVarHttpContext,
body: refreshTokenDto,
responseType: <any>responseType_,
...(withCredentials ? { withCredentials } : {}),
headers: localVarHeaders,
observe: observe,
...(localVarTransferCache !== undefined ? { transferCache: localVarTransferCache } : {}),
reportProgress: reportProgress
}
);
}
}

View File

@@ -0,0 +1,15 @@
/**
* ApiEfCoreLibrary
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
export interface GetRefreshDto {
token?: string | null;
}

View File

@@ -0,0 +1,15 @@
/**
* ApiEfCoreLibrary
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
export interface RefreshTokenDto {
token?: string | null;
}

View File

@@ -0,0 +1,29 @@
import { inject, Injectable } from '@angular/core';
import { LoginService } from './api';
import { firstValueFrom } from 'rxjs';
import { NzNotificationService } from 'ng-zorro-antd/notification';
@Injectable({
providedIn: 'root',
})
export class AuthService {
private loginService = inject(LoginService);
private notificationService = inject(NzNotificationService);
async connectUser(username: string, password: string){
try {
const loginDto = { username, password };
const res = await firstValueFrom(this.loginService.userLoginEndpoint(loginDto));
localStorage.setItem('jwt', res.token);
} catch (e) {
this.notificationService.error(
'Erreur',
'Identifiant invalide'
);
}
}
getToken(): string | null {
return localStorage.getItem('jwt');
}
}