diff --git a/src/app/interceptors/auth-interceptor.ts b/src/app/interceptors/auth-interceptor.ts new file mode 100644 index 0000000..d905f3f --- /dev/null +++ b/src/app/interceptors/auth-interceptor.ts @@ -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); +}; diff --git a/src/app/services/api/api/refresh.service.ts b/src/app/services/api/api/refresh.service.ts new file mode 100644 index 0000000..5330ddf --- /dev/null +++ b/src/app/services/api/api/refresh.service.ts @@ -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; + public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public refreshTokenEndpoint(refreshTokenDto: RefreshTokenDto, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { + 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('post', `${basePath}${localVarPath}`, + { + context: localVarHttpContext, + body: refreshTokenDto, + responseType: responseType_, + ...(withCredentials ? { withCredentials } : {}), + headers: localVarHeaders, + observe: observe, + ...(localVarTransferCache !== undefined ? { transferCache: localVarTransferCache } : {}), + reportProgress: reportProgress + } + ); + } + +} diff --git a/src/app/services/api/model/get-refresh-dto.ts b/src/app/services/api/model/get-refresh-dto.ts new file mode 100644 index 0000000..923da42 --- /dev/null +++ b/src/app/services/api/model/get-refresh-dto.ts @@ -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; +} + diff --git a/src/app/services/api/model/refresh-token-dto.ts b/src/app/services/api/model/refresh-token-dto.ts new file mode 100644 index 0000000..8de6053 --- /dev/null +++ b/src/app/services/api/model/refresh-token-dto.ts @@ -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; +} + diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts new file mode 100644 index 0000000..8f6f852 --- /dev/null +++ b/src/app/services/auth.service.ts @@ -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'); + } +}