From 176e2cfa35fcbfa54488bb41dcf4b860b432f564 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Sun, 26 May 2024 18:52:00 +0200 Subject: [PATCH] added crypto-es & bugfixes translation --- package-lock.json | 6 +++ package.json | 1 + src/app/app.component.ts | 2 - src/app/components/main/main.component.ts | 2 +- src/app/service/login.service.ts | 51 ++++++++++++++++------- src/app/service/user.service.ts | 15 +++++-- src/assets/i18n/de.json | 6 ++- src/assets/i18n/en.json | 6 ++- 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0f09c59..93efd6d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@ctrl/ngx-emoji-mart": "^9.2.0", "@ngx-translate/core": "^15.0.0", "@ngx-translate/http-loader": "^8.0.0", + "crypto-es": "^2.1.0", "ng2-pdf-viewer": "^10.0.0", "ngx-extended-pdf-viewer": "^19.6.6", "rxjs": "~7.8.0", @@ -6106,6 +6107,11 @@ "node": ">= 8" } }, + "node_modules/crypto-es": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/crypto-es/-/crypto-es-2.1.0.tgz", + "integrity": "sha512-C5Dbuv4QTPGuloy5c5Vv/FZHtmK+lobLAypFfuRaBbwCsk3qbCWWESCH3MUcBsrgXloRNMrzwUAiPg4U6+IaKA==" + }, "node_modules/css-loader": { "version": "6.10.0", "resolved": "https://registry.npmjs.org/css-loader/-/css-loader-6.10.0.tgz", diff --git a/package.json b/package.json index c28cd25..c09e623 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@ctrl/ngx-emoji-mart": "^9.2.0", "@ngx-translate/core": "^15.0.0", "@ngx-translate/http-loader": "^8.0.0", + "crypto-es": "^2.1.0", "ng2-pdf-viewer": "^10.0.0", "ngx-extended-pdf-viewer": "^19.6.6", "rxjs": "~7.8.0", diff --git a/src/app/app.component.ts b/src/app/app.component.ts index f5bc0b9..90ae32d 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -12,6 +12,4 @@ import { ToggleBooleanService } from './service/toggle-boolean.service'; }) export class AppComponent { title = 'dabubble'; - - constructor(public toggleAllBooleans:ToggleBooleanService){} } diff --git a/src/app/components/main/main.component.ts b/src/app/components/main/main.component.ts index 581aea2..ec3b9d8 100644 --- a/src/app/components/main/main.component.ts +++ b/src/app/components/main/main.component.ts @@ -67,7 +67,7 @@ export class MainComponent { * Checks if the user is logged in. */ ifUserLogin() { - if (this.userService.getCurrentUserId() === undefined) { + if (this.userService.getCurrentUserId() === null) { this.route.navigateByUrl('/login'); } } diff --git a/src/app/service/login.service.ts b/src/app/service/login.service.ts index 1073fb4..f1dc59c 100644 --- a/src/app/service/login.service.ts +++ b/src/app/service/login.service.ts @@ -11,7 +11,6 @@ import { updateDoc, where, } from '@angular/fire/firestore'; -// import { User } from '../interface/user.interface'; import { getAuth, signInWithPopup, @@ -23,7 +22,9 @@ import { Router } from '@angular/router'; import { User } from '../interface/user.interface'; import { UserService } from './user.service'; import { publicChannels } from '../interface/channel.interface'; -// import { User } from 'firebase/auth'; +import { TranslateService } from '@ngx-translate/core'; +import CryptoJS from 'crypto-es'; +import { CryptoJSSecretKey } from './../../environments/config'; @Injectable({ providedIn: 'root', @@ -43,8 +44,13 @@ export class loginService { passwordIcon: string = './assets/img/login/close-eye.svg'; private hasAnimationPlayed = false; private introCompleteStatus = false; + private secretKey: string = CryptoJSSecretKey.secretKey; - constructor(private router: Router, private userService: UserService) { + constructor( + private router: Router, + private userService: UserService, + private translate: TranslateService + ) { this.ifUserLoggedIn(); } @@ -54,7 +60,7 @@ export class loginService { * @returns {void} */ ifUserLoggedIn() { - let currentUser = localStorage.getItem('currentUserDABubble'); + let currentUser = this.userService.getCurrentUserId(); if (currentUser !== null) { this.router.navigate([`/main`]); } @@ -78,7 +84,7 @@ export class loginService { getDocs(querySnapshot) .then((snapshot) => this.userDocument(snapshot)) .catch((error) => { - console.error('Fehler beim Abrufen des Benutzerdokuments:', error); + console.error('Error when retrieving the user document:', error); }); }) .catch((error) => { @@ -99,7 +105,7 @@ export class loginService { this.email = ''; this.password = ''; } else { - console.error('Kein zugehöriges Benutzerdokument gefunden.'); + console.error('No associated user document found.'); } } @@ -110,15 +116,19 @@ export class loginService { switchCase(errorCode: string) { switch (errorCode) { case 'auth/invalid-credential': - this.errorMessage = - '*Ungültige Anmeldeinformationen. Bitte überprüfen Sie Ihre Eingaben.'; + this.translate.get('login.errorText3').subscribe((res: string) => { + this.errorMessage = res; + }); break; case 'auth/too-many-requests': - this.errorMessage = - '*Der Zugriff auf dieses Konto wurde aufgrund zahlreicher fehlgeschlagener Anmeldeversuche vorübergehend deaktiviert.'; + this.translate.get('login.errorText4').subscribe((res: string) => { + this.errorMessage = res; + }); break; default: - this.errorMessage = '*Bitte Überprüfe deine Eingaben.'; + this.translate.get('login.errorText5').subscribe((res: string) => { + this.errorMessage = res; + }); break; } } @@ -142,8 +152,9 @@ export class loginService { }) .catch((error) => { console.error(error); - this.errorMessage = - 'Fehler bei der Gastanmeldung. Bitte versuchen Sie es später erneut.'; + this.translate.get('login.errorText6').subscribe((res: string) => { + this.errorMessage = res; + }); }); } @@ -363,7 +374,7 @@ export class loginService { talkToUserId: currentUser, }); } catch (error) { - console.error('Fehler beim Erstellen des privaten Kanals: ', error); + console.error('Error when creating the private channel: ', error); } } @@ -371,8 +382,16 @@ export class loginService { * Stores the current user's ID in the local storage. * @param {string} userId - The ID of the current user to be stored. */ - async getUserIdInLocalStorage(userId: string) { - localStorage.setItem('currentUserDABubble', JSON.stringify(userId)); + async getUserIdInLocalStorage(userId: string): Promise { + const encryptedValue = CryptoJS.AES.encrypt( + JSON.stringify(userId), + this.secretKey + ).toString(); + localStorage.setItem('currentUserDABUBBLE', encryptedValue); + localStorage.setItem( + 'sessionTimeDABUBBLE', + new Date().getTime().toString() + ); await this.updateUserOnlineStatus(userId); window.location.reload(); } diff --git a/src/app/service/user.service.ts b/src/app/service/user.service.ts index ff30b62..ad01cf4 100644 --- a/src/app/service/user.service.ts +++ b/src/app/service/user.service.ts @@ -10,6 +10,8 @@ import { User } from '../interface/user.interface'; import { ChannleService } from './channle.service'; import { getAuth, signOut } from 'firebase/auth'; import { Router } from '@angular/router'; +import CryptoJS from 'crypto-es'; +import { CryptoJSSecretKey } from './../../environments/config'; @Injectable({ providedIn: 'root', @@ -22,6 +24,7 @@ export class UserService implements OnDestroy { getUserIDs: string[] = []; getFiltertUsers: User[] = []; isUserLogin: boolean = true; + private secretKey: string = CryptoJSSecretKey.secretKey; unsubUser; @@ -34,10 +37,13 @@ export class UserService implements OnDestroy { * @returns {string|number|undefined} The ID of the current user if found in local storage, otherwise undefined. */ getCurrentUserId() { - let currentUser = localStorage.getItem('currentUserDABubble'); - if (currentUser !== null) { - return JSON.parse(currentUser); + const encryptedValue = localStorage.getItem('currentUserDABUBBLE'); + if (encryptedValue) { + const bytes = CryptoJS.AES.decrypt(encryptedValue, this.secretKey); + const decryptedValue = bytes.toString(CryptoJS.enc.Utf8); + return JSON.parse(decryptedValue); } + return null; } /** @@ -162,7 +168,8 @@ export class UserService implements OnDestroy { * Deletes the user ID from local storage. */ deleteUserIdInLocalStorage() { - localStorage.removeItem('currentUserDABubble'); + localStorage.removeItem('currentUserDABUBBLE'); + localStorage.removeItem('sessionTimeDABUBBLE'); } /** diff --git a/src/assets/i18n/de.json b/src/assets/i18n/de.json index 943078a..17e0a7d 100644 --- a/src/assets/i18n/de.json +++ b/src/assets/i18n/de.json @@ -12,7 +12,11 @@ "topText": "Neu bei DABubble?", "topText2": "Konto erstellen", "errorText": "*Email ist erforderlich.", - "errorText2": "*Passwort ist erforderlich." + "errorText2": "*Passwort ist erforderlich.", + "errorText3": "*Ungültige Anmeldeinformationen.", + "errorText4": "*Zuviele fehlgeschlagener Anmeldeversuche.", + "errorText5": "*Bitte Überprüfe deine Eingaben.", + "errorText6": "*Bitte versuchen Sie es später erneut." }, "createAccount": { diff --git a/src/assets/i18n/en.json b/src/assets/i18n/en.json index b4024ae..2ce7e35 100644 --- a/src/assets/i18n/en.json +++ b/src/assets/i18n/en.json @@ -12,7 +12,11 @@ "topText": "New to DABubble?", "topText2": "Create account", "errorText": "*Email is required.", - "errorText2": "*Password is required." + "errorText2": "*Password is required.", + "errorText3": "*Invalid login credentials.", + "errorText4": "*Too many failed login attempts.", + "errorText5": "*Please check your entries.", + "errorText6": "*Please try again later." }, "createAccount": {