From 7b17b9b28a4082b6e060c79663a40665f07b614d Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Sat, 3 Sep 2022 13:01:54 +0200 Subject: [PATCH] Refactoring, adding some login logic stuff --- src/app/app.component.ts | 2 +- .../components/header/header.component.html | 4 +-- src/app/components/header/header.component.ts | 8 ++++++ .../components/profile/profile.component.html | 8 +++--- .../components/profile/profile.component.ts | 4 ++- .../{ => doppelkopf}/enums/announcement.ts | 0 src/app/models/{ => doppelkopf}/enums/solo.ts | 0 src/app/models/{ => doppelkopf}/enums/team.ts | 0 src/app/models/{ => doppelkopf}/game.ts | 0 src/app/models/{ => doppelkopf}/player.ts | 0 src/app/pages/home/home.component.ts | 25 ++++++++++++++++--- src/app/services/api.service.ts | 13 ++++++++++ src/app/services/storage.service.ts | 9 +++++-- 13 files changed, 60 insertions(+), 13 deletions(-) rename src/app/models/{ => doppelkopf}/enums/announcement.ts (100%) rename src/app/models/{ => doppelkopf}/enums/solo.ts (100%) rename src/app/models/{ => doppelkopf}/enums/team.ts (100%) rename src/app/models/{ => doppelkopf}/game.ts (100%) rename src/app/models/{ => doppelkopf}/player.ts (100%) create mode 100644 src/app/services/api.service.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 6e84894..4bce49e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,5 @@ import {Component} from '@angular/core'; -import {Player} from './models/player'; +import {Player} from './models/doppelkopf/player'; @Component({ selector: 'app-root', diff --git a/src/app/components/header/header.component.html b/src/app/components/header/header.component.html index 5ff69cf..c6d2da7 100644 --- a/src/app/components/header/header.component.html +++ b/src/app/components/header/header.component.html @@ -1,8 +1,8 @@ diff --git a/src/app/components/header/header.component.ts b/src/app/components/header/header.component.ts index e8023b3..e9d162d 100644 --- a/src/app/components/header/header.component.ts +++ b/src/app/components/header/header.component.ts @@ -1,4 +1,6 @@ import {Component, OnInit} from '@angular/core'; +import {User} from '../../models/user'; +import {StorageService} from '../../services/storage.service'; @Component({ selector: 'app-header', @@ -8,8 +10,10 @@ import {Component, OnInit} from '@angular/core'; export class HeaderComponent implements OnInit { showProfilePopover: boolean = false; + user?: User; constructor() { + this.user = StorageService.getUserInfo(); } ngOnInit(): void { @@ -22,4 +26,8 @@ export class HeaderComponent implements OnInit { closeProfile(): void { this.showProfilePopover = false; } + + getUserName(): string { + return this.user ? this.user.firstName : 'Logged out'; + } } diff --git a/src/app/components/profile/profile.component.html b/src/app/components/profile/profile.component.html index d8e820c..fc1d2e5 100644 --- a/src/app/components/profile/profile.component.html +++ b/src/app/components/profile/profile.component.html @@ -1,10 +1,10 @@
-
-

Hello, Paddy!

+
+

Hello, {{this.user!.firstName}}!

You're successfully logged in.

-

Your id is: 1

+

Your id is: {{this.user!.firebonkId}}

-
+

Not logged in

Please log in to continue

diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index 4b16191..e28272c 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -1,4 +1,5 @@ -import {Component, ElementRef, EventEmitter, HostListener, OnInit, Output} from '@angular/core'; +import {Component, ElementRef, EventEmitter, HostListener, Input, OnInit, Output} from '@angular/core'; +import {User} from '../../models/user'; @Component({ selector: 'app-profile', @@ -8,6 +9,7 @@ import {Component, ElementRef, EventEmitter, HostListener, OnInit, Output} from export class ProfileComponent implements OnInit { initialClick = true; + @Input() user?: User; @Output() showProfilePopoverChange = new EventEmitter(); isLoggedIn = true; diff --git a/src/app/models/enums/announcement.ts b/src/app/models/doppelkopf/enums/announcement.ts similarity index 100% rename from src/app/models/enums/announcement.ts rename to src/app/models/doppelkopf/enums/announcement.ts diff --git a/src/app/models/enums/solo.ts b/src/app/models/doppelkopf/enums/solo.ts similarity index 100% rename from src/app/models/enums/solo.ts rename to src/app/models/doppelkopf/enums/solo.ts diff --git a/src/app/models/enums/team.ts b/src/app/models/doppelkopf/enums/team.ts similarity index 100% rename from src/app/models/enums/team.ts rename to src/app/models/doppelkopf/enums/team.ts diff --git a/src/app/models/game.ts b/src/app/models/doppelkopf/game.ts similarity index 100% rename from src/app/models/game.ts rename to src/app/models/doppelkopf/game.ts diff --git a/src/app/models/player.ts b/src/app/models/doppelkopf/player.ts similarity index 100% rename from src/app/models/player.ts rename to src/app/models/doppelkopf/player.ts diff --git a/src/app/pages/home/home.component.ts b/src/app/pages/home/home.component.ts index 411e2d7..be7252e 100644 --- a/src/app/pages/home/home.component.ts +++ b/src/app/pages/home/home.component.ts @@ -1,5 +1,8 @@ import {Component, OnInit} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; +import {User} from '../../models/user'; +import {ApiService} from '../../services/api.service'; +import {StorageService} from '../../services/storage.service'; @Component({ selector: 'app-home', @@ -14,9 +17,25 @@ export class HomeComponent implements OnInit { } ngOnInit(): void { - this.route.queryParams.subscribe(params => { - // Read params - }); + this.authenticateUser(); } + /** + * Reads authentication details from query parameters and tries to authenticate the user with the API. + * @private + */ + private authenticateUser() { + this.route.queryParams.subscribe(params => { + let user: User = { + firebonkId: params['firebonkId'], + uuid: '', + firstName: params['firstName'], + token: params['token'], + sessionKey: '' + }; + + let authenticatedUser = ApiService.performAuthentication(user); + StorageService.setUserInfo(authenticatedUser); + }); + } } diff --git a/src/app/services/api.service.ts b/src/app/services/api.service.ts new file mode 100644 index 0000000..57230c3 --- /dev/null +++ b/src/app/services/api.service.ts @@ -0,0 +1,13 @@ +import {User} from '../models/user'; + +export class ApiService { + static apiUrl = 'https://api.plutodev.de/fbcards/'; + + /** + * Sends user info to API to retrieve a session key. + * @param user + */ + static performAuthentication(user: User): User { + return user; + } +} diff --git a/src/app/services/storage.service.ts b/src/app/services/storage.service.ts index 32b504c..f7b5aae 100644 --- a/src/app/services/storage.service.ts +++ b/src/app/services/storage.service.ts @@ -1,7 +1,11 @@ import {User} from '../models/user'; export class StorageService { - getUserInfo(): User { + static getUserInfo(): User | undefined { + if(localStorage.getItem('user_loggedIn') !== '1') { + return undefined; + } + const firebonkId = parseInt((localStorage.getItem('user_firebonkId') ?? '-1'), 10); const uuid = localStorage.getItem('user_uuid') ?? ''; const firstName = localStorage.getItem('user_firstName') ?? ''; @@ -17,7 +21,8 @@ export class StorageService { }; } - setUserInfo(user: User): void { + static setUserInfo(user: User): void { + localStorage.setItem('user_loggedIn', '1'); localStorage.setItem('user_firebonkId', String(user.firebonkId)); localStorage.setItem('user_uuid', String(user.uuid)); localStorage.setItem('user_firstName', String(user.firstName));