Skip to content

Commit

Permalink
[src/app/api/auth/auth.service.ts] Implement logout properly (with HT…
Browse files Browse the repository at this point in the history
…TP POST also to delete tokens)
  • Loading branch information
SamuelMarks committed Nov 27, 2024
1 parent 7bff6e4 commit dda6329
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/app/api/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { Router } from '@angular/router';

import { Observable, of, throwError } from 'rxjs';
import { Observable, of, throwError, lastValueFrom } from 'rxjs';
import { catchError, map } from 'rxjs/operators';

import { AlertsService } from "../../alerts/alerts.service";
Expand Down Expand Up @@ -35,11 +35,15 @@ export class AuthService {
}

logout() {
localStorage.removeItem('access-token');
localStorage.removeItem('user');
this.router
.navigate(['/'], this.router.url === '/auth/logout' ? {} : { queryParams: { redirectUrl: this.router.url } })
.catch(console.error);
lastValueFrom(this.http.post('/secured/logout', null)).then(() => {
localStorage.removeItem('access-token');
localStorage.removeItem('user');
this.accessToken = undefined;
return this.router
.navigate(['/'], this.router.url === '/auth/logout' ? {} : {queryParams: {redirectUrl: this.router.url}})
.catch(console.error)
}
).then(console.error);
}

_login(loginResp: ILoginResp) {
Expand All @@ -57,7 +61,7 @@ export class AuthService {
public register(user: IAuthReq): Observable<HttpResponse<IAuthReq>> {
localStorage.setItem('user', user.username);
user.grant_type = 'password';
return this.http.post<IAuthReq>('/api/user', user, { observe: 'response' })
return this.http.post<IAuthReq>('/api/user', user, {observe: 'response'})
.pipe(
catchError((err: HttpErrorResponse) => {
if (err && err.error)
Expand All @@ -78,7 +82,7 @@ export class AuthService {
if (err && err.error && err.error.error_message && err.error.error_message === 'User not found')
return this.register(user)
.pipe(
map(o => Object.assign(o.body!, { access_token: o.headers.get('X-Access-Token') }) as IAuthReq | ILoginResp)
map(o => Object.assign(o.body!, {access_token: o.headers.get('X-Access-Token')}) as IAuthReq | ILoginResp)
);
// tslint:disable:no-unused-expression
if (typeof this.alertsService.add(err.error.error_message) !== "undefined")
Expand Down

0 comments on commit dda6329

Please sign in to comment.