Skip to content

Commit

Permalink
fix: #1044 | 1,2,3,5,7,8,9,0,10,11,12;
Browse files Browse the repository at this point in the history
  • Loading branch information
migbash committed Feb 15, 2023
1 parent 82d989b commit 5bd24e1
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 31 deletions.
52 changes: 29 additions & 23 deletions src/lib/components/_main_/auth/Auth_Widget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ COMPONENT JS (w/ TS)
const [BETARENA_USER, EXISTS] = await user_firestore(
firebase_user?.uid,
firebase_user,
web3_wallet_addr
web3_wallet_addr,
auth_provider_type
);
let user_obj: Scores_User = {
firebase_user_data: firebase_user,
Expand Down Expand Up @@ -547,29 +548,34 @@ COMPONENT JS (w/ TS)
async function user_firestore(
uid: string,
firebase_user: User,
web3_wallet_addr: string
web3_wallet_addr: string,
auth_provider_type: Auth_Type
): Promise<[Betarena_User, boolean]> {
const docRef = doc(db_firestore, "betarena_users", uid);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
// [ℹ] return existing firestore user-instance;
dlog(`${AUTH_DEBUG_TAG} 🟢 Target UID exists`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
dlog(`${AUTH_DEBUG_TAG} User Data ${docSnap.data()}`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
return [docSnap.data() as Betarena_User, true]
} else {
// [ℹ] create new user-instance;
dlog(`${AUTH_DEBUG_TAG} 🔴 Target UID does not exists`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
dlog(`${AUTH_DEBUG_TAG} 🔵 Creating new Betarena_User instance`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
const scores_user_data: Betarena_User = {
lang: server_side_language,
registration_type: [],
// NOTE: max. length - no separator - no random digits
username: generateUsername('', 0, 10),
register_date: firebase_user?.metadata?.creationTime, // [ℹ] can be null (wehn using web3)
profile_photo: firebase_user?.photoURL, // [ℹ] can be null (wehn using web3)
web3_wallet_addr: web3_wallet_addr || undefined
}
return [scores_user_data, false]
try {
const docRef = doc(db_firestore, "betarena_users", uid);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
// [ℹ] return existing firestore user-instance;
dlog(`${AUTH_DEBUG_TAG} 🟢 Target UID exists`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
dlog(`${AUTH_DEBUG_TAG} User Data ${docSnap.data()}`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
return [docSnap.data() as Betarena_User, true]
} else {
// [ℹ] create new user-instance;
dlog(`${AUTH_DEBUG_TAG} 🔴 Target UID does not exists`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
dlog(`${AUTH_DEBUG_TAG} 🔵 Creating new Betarena_User instance`, AUTH_DEBUG_TOGGLE, AUTH_DEBUG_STYLE)
const scores_user_data: Betarena_User = {
lang: server_side_language,
registration_type: [auth_provider_type],
// NOTE: max. length - no separator - no random digits
username: generateUsername('', 0, 10),
register_date: firebase_user?.metadata?.creationTime, // [ℹ] can be null (wehn using web3)
profile_photo: firebase_user?.photoURL, // [ℹ] can be null (wehn using web3)
web3_wallet_addr: web3_wallet_addr || undefined
}
return [scores_user_data, false]
}
} catch (error) {
console.error('Error adding document: ', error);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/components/_main_/header/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ COMPONENT JS - BASIC
// COMPONENT METHODS
// ~~~~~~~~~~~~~~~~~~~~~
let setOnce = false;
$: if ($userBetarenaSettings.user != undefined && !setOnce) {
setOnce = true
let userlang = $userBetarenaSettings.user?.scores_user_data?.lang
console.log("🔴🔴🔴🔴 HERE!!!")
selectLanguage(userlang)
}
$: server_side_language = platfrom_lang_ssr(
$page.route.id,
$page.error,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/components/page/profile/Modal-ConnectWallet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ COMPONENT HTML
btn-hollow
w-500
s-14
row-space-out
color-black-2
"
on:click={() => connect_wallet_action()}
Expand Down Expand Up @@ -163,6 +162,7 @@ COMPONENT STYLE
padding: 20px;
padding-top: 45px;
text-align: -webkit-center;
text-align: -moz-center;
overflow: hidden;
}
div#modal-delete-box > img#close-vector {
Expand All @@ -173,7 +173,8 @@ COMPONENT STYLE
z-index: 400000002;
}
button#sign-in-metamask-btn {
width: auto;
width: -webkit-fill-available;
width: -moz-available;
border-radius: 60px;
}
Expand Down
48 changes: 45 additions & 3 deletions src/lib/components/page/profile/Widget-AccountSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ COMPONENT JS (w/ TS)
import { viewport_change } from '$lib/utils/platform-functions';
import { deleteUser } from 'firebase/auth';
import {
collection,
deleteDoc,
doc,
updateDoc
getDocs,
query,
updateDoc,
where
} from 'firebase/firestore';
import { onMount } from 'svelte';
Expand All @@ -44,6 +48,7 @@ COMPONENT JS (w/ TS)
let files: HTMLInputElement['files'];
let fileInputElem: HTMLInputElement;
let usernameInput: string;
let usernameErrorExist: boolean;
let profile_picture_exists: boolean = false;
let profile_wallet_connected: boolean = false;
let processing: boolean = false;
Expand All @@ -55,7 +60,7 @@ COMPONENT JS (w/ TS)
$: if (RESPONSE_PROFILE_DATA != undefined) no_widget_data = false;
console.log('no_widget_data', no_widget_data)
$: if (files) {
$: if (files != undefined) {
profile_picture_select();
}
Expand All @@ -77,6 +82,7 @@ COMPONENT JS (w/ TS)
/**
* @description kickstarts the picture crop step;
* @returns {Promise<void>}
*/
async function profile_picture_select(): Promise<void> {
// NOTE: `file` is of type `FileList`, not an Array:
Expand All @@ -89,8 +95,15 @@ COMPONENT JS (w/ TS)
true
);
}
// [ℹ] validation [1]
if (files[0].size >= 1000000) {
alert("🔴 Uploaded picture is too large. Limit is 1MB.");
files = undefined;
return;
}
profile_crop_widget.load_picture(files[0]);
modal_pic_crop_show = true;
files = undefined;
return;
}
Expand Down Expand Up @@ -189,6 +202,12 @@ COMPONENT JS (w/ TS)
*/
async function update_username(): Promise<void> {
dlog('🔵 Updating username...');
// [ℹ] validation [1]
const valid = await username_Update_validation()
if (!valid) {
alert('🔴 Username is invalid')
return;
}
// [ℹ] (update)from localStorage()
userBetarenaSettings.updateUsername(
usernameInput
Expand All @@ -206,7 +225,30 @@ COMPONENT JS (w/ TS)
dlog('🟢 Username updated', true);
}
// TODO: update wallet address (+connect/disconnect)
async function username_Update_validation(): Promise<boolean> {
dlog('🔵 Validating username...', true);
let valid = true;
// [ℹ] validation [1] - uniqueness
const usersDb = collection(db_firestore, "betarena_users");
const queryUsername = query(usersDb, where("username", "==", usernameInput));
const querySnapshot = await getDocs(queryUsername); // can be access individually;
// DOC: https://firebase.google.com/docs/firestore/query-data/queries
dlog(querySnapshot, false);
if (querySnapshot.docs.length > 0) valid = false;
// [ℹ] validation [2] - length
if (usernameInput.length < 3) valid = false;
// [ℹ] validation [3] - only-numbers
if (/^\d+$/.test(usernameInput)) valid = false;
// [ℹ] validation [4] - has a space
if (/\s/g.test(usernameInput)) valid = false;
// [ℹ] validation [5] - has special char
let format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
if (format.test(usernameInput)) valid = false;
// [ℹ] return;
return valid;
}
// TODO:IMPORTANT update wallet address (+connect/disconnect)
// -> connect to MetaMask and retrieve data
// -> update Firestore: wallet-id + providers for the target user
// -> display on Moralis/Users of request made
Expand Down
9 changes: 6 additions & 3 deletions src/routes/u/[view]/[lang=lang]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
COMPONENT JS (w/ TS)
=================-->
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import AccountSettingsBoard from '$lib/components/page/profile/Widget-AccountSettings.svelte';
import DashboardWidget from '$lib/components/page/profile/Widget-Dashboard.svelte';
import UserMenu from '$lib/components/page/profile/Widget-MenuOpt.svelte';
import type { REDIS_CACHE_SINGLE_profile_translation } from '$lib/models/profile/account-setting/types';
import { userBetarenaSettings } from '$lib/store/user-settings';
import { dlogv2, PR_P_STY, PR_P_TAG, PR_P_TOG } from '$lib/utils/debug';
import type { PageData } from '../$types';
Expand Down Expand Up @@ -38,13 +40,14 @@ COMPONENT JS (w/ TS)
// ~~~~~~~~~~~~~~~~~~~~~
// TODO: have this check on the navbar directly
// $: if ($userBetarenaSettings?.user == undefined) {
// goto('/')
// }
$: if ($userBetarenaSettings?.user == undefined) {
goto('/')
}
// ~~~~~~~~~~~~~~~~~~~~~
// VIEWPORT CHANGES
// ~~~~~~~~~~~~~~~~~~~~~
</script>

<!--===============
Expand Down

0 comments on commit 5bd24e1

Please sign in to comment.