diff --git a/backend/src/modules/auth-module/controller/auth.controller.ts b/backend/src/modules/auth-module/controller/auth.controller.ts index 21091f7..721aa52 100644 --- a/backend/src/modules/auth-module/controller/auth.controller.ts +++ b/backend/src/modules/auth-module/controller/auth.controller.ts @@ -6,7 +6,7 @@ import { HttpStatus, UseGuards, } from '@nestjs/common'; -import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger'; +import { ApiCreatedResponse, ApiHeader, ApiTags } from '@nestjs/swagger'; import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators'; import { RefreshTokenGuard } from '../common/guards'; @@ -54,6 +54,13 @@ export class AuthController { return this.authService.logout(userId); } + @ApiHeader({ + name: 'Authorization', + required: true, + schema: { + example: 'Bearer ', + }, + }) @ApiCreatedResponse({ description: 'User tokens refreshed successfully', type: TokensDto, diff --git a/frontend/src/app/shared/service/auth.service.ts b/frontend/src/app/shared/service/auth.service.ts index 8fc40eb..f1d97e0 100644 --- a/frontend/src/app/shared/service/auth.service.ts +++ b/frontend/src/app/shared/service/auth.service.ts @@ -1,9 +1,8 @@ -import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable, tap } from 'rxjs'; -import { environment } from '../../../environments/environment'; import { AuthenticationApiService } from '../../api/api/authentication.api.service'; import { LoginCredentials, Tokens } from '../types'; @@ -49,13 +48,28 @@ export class AuthService { }); } - // TODO Implement authControllerSignup + public refreshToken(): Observable { + if (this._refresh_token) { + return this.authenticationApiService + .authControllerRefresh(this._refresh_token) + .pipe(tap((response: Tokens) => this.handleSuccess(response))); + } else { + throw new Error('Refresh token is missing'); + } + } + public signout(): void { - this._access_token = null; - this._refresh_token = null; - this.localStorageService.removeItem('access_token'); - this.sessionStorageService.removeItem('refresh_token'); - this._isAuthenticated$.next(false); + this.authenticationApiService + .authControllerLogout() + .subscribe((response: boolean) => { + if (response) { + this._access_token = null; + this._refresh_token = null; + this.localStorageService.removeItem('access_token'); + this.sessionStorageService.removeItem('refresh_token'); + this._isAuthenticated$.next(false); + } + }); } public autoLogin(): void { @@ -73,25 +87,6 @@ export class AuthService { } } - public refreshToken(): Observable { - const headers = new HttpHeaders().set( - 'Authorization', - 'Bearer ' + this._refresh_token - ); - - return this.httpClient - .post( - environment.api.base + `${this._path}/refresh`, - {}, - { headers: headers } - ) - .pipe( - tap((response: Tokens) => { - this.handleSuccess(response); - }) - ); - } - private handleSuccess(tokens: Tokens): void { this._access_token = tokens.access_token; this._refresh_token = tokens.refresh_token;