Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"polyfills": [
"zone.js"
],
"allowedCommonJsDependencies": [
"crypto-js"
],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "sass",
"assets": [
Expand Down
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"@angular/platform-browser": "^16.1.0",
"@angular/platform-browser-dynamic": "^16.1.0",
"@angular/router": "^16.1.0",
"@types/crypto-js": "^4.1.1",
"angular-cli-ghpages": "^1.0.6",
"crypto-js": "^4.1.1",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"xp.css": "^0.2.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CorrectAnswerWindowComponent implements OnInit {

private saveCurrentScore(): void {
const currentScore: string | null = this.storageService.get(StorageKeyEnum.CURRENT_SCORE);
let currentScoreNumber: number = currentScore === null ? 0 : parseInt(currentScore);
let currentScoreNumber: number = (currentScore === null || currentScore === '') ? 0 : parseInt(currentScore);

currentScoreNumber += this.questionScore;

Expand Down
2 changes: 1 addition & 1 deletion src/app/main-window/main-window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class MainWindowComponent implements OnInit {

if (sameHour && sameMinute && sameSecond) {
this.quizCanBeAnswered = true;
this.storageService.save(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE, '');
this.storageService.clear(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE);
resolve();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/score-window/score-window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ScoreWindowComponent implements OnInit {
public ngOnInit(): void {
const currentScore: string | null = this.storageService.get(StorageKeyEnum.CURRENT_SCORE);

this.score = currentScore === null ? 0 : parseInt(currentScore);
this.score = (currentScore === null || currentScore === '') ? 0 : parseInt(currentScore);
}

public async returnHome(): Promise<void> {
Expand Down
22 changes: 20 additions & 2 deletions src/service/storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import {Injectable} from '@angular/core';
import {StorageKeyEnum} from "../model/StorageKeyEnum";
import { AES, enc } from 'crypto-js';

@Injectable({
providedIn: 'root'
})
export class StorageService {

private readonly localStorage: Storage;
private readonly KEY: string = 'ENCRYPTION_KEY';

constructor() {
this.localStorage = window.localStorage;
}

public save(key: StorageKeyEnum, value: string): void {
this.localStorage.setItem(key, value);
this.localStorage.setItem(key, this.encrypt(value));
}

public get(key: StorageKeyEnum): string | null {
return this.localStorage.getItem(key);
const encryptedItem: string | null = this.localStorage.getItem(key);

return encryptedItem === null ? '' : this.decrypt(encryptedItem);
}

public clear(key: StorageKeyEnum) {
this.localStorage.removeItem(key);
}

private encrypt(value: string): string {
const encrypted = AES.encrypt(value, this.KEY);
return encrypted.toString();
}

private decrypt(value: string): string {
const decrypted = AES.decrypt(value, this.KEY);
return decrypted.toString(enc.Utf8);
}
}