diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-24 22:02:07 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-24 22:02:07 -0400 |
commit | a8175ba69669fddadfbe143e11972cc21821ed5f (patch) | |
tree | 3f66f5ebff82ab2433563294016eed3ce0e0f1be | |
parent | 12551843966b285ce3113fe0243626cc961a7715 (diff) | |
download | JellySolutions-a8175ba69669fddadfbe143e11972cc21821ed5f.tar.gz JellySolutions-a8175ba69669fddadfbe143e11972cc21821ed5f.tar.bz2 JellySolutions-a8175ba69669fddadfbe143e11972cc21821ed5f.zip |
Fix authentication bug
-rw-r--r-- | ufund-ui/src/app/services/users.service.ts | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index 8515073..6671440 100644 --- a/ufund-ui/src/app/services/users.service.ts +++ b/ufund-ui/src/app/services/users.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; -import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs'; +import {BehaviorSubject, catchError, firstValueFrom, Observable, of} from 'rxjs'; import {User} from '../models/User'; import { Need } from '../models/Need'; import { CupboardService } from './cupboard.service'; @@ -16,20 +16,20 @@ export class UsersService { private url = "http://localhost:8080/users" private authUrl = "http://localhost:8080/auth" - private httpOptions = { + private httpOptions = () => ({ headers: new HttpHeaders({ 'Content-Type': 'application/json', "jelly-api-key": this.apiKey }) - }; - private httpOptions2 = { + }); + private httpOptions2 = () => ({ headers: new HttpHeaders({ 'Content-Type': 'application/json', "jelly-api-key": this.apiKey }), responseType: "text" as "json" // don't ask me how or why this works, bc i have no clue... // see the relevant angular bug report https://github.com/angular/angular/issues/18586 - }; + }); constructor( private http: HttpClient, @@ -37,19 +37,20 @@ export class UsersService { ) {} async createUser(username:string, password:string) { - await firstValueFrom(this.http.post<User>(this.url, {username: username, password: password}, this.httpOptions)) + await firstValueFrom(this.http.post<User>(this.url, {username: username, password: password}, this.httpOptions())) } getUser(id: string): Observable<User> { - return this.http.get<User>(`${this.url}/${id}`, this.httpOptions) + return this.http.get<User>(`${this.url}/${id}`, this.httpOptions()) } updateUser(user: User): Observable<User> { - return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions2) // This line is causing issues as the key is not properly being passed + console.log(`${this.url}/${user.username}`, user, this.httpOptions) + return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions2()) // This line is causing issues as the key is not properly being passed } deleteUser(id: number): Observable<boolean> { - return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions) + return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions()) } getCurrentUserSubject() { @@ -61,7 +62,7 @@ export class UsersService { } async login(username: string, password: string) { - let res = this.http.post<string>(this.authUrl, {username: username, password: password}, this.httpOptions2); + let res = this.http.post<string>(this.authUrl, {username: username, password: password}, this.httpOptions2()); this.apiKey = await firstValueFrom(res); console.log("apikey: "+this.apiKey) let res2 = this.http.get<User>(`${this.url}/${username}`, { @@ -81,16 +82,20 @@ export class UsersService { }) Promise.all(promiseArr).then(r => this.basket.next(r)); } - + removeNeed(id: number) { let newArr = this.basket.getValue().filter(v => v.id != id); this.basket.next(newArr); this.getCurrentUser()!.basket = newArr.map(need => need.id); - this.updateUser(this.getCurrentUser()!).subscribe(() => { + this.updateUser(this.getCurrentUser()!) + .pipe( + catchError((err: any, ob) => { + console.error(err); + return of(); + }) + ) + .subscribe(() => { this.refreshBasket(); - error: (err: any) => { - console.error(err); - } }); } |