aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/services/users.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-ui/src/app/services/users.service.ts')
-rw-r--r--ufund-ui/src/app/services/users.service.ts52
1 files changed, 10 insertions, 42 deletions
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts
index 6671440..4080ebf 100644
--- a/ufund-ui/src/app/services/users.service.ts
+++ b/ufund-ui/src/app/services/users.service.ts
@@ -4,36 +4,27 @@ import {BehaviorSubject, catchError, firstValueFrom, Observable, of} from 'rxjs'
import {User} from '../models/User';
import { Need } from '../models/Need';
import { CupboardService } from './cupboard.service';
+import {AuthService} from './auth.service';
@Injectable({
providedIn: 'root'
})
export class UsersService {
- private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null);
- private apiKey: string = "";
private basket = new BehaviorSubject<Need[]>([]);
-
private url = "http://localhost:8080/users"
- private authUrl = "http://localhost:8080/auth"
- private httpOptions = () => ({
+
+ httpOptions = () => ({
headers: new HttpHeaders({
'Content-Type': 'application/json',
- "jelly-api-key": this.apiKey
+ "jelly-api-key": this.authService.getApiKey()
})
});
- 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,
private cupboardService: CupboardService,
+ private authService: AuthService
) {}
async createUser(username:string, password:string) {
@@ -46,38 +37,15 @@ export class UsersService {
updateUser(user: User): Observable<User> {
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
+ return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions())
}
deleteUser(id: number): Observable<boolean> {
return this.http.delete<boolean>(`${this.url}/${id}`, this.httpOptions())
}
- 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))
- }
-
refreshBasket() {
- let promiseArr = this.getCurrentUser()!.basket.map(async needID => {
+ let promiseArr = this.authService.getCurrentUser()!.basket.map(async needID => {
return await firstValueFrom(this.cupboardService.getNeed(needID));
})
Promise.all(promiseArr).then(r => this.basket.next(r));
@@ -86,10 +54,10 @@ export class UsersService {
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()!)
+ this.authService.getCurrentUser()!.basket = newArr.map(need => need.id);
+ this.updateUser(this.authService.getCurrentUser()!)
.pipe(
- catchError((err: any, ob) => {
+ catchError((err: any, _) => {
console.error(err);
return of();
})