From cd751b529baa421d4ad775f0bf2f6df22ec779b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Wed, 3 May 2023 11:55:26 +0200 Subject: [PATCH] cookie init --- package.json | 1 + src/app/app.module.ts | 4 ++- src/app/component/list/list.component.ts | 4 +++ .../management/management.component.ts | 6 +++- src/app/home/home.component.ts | 16 +++++---- src/app/service/cookie.service.ts | 34 +++++++++++++++++++ 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/app/service/cookie.service.ts diff --git a/package.json b/package.json index b2336c8..784e7c6 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "deepl": "^1.0.13", "geolib": "^3.3.3", "html-to-image": "^1.11.11", + "ngx-cookie-service": "^15.0.0", "nodemon": "^2.0.22", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", diff --git a/src/app/app.module.ts b/src/app/app.module.ts index d9ff059..f234120 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -10,6 +10,8 @@ import {FormsModule} from "@angular/forms"; import { HttpClientModule} from "@angular/common/http"; import { SafePipe } from './pipes/safe.pipe'; import { ManagementComponent } from './component/management/management.component'; +import {CookieService} from "ngx-cookie-service"; + @NgModule({ declarations: [ AppComponent, @@ -25,7 +27,7 @@ import { ManagementComponent } from './component/management/management.component FormsModule, HttpClientModule, ], - providers: [], + providers: [CookieService], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/component/list/list.component.ts b/src/app/component/list/list.component.ts index 4086238..c86922a 100644 --- a/src/app/component/list/list.component.ts +++ b/src/app/component/list/list.component.ts @@ -8,6 +8,7 @@ import {LocationService} from "../../service/http/location.service"; import {LocationEntity} from "../../interface/LocationEntity"; import {WaypointsEntity} from "../../interface/WaypointsEntity"; import {WaypointService} from "../../service/http/waypoint.service" +import {cookieService} from "../../service/cookie.service"; @Component({ selector: 'app-list', @@ -15,6 +16,7 @@ import {WaypointService} from "../../service/http/waypoint.service" styleUrls: ['./list.component.css'] }) export class ListComponent implements OnInit, OnChanges { + username: string = ''; locationParams: string | undefined locations: LocationEntity[] | undefined; location: LocationEntity | undefined; @@ -37,11 +39,13 @@ export class ListComponent implements OnInit, OnChanges { private readTranslationJsonService: ReadTranslateJsonService, private locationService: LocationService, private waypointService: WaypointService, + private cookieService: cookieService, ) { } async ngOnInit() { this.translations = this.readTranslationJsonService.getListTransaltions(); + this.username = this.cookieService.getUsername(); this.route.params.subscribe(params => { this.locationParams = params['location']; }); diff --git a/src/app/component/management/management.component.ts b/src/app/component/management/management.component.ts index 08af5c8..79c8cfb 100644 --- a/src/app/component/management/management.component.ts +++ b/src/app/component/management/management.component.ts @@ -6,10 +6,10 @@ import {ActivatedRoute} from "@angular/router"; import {LocationService} from "../../service/http/location.service"; import {UserService} from "../../service/http/user.service"; import {WaypointService} from "../../service/http/waypoint.service"; -import {toNumbers} from "@angular/compiler-cli/src/version_helpers"; import {managementTranslations} from "../../interface/translations"; import {TranslateService} from "../../service/language/translate.service"; import {ReadTranslateJsonService} from "../../service/language/readTranslateJson.service"; +import {cookieService} from "../../service/cookie.service"; @Component({ selector: 'app-home', @@ -18,6 +18,8 @@ import {ReadTranslateJsonService} from "../../service/language/readTranslateJson }) export class ManagementComponent implements OnInit { + + username: string = ''; showUserForm: boolean = false; showLocationForm: boolean = false; showWaypointForm: boolean = false; @@ -39,11 +41,13 @@ export class ManagementComponent implements OnInit { private userService: UserService, private translateService: TranslateService, private readTranslationJsonService: ReadTranslateJsonService, + private cookieService: cookieService, ) { } ngOnInit(): void { this.translations = this.readTranslationJsonService.getManagementTranslations(); + this.username = this.cookieService.getUsername(); this.locationService.getLocations().subscribe(locations => { this.locations = locations; }); diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index 649451d..2f5c906 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -1,5 +1,5 @@ import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; -import {BehaviorSubject, distinctUntilChanged, fromEvent, Observable, Subject, Subscription} from "rxjs"; +import {distinctUntilChanged, fromEvent, Subject, Subscription} from "rxjs"; import {ReadjsonService} from "../service/readjson.service"; import {Router} from "@angular/router"; import {TranslateService} from '../service/language/translate.service'; @@ -7,6 +7,7 @@ import {ReadTranslateJsonService} from "../service/language/readTranslateJson.se import {homeTranslations} from "../interface/translations"; import {LocationService} from "../service/http/location.service"; import {LocationEntity} from "../interface/LocationEntity"; +import {cookieService} from "../service/cookie.service"; @Component({ @@ -16,11 +17,9 @@ import {LocationEntity} from "../interface/LocationEntity"; }) export class HomeComponent implements OnInit, AfterViewInit, OnDestroy { - @ViewChild('myInput') myInput?: ElementRef; - public locationsPopup: Subject = new Subject() - + username: string = ''; subs: Subscription[] = []; backgroundColor: string | undefined; locations: LocationEntity[] = []; @@ -31,6 +30,7 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy { suggerimento: string = ''; completamento: string = ''; translations: homeTranslations = {} as homeTranslations; + public locationsPopup: Subject = new Subject() constructor( @@ -38,14 +38,16 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy { private router: Router, private translateService: TranslateService, private readTranslationJsonService: ReadTranslateJsonService, - private locationService: LocationService - ){ + private locationService: LocationService, + private cookieService: cookieService, + ) { } // Initializes the component and loads translations and locations ngOnInit(): void { this.translations = this.readTranslationJsonService.getHomeTranslations(); - console.log("translations loaded", this.translations) + console.log("translations loaded", this.translations); + this.username= this.cookieService.getUsername(); this.locationService.getLocations() .subscribe(locations => { this.locations = locations; diff --git a/src/app/service/cookie.service.ts b/src/app/service/cookie.service.ts new file mode 100644 index 0000000..2bf6a1e --- /dev/null +++ b/src/app/service/cookie.service.ts @@ -0,0 +1,34 @@ +import {Injectable} from '@angular/core'; +import {CookieService} from 'ngx-cookie-service'; +import {Router} from "@angular/router"; + +@Injectable({ + providedIn: 'root' +}) +export class cookieService { + + constructor( + private coockieService: CookieService, + private router: Router, + ) { + } + + setUsername(username: string): void { + this.coockieService.set('username', username); + } + + getUsername(): string { + let username = this.coockieService.get('username'); + if (username == null) { + this.router.navigate(['/login']); + return ''; + } else { + return username; + } + } + + deleteUsername(): void { + this.coockieService.delete('username'); + } + +}