Make WS work with Auth
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import { Injectable, Injector } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import { RxStompService } from '../../shared/websocket/rx-stomp.service';
|
||||
import { createRxStompConfig } from '../../shared/websocket/rx-stomp.config';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
private apiUrl = 'http://localhost:7050/api/v1/auth/login';
|
||||
private rxStompService?: RxStompService;
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
constructor(private http: HttpClient, private injector: Injector) {}
|
||||
|
||||
login(credentials: { username: string; password: string }): Observable<any> {
|
||||
return this.http.post(this.apiUrl, credentials);
|
||||
return this.http.post<{ token: string }>(this.apiUrl, credentials).pipe(
|
||||
tap((response) => {
|
||||
if (response.token) {
|
||||
this.saveToken(response.token);
|
||||
this.startWebSocket();
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
saveToken(token: string): void {
|
||||
@@ -24,5 +34,22 @@ export class AuthService {
|
||||
|
||||
logout(): void {
|
||||
localStorage.removeItem('token');
|
||||
this.getRxStompService().deactivate();
|
||||
}
|
||||
|
||||
private startWebSocket(): void {
|
||||
const token = this.getToken();
|
||||
if (token) {
|
||||
const rxStompService = this.getRxStompService();
|
||||
rxStompService.configure(createRxStompConfig(this));
|
||||
rxStompService.activate();
|
||||
}
|
||||
}
|
||||
|
||||
private getRxStompService(): RxStompService {
|
||||
if (!this.rxStompService) {
|
||||
this.rxStompService = this.injector.get(RxStompService);
|
||||
}
|
||||
return this.rxStompService;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user