Skip to content

Commit

Permalink
add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Assios committed Aug 31, 2023
1 parent 7fdd848 commit 83bf5ed
Show file tree
Hide file tree
Showing 5 changed files with 6,558 additions and 0 deletions.
14 changes: 14 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
}
]
}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
45 changes: 45 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import CryptoJS from 'crypto-js';

admin.initializeApp();

const db = admin.firestore();

const initialPlayerStats = {
gamesPlayed: 0,
lastPlayed: "",
currentStreak: 0,
guesses: {
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
failed: 0,
},
};

exports.saveNewUser = functions.auth.user().onCreate(async (user) => {
const uid = user.uid;
const email = user.email || '';
const username = user.displayName || '';

const emailHash = CryptoJS.MD5(email).toString();
const userRef = db.collection('users').doc(uid);
const usernameRef = db.collection('usernames').doc(username);

const batch = db.batch();

batch.set(userRef, {
email,
username,
stats: initialPlayerStats,
emailHash,
lastUpdatedUsername: admin.firestore.FieldValue.serverTimestamp(),
});

batch.set(usernameRef, { uid });

await batch.commit();
});
Loading

1 comment on commit 83bf5ed

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for chessguessr ready!

✅ Preview
https://chessguessr-2letljrs1-assios.vercel.app

Built with commit 83bf5ed.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.