diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-17 17:17:06 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-17 17:17:06 -0400 |
commit | baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754 (patch) | |
tree | e9213224b8f1b35b860f016a6a3d1318def8aae2 /ufund-ui/src/app/services/users.service.ts | |
parent | bf33fa3ca9f29b1e75cc077ae2eaaf4f5725e4b3 (diff) | |
parent | d737551fba5617843f3014be6994490dd4328183 (diff) | |
download | JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.gz JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.bz2 JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.zip |
Merge remote-tracking branch 'origin/main' into cupboard-component
# Conflicts:
# ufund-api/data/cupboard.json
# ufund-ui/src/app/app.module.ts
Diffstat (limited to 'ufund-ui/src/app/services/users.service.ts')
-rw-r--r-- | ufund-ui/src/app/services/users.service.ts | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index 571c004..c570ccf 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 {Observable} from 'rxjs'; +import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs'; import {User} from '../models/User'; @Injectable({ @@ -8,22 +8,35 @@ import {User} from '../models/User'; }) export class UsersService { - private currentUserID? : number + private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null); + private apiKey: string = ""; private url = "http://localhost:8080/users" + private authUrl = "http://localhost:8080/auth" private httpOptions = { - headers: new HttpHeaders({'Content-Type': 'application/json'}) + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + "jelly-api-key": this.apiKey + }) + }; + 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 ) {} - createUser(data: User): Observable<User> { - return this.http.post<User>(this.url, data, this.httpOptions) + async createUser(username:string, password:string) { + await firstValueFrom(this.http.post<User>(this.url, {username: username, password: password}, this.httpOptions)) } - getUser(id: number): Observable<User> { + getUser(id: string): Observable<User> { return this.http.get<User>(`${this.url}/${id}`, this.httpOptions) } @@ -35,7 +48,26 @@ export class UsersService { return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions) } - getCurrentUser(): Observable<User> | undefined { - return this.currentUserID ? this.getUser(this.currentUserID) : undefined + getCurrentUserSubject() { + return this.currentUser; + } + + getCurrentUser() { + return this.currentUser.getValue() + } + + async login(username: string, password: string) { + 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}`, { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + "jelly-api-key": this.apiKey + }) + }) + let currentU = await firstValueFrom(res2); + this.currentUser.next(currentU); + // this.currentUser.subscribe(r => console.log("currentUser: "+r.username)) } } |