From cb3b7710b9e32df408b3a38383aca049fa98214e Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 24 Mar 2025 21:17:33 -0400 Subject: Fixed various bugs and began fixing auth system. Also started implementing checkout method in cupboardService --- ufund-ui/src/app/services/users.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-ui/src/app/services') diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index dba8185..6709192 100644 --- a/ufund-ui/src/app/services/users.service.ts +++ b/ufund-ui/src/app/services/users.service.ts @@ -45,7 +45,7 @@ export class UsersService { } updateUser(user: User): Observable { - return this.http.put(`${this.url}/${user.username}`,user, this.httpOptions2) + return this.http.put(`${this.url}/${user.username}`, user, this.httpOptions2) } deleteUser(id: number): Observable { -- cgit v1.2.3 From 12551843966b285ce3113fe0243626cc961a7715 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 24 Mar 2025 21:18:25 -0400 Subject: Added comment --- ufund-ui/src/app/services/users.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-ui/src/app/services') diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index 6709192..8515073 100644 --- a/ufund-ui/src/app/services/users.service.ts +++ b/ufund-ui/src/app/services/users.service.ts @@ -45,7 +45,7 @@ export class UsersService { } updateUser(user: User): Observable { - return this.http.put(`${this.url}/${user.username}`, user, this.httpOptions2) + return this.http.put(`${this.url}/${user.username}`, user, this.httpOptions2) // This line is causing issues as the key is not properly being passed } deleteUser(id: number): Observable { -- cgit v1.2.3 From a8175ba69669fddadfbe143e11972cc21821ed5f Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 24 Mar 2025 22:02:07 -0400 Subject: Fix authentication bug --- ufund-ui/src/app/services/users.service.ts | 35 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'ufund-ui/src/app/services') 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(this.url, {username: username, password: password}, this.httpOptions)) + await firstValueFrom(this.http.post(this.url, {username: username, password: password}, this.httpOptions())) } getUser(id: string): Observable { - return this.http.get(`${this.url}/${id}`, this.httpOptions) + return this.http.get(`${this.url}/${id}`, this.httpOptions()) } updateUser(user: User): Observable { - return this.http.put(`${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(`${this.url}/${user.username}`, user, this.httpOptions2()) // This line is causing issues as the key is not properly being passed } deleteUser(id: number): Observable { - return this.http.delete(`${this.url}/${id}`, this.httpOptions) + return this.http.delete(`${this.url}/${id}`, this.httpOptions()) } getCurrentUserSubject() { @@ -61,7 +62,7 @@ export class UsersService { } async login(username: string, password: string) { - let res = this.http.post(this.authUrl, {username: username, password: password}, this.httpOptions2); + let res = this.http.post(this.authUrl, {username: username, password: password}, this.httpOptions2()); this.apiKey = await firstValueFrom(res); console.log("apikey: "+this.apiKey) let res2 = this.http.get(`${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); - } }); } -- cgit v1.2.3 From ea13cf6ab3b71ff5e83fca876ec71fec1f7b00ae Mon Sep 17 00:00:00 2001 From: sowgro Date: Wed, 26 Mar 2025 15:38:46 -0400 Subject: Make frontend work with the new backend checkout system --- ufund-ui/src/app/services/auth.service.ts | 57 +++++++++++++++++++++++++++ ufund-ui/src/app/services/cupboard.service.ts | 30 +++++++++----- ufund-ui/src/app/services/users.service.ts | 52 +++++------------------- 3 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 ufund-ui/src/app/services/auth.service.ts (limited to 'ufund-ui/src/app/services') 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 = new BehaviorSubject(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(this.authUrl, {username: username, password: password}, this.httpOptions2()); + this.apiKey = await firstValueFrom(res); + console.log("apikey: "+this.apiKey) + let res2 = this.http.get(`${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; + } + +} diff --git a/ufund-ui/src/app/services/cupboard.service.ts b/ufund-ui/src/app/services/cupboard.service.ts index 9e14106..9232c0c 100644 --- a/ufund-ui/src/app/services/cupboard.service.ts +++ b/ufund-ui/src/app/services/cupboard.service.ts @@ -2,6 +2,7 @@ import {Injectable} from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import {Need} from '../models/Need'; import {Observable} from 'rxjs'; +import {AuthService} from './auth.service'; @Injectable({ providedIn: 'root' @@ -9,35 +10,44 @@ import {Observable} from 'rxjs'; export class CupboardService { private url = "http://localhost:8080/cupboard" - private httpOptions = { - headers: new HttpHeaders({'Content-Type': 'application/json'}) - }; + + httpOptions = () => ({ + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + "jelly-api-key": this.authService.getApiKey() + }) + }); constructor( - private http: HttpClient + private http: HttpClient, + private authService: AuthService ) {} createNeed(need: Need): Observable { - return this.http.post(this.url, need, this.httpOptions) + return this.http.post(this.url, need, this.httpOptions()) } getNeeds(): Observable { - return this.http.get(this.url, this.httpOptions) + return this.http.get(this.url, this.httpOptions()) } searchNeeds(name: String): Observable { - return this.http.get(`${this.url}/?name=${name}`, this.httpOptions) + return this.http.get(`${this.url}/?name=${name}`, this.httpOptions()) } getNeed(id: number): Observable { - return this.http.get(`${this.url}/${id}`, this.httpOptions) + return this.http.get(`${this.url}/${id}`, this.httpOptions()) } updateNeed(id: number, data: Need): Observable { - return this.http.put(`${this.url}/${id}`, data, this.httpOptions) + return this.http.put(`${this.url}/${id}`, data, this.httpOptions()) } deleteNeed(id: number): Observable { - return this.http.delete(`${this.url}/${id}`, this.httpOptions) + return this.http.delete(`${this.url}/${id}`, this.httpOptions()) + } + + checkoutNeed(id: number, quantity: number) { + return this.http.put(`${this.url}/checkout`, {needID: id, amount: quantity}, this.httpOptions()) } } 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 = new BehaviorSubject(null); - private apiKey: string = ""; private basket = new BehaviorSubject([]); - 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 { console.log(`${this.url}/${user.username}`, user, this.httpOptions) - return this.http.put(`${this.url}/${user.username}`, user, this.httpOptions2()) // This line is causing issues as the key is not properly being passed + return this.http.put(`${this.url}/${user.username}`, user, this.httpOptions()) } deleteUser(id: number): Observable { return this.http.delete(`${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(this.authUrl, {username: username, password: password}, this.httpOptions2()); - this.apiKey = await firstValueFrom(res); - console.log("apikey: "+this.apiKey) - let res2 = this.http.get(`${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(); }) -- cgit v1.2.3 From cfe40fa1e416fbf4586ef36b63a145453a4d6224 Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 31 Mar 2025 00:38:37 -0400 Subject: Continue working on css (1) --- ufund-ui/src/app/services/toasts.service.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 ufund-ui/src/app/services/toasts.service.ts (limited to 'ufund-ui/src/app/services') diff --git a/ufund-ui/src/app/services/toasts.service.ts b/ufund-ui/src/app/services/toasts.service.ts new file mode 100644 index 0000000..0c35e45 --- /dev/null +++ b/ufund-ui/src/app/services/toasts.service.ts @@ -0,0 +1,19 @@ +import {Injectable} from '@angular/core'; + +export enum ToastType { + INFO, + WARNING, + ERROR +} + +@Injectable({ + providedIn: 'root' +}) +export class ToastsService { + + constructor() {} + + sendToast(type: ToastType, message: string, action?: {label: string, onAction: () => void}) { + + } +} -- cgit v1.2.3 From 655f83af9ba59d6153c9916b9a31bfe12680655e Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 31 Mar 2025 13:12:42 -0400 Subject: Implemented localStorage cookies to maintain user persistence --- ufund-ui/src/app/services/auth.service.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'ufund-ui/src/app/services') diff --git a/ufund-ui/src/app/services/auth.service.ts b/ufund-ui/src/app/services/auth.service.ts index 6bc7145..b75c931 100644 --- a/ufund-ui/src/app/services/auth.service.ts +++ b/ufund-ui/src/app/services/auth.service.ts @@ -1,7 +1,8 @@ -import {Injectable} from '@angular/core'; +import {Injectable, Injector} from '@angular/core'; import {BehaviorSubject, firstValueFrom} from 'rxjs'; import {User} from '../models/User'; import {HttpClient, HttpHeaders} from '@angular/common/http'; +import {UsersService} from './users.service'; @Injectable({ providedIn: 'root' @@ -24,7 +25,9 @@ export class AuthService { }); constructor( - private http: HttpClient + private http: HttpClient, + // private userService: UsersService + private injector: Injector ) {} async login(username: string, password: string) { @@ -42,6 +45,13 @@ export class AuthService { // this.currentUser.subscribe(r => console.log("currentUser: "+r.username)) } + async restoreLogin(username: string, key: string) { + + const userService = this.injector.get(UsersService); + this.apiKey = key; + this.currentUser.next(await firstValueFrom(userService.getUser(username))) + } + getCurrentUserSubject() { return this.currentUser; } -- cgit v1.2.3 From 97c9e1e0bb73c7b08c830c47548ac1c4b5bebd0b Mon Sep 17 00:00:00 2001 From: sowgro Date: Mon, 31 Mar 2025 17:33:04 -0400 Subject: TOASTS --- ufund-ui/src/app/services/toasts.service.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'ufund-ui/src/app/services') diff --git a/ufund-ui/src/app/services/toasts.service.ts b/ufund-ui/src/app/services/toasts.service.ts index 0c35e45..4fd024e 100644 --- a/ufund-ui/src/app/services/toasts.service.ts +++ b/ufund-ui/src/app/services/toasts.service.ts @@ -1,4 +1,5 @@ -import {Injectable} from '@angular/core'; +import {Injectable, ViewContainerRef} from '@angular/core'; +import {ToastComponent} from '../components/toast/toast.component'; export enum ToastType { INFO, @@ -11,9 +12,16 @@ export enum ToastType { }) export class ToastsService { - constructor() {} + private vcr?: ViewContainerRef sendToast(type: ToastType, message: string, action?: {label: string, onAction: () => void}) { + let compRef = this.vcr?.createComponent(ToastComponent)! + compRef.setInput("message", message) + compRef.setInput("type", type) + compRef.setInput("action", action) + } + setRootViewContainerRef(vcr: ViewContainerRef) { + this.vcr = vcr } } -- cgit v1.2.3