aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/services/auth.service.ts
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-03-27 18:55:39 -0400
committersowgro <tpoke.ferrari@gmail.com>2025-03-27 18:55:39 -0400
commitd6f4aad157baad7d9b25d56ce056b80805e88f5f (patch)
tree1b4a3df94389afb1637ea59fe47ba3a327c89561 /ufund-ui/src/app/services/auth.service.ts
parent96f833352eff7b9428daf2add988ecd0a2b41d92 (diff)
parentddbd1cc688aa98fb275ad72a750fbaaf53e6c0ae (diff)
downloadJellySolutions-d6f4aad157baad7d9b25d56ce056b80805e88f5f.tar.gz
JellySolutions-d6f4aad157baad7d9b25d56ce056b80805e88f5f.tar.bz2
JellySolutions-d6f4aad157baad7d9b25d56ce056b80805e88f5f.zip
Merge branch 'main' into css
Diffstat (limited to 'ufund-ui/src/app/services/auth.service.ts')
-rw-r--r--ufund-ui/src/app/services/auth.service.ts57
1 files changed, 57 insertions, 0 deletions
diff --git a/ufund-ui/src/app/services/auth.service.ts b/ufund-ui/src/app/services/auth.service.ts
new file mode 100644
index 0000000..6bc7145
--- /dev/null
+++ b/ufund-ui/src/app/services/auth.service.ts
@@ -0,0 +1,57 @@
+import {Injectable} from '@angular/core';
+import {BehaviorSubject, firstValueFrom} from 'rxjs';
+import {User} from '../models/User';
+import {HttpClient, HttpHeaders} from '@angular/common/http';
+
+@Injectable({
+ providedIn: 'root'
+})
+export class AuthService {
+
+ private authUrl = "http://localhost:8080/auth"
+ private userUrl = "http://localhost:8080/users"
+
+ private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null);
+ private apiKey: string = "";
+
+ 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
+ ) {}
+
+ 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.userUrl}/${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))
+ }
+
+ getCurrentUserSubject() {
+ return this.currentUser;
+ }
+
+ getCurrentUser() {
+ return this.currentUser.getValue()
+ }
+
+ getApiKey() {
+ return this.apiKey;
+ }
+
+}