createUser: create private user; detect multiple signups

This commit is contained in:
mantikoros 2022-01-18 16:32:02 -06:00
parent f74af0f5b7
commit 9d86c37e53
2 changed files with 54 additions and 5 deletions

View File

@ -1,13 +1,18 @@
import * as functions from 'firebase-functions' import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { STARTING_BALANCE, User } from '../../common/user' import {
PrivateUser,
STARTING_BALANCE,
SUS_STARTING_BALANCE,
User,
} from '../../common/user'
import { getUser, getUserByUsername } from './utils' import { getUser, getUserByUsername } from './utils'
import { randomString } from '../../common/util/random' import { randomString } from '../../common/util/random'
export const createUser = functions export const createUser = functions
.runWith({ minInstances: 1 }) .runWith({ minInstances: 1 })
.https.onCall(async (_, context) => { .https.onCall(async (data: { deviceToken?: string }, context) => {
const userId = context?.auth?.uid const userId = context?.auth?.uid
if (!userId) return { status: 'error', message: 'Not authorized' } if (!userId) return { status: 'error', message: 'Not authorized' }
@ -34,12 +39,22 @@ export const createUser = functions
const avatarUrl = fbUser.photoURL const avatarUrl = fbUser.photoURL
const { deviceToken } = data
const deviceUsedBefore =
!deviceToken || (await isPrivateUserWithDeviceToken(deviceToken))
const ipAddress = context.rawRequest.ip
const ipCount = ipAddress ? await numberUsersWithIp(ipAddress) : 0
const balance =
deviceUsedBefore || ipCount > 2 ? SUS_STARTING_BALANCE : STARTING_BALANCE
const user: User = { const user: User = {
id: userId, id: userId,
name, name,
username, username,
avatarUrl, avatarUrl,
balance: STARTING_BALANCE, balance,
createdTime: Date.now(), createdTime: Date.now(),
totalPnLCached: 0, totalPnLCached: 0,
creatorVolumeCached: 0, creatorVolumeCached: 0,
@ -48,6 +63,14 @@ export const createUser = functions
await firestore.collection('users').doc(userId).create(user) await firestore.collection('users').doc(userId).create(user)
console.log('created user', username, 'firebase id:', userId) console.log('created user', username, 'firebase id:', userId)
const privateUser: PrivateUser = {
email,
initialIpAddress: ipAddress,
initialDeviceToken: deviceToken,
}
await firestore.collection('private-users').doc(userId).create(privateUser)
return { status: 'success', user } return { status: 'success', user }
}) })
@ -60,3 +83,21 @@ const cleanUsername = (name: string) => {
} }
const firestore = admin.firestore() const firestore = admin.firestore()
const isPrivateUserWithDeviceToken = async (deviceToken: string) => {
const snap = await firestore
.collection('private-users')
.where('initialDeviceToken', '==', deviceToken)
.get()
return !snap.empty
}
const numberUsersWithIp = async (ipAddress: string) => {
const snap = await firestore
.collection('private-users')
.where('initialIpAddress', '==', ipAddress)
.get()
return snap.docs.length
}

View File

@ -1,5 +1,6 @@
import { getFunctions, httpsCallable } from 'firebase/functions' import { getFunctions, httpsCallable } from 'firebase/functions'
import { User } from '../../../common/user' import { User } from '../../../common/user'
import { randomString } from '../../../common/util/random'
const functions = getFunctions() const functions = getFunctions()
@ -13,7 +14,14 @@ export const resolveMarket = cloudFunction('resolveMarket')
export const sellBet = cloudFunction('sellBet') export const sellBet = cloudFunction('sellBet')
export const createUser: () => Promise<User | null> = () => export const createUser: () => Promise<User | null> = () => {
cloudFunction('createUser')({}) let deviceToken = window.localStorage.getItem('device-token')
if (!deviceToken) {
deviceToken = randomString()
window.localStorage.setItem('device-token', deviceToken)
}
return cloudFunction('createUser')({ deviceToken })
.then((r) => (r.data as any)?.user || null) .then((r) => (r.data as any)?.user || null)
.catch(() => null) .catch(() => null)
}