Skip to content

Commit

Permalink
created user collection
Browse files Browse the repository at this point in the history
  • Loading branch information
BergerAPI committed Apr 10, 2021
1 parent 2ef9260 commit 983babf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
15 changes: 13 additions & 2 deletions components/input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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();
});
}
Expand Down
11 changes: 11 additions & 0 deletions pages/auth/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions util/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

1 comment on commit 983babf

@vercel
Copy link

@vercel vercel bot commented on 983babf Apr 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.