Last active
December 25, 2018 06:57
-
-
Save leejh3224/f8b7357c6d19897b9b418ccf4a2df67a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* functions.auth.user().onCreate is triggered when | |
* 1. register via email/password | |
* 2. first logIn via 3rd party auth providers (Facebook, Google, etc) | |
* 3. developer creates new account via Firebase Admin SDK | |
* 4. first logIn as anonymousUser | |
* | |
* What this functions does is that when onCreate method is triggered, | |
* we create `newUser` with necessary information including uid or displayName from given user | |
* And then save it in our database | |
*/ | |
export const onUserCreate = functions.auth.user().onCreate(user => { | |
const dayInMs = 24 * 60 * 60 * 1000; | |
// Think of this as user of simple game app | |
// you have daily goals(quests) and you earn points by finishing them | |
const newUser = { | |
...user, | |
points: 0, | |
questType: "kill_monster", | |
questGoal: 30, | |
questActual: 10, | |
// valid for next 24 hours | |
questValidUntil: Date.now() + dayInMs | |
}; | |
return admin | |
.database() | |
.ref(`/users/${user.uid}`) | |
.set(newUser); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment