diff options
Diffstat (limited to 'ufund-ui/src')
-rw-r--r-- | ufund-ui/src/app/app.component.html | 4 | ||||
-rw-r--r-- | ufund-ui/src/app/app.component.ts | 21 | ||||
-rw-r--r-- | ufund-ui/src/app/components/login/login.component.html | 8 | ||||
-rw-r--r-- | ufund-ui/src/app/components/login/login.component.ts | 14 | ||||
-rw-r--r-- | ufund-ui/src/app/models/Need.ts | 2 | ||||
-rw-r--r-- | ufund-ui/src/app/models/User.ts | 2 | ||||
-rw-r--r-- | ufund-ui/src/app/services/cupboard.service.ts | 3 | ||||
-rw-r--r-- | ufund-ui/src/app/services/users.service.ts | 40 |
8 files changed, 75 insertions, 19 deletions
diff --git a/ufund-ui/src/app/app.component.html b/ufund-ui/src/app/app.component.html index cfebc2b..6b9338c 100644 --- a/ufund-ui/src/app/app.component.html +++ b/ufund-ui/src/app/app.component.html @@ -1,4 +1,6 @@ -<h1>jelly solutions:</h1> +<h1>jelly solutions</h1> +<span>{{currentUser$ | async}}</span> +<hr> <router-outlet /> diff --git a/ufund-ui/src/app/app.component.ts b/ufund-ui/src/app/app.component.ts index 2dbf33c..a85d04b 100644 --- a/ufund-ui/src/app/app.component.ts +++ b/ufund-ui/src/app/app.component.ts @@ -1,4 +1,7 @@ -import { Component } from '@angular/core'; +import {Component, OnInit} from '@angular/core'; +import {UsersService} from './services/users.service'; +import {BehaviorSubject, Observable, Subject} from 'rxjs'; +import {User} from './models/User'; @Component({ selector: 'app-root', @@ -6,6 +9,18 @@ import { Component } from '@angular/core'; standalone: false, styleUrl: './app.component.css' }) -export class AppComponent { - title = 'ufund-ui'; +export class AppComponent implements OnInit { + // title = 'ufund-ui'; + currentUser$: BehaviorSubject<string> = new BehaviorSubject<string>("Logged out."); + + constructor( + private userService: UsersService + ) {} + + ngOnInit() { + this.userService.getCurrentUser().subscribe(r => { + this.currentUser$?.next("Logged in as " + r.username) + }) + } + } diff --git a/ufund-ui/src/app/components/login/login.component.html b/ufund-ui/src/app/components/login/login.component.html index 41427ae..178ddbf 100644 --- a/ufund-ui/src/app/components/login/login.component.html +++ b/ufund-ui/src/app/components/login/login.component.html @@ -1,5 +1,5 @@ <p>Login:</p> -<input placeholder="Username" type="text"> -<input placeholder="Password" type="password"> -<button>Login</button> -<button>Create Account...</button> +<input placeholder="Username" type="text" #username> +<input placeholder="Password" type="password" #password> +<button type="button" (click)="login(username.value, password.value)">Login</button> +<button type="button">Create Account...</button> diff --git a/ufund-ui/src/app/components/login/login.component.ts b/ufund-ui/src/app/components/login/login.component.ts index efb8a58..9a4eb0f 100644 --- a/ufund-ui/src/app/components/login/login.component.ts +++ b/ufund-ui/src/app/components/login/login.component.ts @@ -1,4 +1,5 @@ -import { Component } from '@angular/core'; +import { Component } from '@angular/core' +import {UsersService} from '../../services/users.service'; @Component({ selector: 'app-login', @@ -7,5 +8,16 @@ import { Component } from '@angular/core'; styleUrl: './login.component.css' }) export class LoginComponent { + constructor( + protected usersService: UsersService + ) {} + login(username: string | null, password: string | null) { + console.log(`attempting to log in with ${username} ${password}`) + if (!username || !password) { + return; + } + + this.usersService.login(username, password) + } } diff --git a/ufund-ui/src/app/models/Need.ts b/ufund-ui/src/app/models/Need.ts index c0425ec..9e97fd4 100644 --- a/ufund-ui/src/app/models/Need.ts +++ b/ufund-ui/src/app/models/Need.ts @@ -1,7 +1,7 @@ export interface Need { name: string, id: number, - filterAttributes: String[], + filterAttributes: string[], type: GoalType; maxGoal: number; current: number; diff --git a/ufund-ui/src/app/models/User.ts b/ufund-ui/src/app/models/User.ts index 46fe4a1..9149fe7 100644 --- a/ufund-ui/src/app/models/User.ts +++ b/ufund-ui/src/app/models/User.ts @@ -2,5 +2,5 @@ import {Need} from './Need'; export interface User { username: string; - cupboard: Need[]; + basket: Need[]; } diff --git a/ufund-ui/src/app/services/cupboard.service.ts b/ufund-ui/src/app/services/cupboard.service.ts index c123841..4a2b4b0 100644 --- a/ufund-ui/src/app/services/cupboard.service.ts +++ b/ufund-ui/src/app/services/cupboard.service.ts @@ -18,8 +18,7 @@ export class CupboardService { ) {} createNeed(need: Need): Observable<boolean> { - return this.http.post<boolean>( - this.url, need, this.httpOptions) + return this.http.post<boolean>(this.url, need, this.httpOptions) } getNeeds(): Observable<Need[]> { diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts index 571c004..28cc266 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 {firstValueFrom, Observable, of, Subject} from 'rxjs'; import {User} from '../models/User'; @Injectable({ @@ -8,11 +8,24 @@ import {User} from '../models/User'; }) export class UsersService { - private currentUserID? : number + private currentUser : Subject<User> = new Subject(); + 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( @@ -23,7 +36,7 @@ export class UsersService { return this.http.post<User>(this.url, data, 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,22 @@ 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 + getCurrentUser() { + return this.currentUser; + } + + 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)) } } |