FIX: improved the validation from localstorage for username and characterLayers
This commit is contained in:
parent
575c1a9f23
commit
8e467f3e10
8 changed files with 84 additions and 9 deletions
|
@ -1,3 +1,5 @@
|
|||
import {MAX_USERNAME_LENGTH} from "../Enum/EnvironmentVariable";
|
||||
|
||||
export interface CharacterTexture {
|
||||
id: number,
|
||||
level: number,
|
||||
|
@ -5,6 +7,23 @@ export interface CharacterTexture {
|
|||
rights: string
|
||||
}
|
||||
|
||||
export const maxUserNameLength: number = MAX_USERNAME_LENGTH;
|
||||
|
||||
export function isUserNameValid(value: string): boolean {
|
||||
const regexp = new RegExp('^[A-Za-z]{1,'+maxUserNameLength+'}$');
|
||||
return regexp.test(value);
|
||||
}
|
||||
|
||||
export function areCharacterLayersValid(value: string[]): boolean {
|
||||
if (!value.length) return false;
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (/^\w+$/.exec(value[i]) === null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export class LocalUser {
|
||||
constructor(public readonly uuid:string, public readonly jwtToken: string, public readonly textures: CharacterTexture[]) {
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {LocalUser} from "./LocalUser";
|
||||
import {areCharacterLayersValid, isUserNameValid, LocalUser} from "./LocalUser";
|
||||
|
||||
const playerNameKey = 'playerName';
|
||||
const selectedPlayerKey = 'selectedPlayer';
|
||||
|
@ -22,8 +22,9 @@ class LocalUserStore {
|
|||
setName(name:string): void {
|
||||
localStorage.setItem(playerNameKey, name);
|
||||
}
|
||||
getName(): string {
|
||||
return localStorage.getItem(playerNameKey) || '';
|
||||
getName(): string|null {
|
||||
const value = localStorage.getItem(playerNameKey) || '';
|
||||
return isUserNameValid(value) ? value : null;
|
||||
}
|
||||
|
||||
setPlayerCharacterIndex(playerCharacterIndex: number): void {
|
||||
|
@ -44,7 +45,8 @@ class LocalUserStore {
|
|||
localStorage.setItem(characterLayersKey, JSON.stringify(layers));
|
||||
}
|
||||
getCharacterLayers(): string[]|null {
|
||||
return JSON.parse(localStorage.getItem(characterLayersKey) || "null");
|
||||
const value = JSON.parse(localStorage.getItem(characterLayersKey) || "null");
|
||||
return areCharacterLayersValid(value) ? value : null;
|
||||
}
|
||||
|
||||
setGameQualityValue(value: number): void {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue