Created
March 6, 2020 03:43
-
-
Save ajurcevic/1598820ac6c8183b2d56296e47e30b16 to your computer and use it in GitHub Desktop.
Cloud functions to manage user pin code
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
// orignal source | |
// https://github.com/lemberg/connfa-ios/blob/master/Cloud-Functions/functions/index.js | |
// Added region and updated packages for Cloud Functions to the following: | |
// "firebase-admin": "^8.9.0" | |
// "firebase-functions": "^3.3.0" | |
// "node": "10" | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const region = "asia-northeast1"; | |
admin.initializeApp(); | |
const ref = admin.database().ref(); | |
const availablePins = '/availablePins/'; | |
const pins = '/pins/'; | |
const unspecifiedUsers = '/unspecifiedUsers/'; | |
var countOfPINDigits = 4 | |
exports.anonUserEnter = functions.region(region).auth.user().onCreate(user => { | |
var anon = user.uid; | |
console.log("user!: " + anon); | |
generateUnquePIN(anon); | |
return user; | |
}); | |
exports.syncFilePaths = functions.region(region).storage.object().onFinalize(object => { | |
const filePath = object.name; | |
const fileId = filePath.replace('.',''); | |
console.log(filePath); | |
if (object.resourceState === 'not_exists') { | |
console.log("Delete file path"); | |
ref.child(fileId).set(null); | |
} else { | |
console.log("Add file path"); | |
ref.child(fileId).set(filePath); | |
} | |
}); | |
function generateUnquePIN(userID) { | |
ref.child(availablePins).limitToLast(1).once("value", function (snapshot) { | |
if (snapshot.numChildren() > 0) { | |
var pinKey = Object.keys(snapshot.val())[0]; | |
var pinValue = snapshot.val()[pinKey]; | |
console.log("new pin for KEY = " + pinKey + ' VALUE = ' + pinValue); | |
ref.child(availablePins + pinKey).remove(); | |
saveGeneratedPIN(userID, pinValue); | |
if (pinKey.toString() === '0') { | |
console.log("POOL IS EMPTY"); | |
let digitsCount = pinValue.toString().length + 1; | |
processInCaseOfEmptyPINsPool(digitsCount); | |
} | |
} else { | |
console.log("POOL IS EMPTY"); | |
processInCaseOfEmptyPINsPool(); | |
processUserWithoutPin(userID); | |
} | |
}).catch(function (reason) { | |
console.log("PROBLEM! " + reason); | |
processUserWithUnspecifiedError(userID, reason); | |
}) | |
} | |
function processInCaseOfEmptyPINsPool(digitsCount = countOfPINDigits) { | |
countOfPINDigits = digitsCount | |
generatePinsArray(countOfPINDigits) | |
} | |
function saveGeneratedPIN(userID, pin) { | |
ref.child(pins).child(pin).set(true); | |
const userRef = ref.child('/users/' + userID); | |
userRef.set({ | |
ownPin: pin | |
}); | |
console.log("Anon user " + userID + " registered with pin = " + pin); | |
} | |
/** | |
* DATA GENERATOR SECTION | |
*/ | |
exports.pinsDataGenerator = functions.region(region).https.onRequest((req, res) => { | |
res.send("{ Generating! }"); | |
generatePinsArray(); | |
}); | |
function saveGeneratedPINsDataToDB(dataArray) { | |
dataArray.forEach(function (pin, i) { | |
ref.child(availablePins + i).set(pin); | |
}); | |
} | |
function generatePinsArray() { | |
let arrayCount = Math.pow(10, countOfPINDigits); | |
var numbers = Array(arrayCount).fill().map((e, i) => formatDigit(i + 1)); | |
shuffleArray(numbers); | |
console.log("CONGRATS!"); | |
saveGeneratedPINsDataToDB(numbers); | |
} | |
/** | |
* Shuffle array in random way | |
*/ | |
function shuffleArray(array) { | |
for (let i = array.length - 1; i > 0; i--) { | |
let j = Math.floor(Math.random() * (i + 1)); | |
[array[i], array[j]] = [array[j], array[i]]; | |
} | |
} | |
/** | |
* FORMAT Int to 4-Digits String | |
*/ | |
function formatDigit(num, n = countOfPINDigits) { | |
digit = ''; | |
if (typeof n === 'undefined' || num.toString().length === 4) | |
return num.toString(); | |
for (i = 1; i < n; i++) { | |
if (num < (1 + Array(i + 1).join("0"))) | |
digit += '0'; | |
} | |
digit = digit + num; | |
return digit; | |
} | |
/** | |
* In case of: 'The error occurs. PIN is not generated.' | |
*/ | |
function processUserWithoutPin(userID) { | |
let UserWithoutPinErrorCode = "404/"; | |
const noPinError = "PIN is not specified cause pool is empty!"; | |
ref.child(unspecifiedUsers + UserWithoutPinErrorCode + userID).set(noPinError); | |
} | |
/** | |
* In case of: 'The error occurs. Internal server error.' | |
*/ | |
function processUserWithUnspecifiedError(userID, errorReason) { | |
let UserWithoutPinErrorCode = "500/"; | |
ref.child(unspecifiedUsers + UserWithoutPinErrorCode + userID).set(errorReason); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment