createUser: create private user; detect multiple signups
This commit is contained in:
parent
f74af0f5b7
commit
9d86c37e53
|
@ -1,13 +1,18 @@
|
|||
import * as functions from 'firebase-functions'
|
||||
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 { randomString } from '../../common/util/random'
|
||||
|
||||
export const createUser = functions
|
||||
.runWith({ minInstances: 1 })
|
||||
.https.onCall(async (_, context) => {
|
||||
.https.onCall(async (data: { deviceToken?: string }, context) => {
|
||||
const userId = context?.auth?.uid
|
||||
if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||
|
||||
|
@ -34,12 +39,22 @@ export const createUser = functions
|
|||
|
||||
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 = {
|
||||
id: userId,
|
||||
name,
|
||||
username,
|
||||
avatarUrl,
|
||||
balance: STARTING_BALANCE,
|
||||
balance,
|
||||
createdTime: Date.now(),
|
||||
totalPnLCached: 0,
|
||||
creatorVolumeCached: 0,
|
||||
|
@ -48,6 +63,14 @@ export const createUser = functions
|
|||
await firestore.collection('users').doc(userId).create(user)
|
||||
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 }
|
||||
})
|
||||
|
||||
|
@ -60,3 +83,21 @@ const cleanUsername = (name: string) => {
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { getFunctions, httpsCallable } from 'firebase/functions'
|
||||
import { User } from '../../../common/user'
|
||||
import { randomString } from '../../../common/util/random'
|
||||
|
||||
const functions = getFunctions()
|
||||
|
||||
|
@ -13,7 +14,14 @@ export const resolveMarket = cloudFunction('resolveMarket')
|
|||
|
||||
export const sellBet = cloudFunction('sellBet')
|
||||
|
||||
export const createUser: () => Promise<User | null> = () =>
|
||||
cloudFunction('createUser')({})
|
||||
export const createUser: () => Promise<User | null> = () => {
|
||||
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)
|
||||
.catch(() => null)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user