diff --git a/components/input/Input.jsx b/components/input/Input.jsx index 015b8a2..760acd4 100644 --- a/components/input/Input.jsx +++ b/components/input/Input.jsx @@ -2,7 +2,7 @@ import React from "react"; import styles from "../../styles/components/Input.module.css"; import MoonLoader from "react-spinners/MoonLoader"; -import { getQuote, getRandomText } from "../../util/helper.js"; +import { getQuote, getRandomText, randomString } from "../../util/helper.js"; import { initSounds, playSound } from "../../util/sound/sound-handler.js"; import { calculate } from "../../util/logic/type-logic.js"; import { db, auth } from "../../util/firebase/firebase.js"; @@ -164,12 +164,14 @@ export class Input extends React.Component { ); await auth.onAuthStateChanged(async (authUser) => { + let id = randomString(); if (authUser !== null) { await db .collection("stats") .add({ displayName: authUser.displayName, userUid: authUser.uid, + gameId: id, photo: authUser.photoURL, wpm: calculated.wpm, accuracy: calculated.accuracy, @@ -178,7 +180,16 @@ export class Input extends React.Component { time: this.state.time, timeStamp: Date.now(), }) - .then(() => this.props.finish()); + .then(async () => { + let userDoc = await db.collection("users").doc(authUser.uid); + let stats = (await userDoc.get()).data().stats + stats.push(id) + + await userDoc.update({ + stats: stats, + }); + this.props.finish(); + }); } else this.props.finish(); }); } diff --git a/pages/auth/register.js b/pages/auth/register.js index e6b617a..d861fdd 100644 --- a/pages/auth/register.js +++ b/pages/auth/register.js @@ -29,6 +29,17 @@ export default function Register() { displayName: email.split("@")[0], photoURL: "https://firebasestorage.googleapis.com/v0/b/typo-io.appspot.com/o/default-profile.png?alt=media&token=984243f8-947d-4f41-945a-e9fbd3d0825f" }) + + await db.collection("users").doc(user.uid).set({ + displayName: user.displayName, + photoURL: user.photoURL, + banned: false, + badges: [], + stats: [], + nameChanges: [], + pictureChanges: [], + keyboard: "😭" + }) }) .catch((error) => { var errorMessage = error.message; diff --git a/util/helper.js b/util/helper.js index 4b6780b..a250f3d 100644 --- a/util/helper.js +++ b/util/helper.js @@ -33,6 +33,22 @@ export async function getRandomText(language) { return response } +/** + * Creates a random string + * @param {number} length + * @returns random string + */ +export function randomString(length = 64) { + let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-'; + + let str = ''; + for (let i = 0; i < length; i++) { + str += chars.charAt(Math.floor(Math.random() * chars.length)); + } + + return str; +} + /** * Packages a array into parts * @param {strign} text