Feature: Use Api generated by swagger #5
|
@ -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 <refresh_token>',
|
||||
},
|
||||
})
|
||||
@ApiCreatedResponse({
|
||||
description: 'User tokens refreshed successfully',
|
||||
type: TokensDto,
|
||||
|
|
|
@ -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<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 {
|
||||
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<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 {
|
||||
this._access_token = tokens.access_token;
|
||||
this._refresh_token = tokens.refresh_token;
|
||||
|
|
Loading…
Reference in New Issue