Feature: Use Api generated by swagger #5

Merged
igorpropisnov merged 12 commits from feature/use-generated-api into main 2024-05-24 20:04:54 +02:00
2 changed files with 30 additions and 28 deletions
Showing only changes of commit ace60ab7b1 - Show all commits

View File

@ -6,7 +6,7 @@ import {
HttpStatus, HttpStatus,
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger'; import { ApiCreatedResponse, ApiHeader, ApiTags } from '@nestjs/swagger';
import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators'; import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators';
import { RefreshTokenGuard } from '../common/guards'; import { RefreshTokenGuard } from '../common/guards';
@ -54,6 +54,13 @@ export class AuthController {
return this.authService.logout(userId); return this.authService.logout(userId);
} }
@ApiHeader({
name: 'Authorization',
required: true,
schema: {
example: 'Bearer <refresh_token>',
},
})
@ApiCreatedResponse({ @ApiCreatedResponse({
description: 'User tokens refreshed successfully', description: 'User tokens refreshed successfully',
type: TokensDto, type: TokensDto,

View File

@ -1,9 +1,8 @@
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, tap } from 'rxjs'; import { BehaviorSubject, Observable, tap } from 'rxjs';
import { environment } from '../../../environments/environment';
import { AuthenticationApiService } from '../../api/api/authentication.api.service'; import { AuthenticationApiService } from '../../api/api/authentication.api.service';
import { LoginCredentials, Tokens } from '../types'; import { LoginCredentials, Tokens } from '../types';
@ -49,13 +48,28 @@ export class AuthService {
}); });
} }
// TODO Implement authControllerSignup public refreshToken(): Observable<Tokens> {
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 { public signout(): void {
this._access_token = null; this.authenticationApiService
this._refresh_token = null; .authControllerLogout()
this.localStorageService.removeItem('access_token'); .subscribe((response: boolean) => {
this.sessionStorageService.removeItem('refresh_token'); if (response) {
this._isAuthenticated$.next(false); 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 { public autoLogin(): void {
@ -73,25 +87,6 @@ export class AuthService {
} }
} }
public refreshToken(): Observable<Tokens> {
const headers = new HttpHeaders().set(
'Authorization',
'Bearer ' + this._refresh_token
);
return this.httpClient
.post<Tokens>(
environment.api.base + `${this._path}/refresh`,
{},
{ headers: headers }
)
.pipe(
tap((response: Tokens) => {
this.handleSuccess(response);
})
);
}
private handleSuccess(tokens: Tokens): void { private handleSuccess(tokens: Tokens): void {
this._access_token = tokens.access_token; this._access_token = tokens.access_token;
this._refresh_token = tokens.refresh_token; this._refresh_token = tokens.refresh_token;