Skip to content

Commit

Permalink
feat: add demo emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-hamel committed Aug 2, 2024
1 parent 7e93dcc commit c725288
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 791 deletions.
7 changes: 5 additions & 2 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"projects": {
"default": "demo-project"
}
"production": "the-scene-social-app"
},
"targets": {},
"etags": {},
"dataconnectEmulatorConfig": {}
}
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ This app is designed to help you find local creatives and collaborators, this ap
## Screenshots

![App Screenshot](docs/assets/App_Example.gif)

## Emulator

firebase emulators:start --project demo-the-scene
13 changes: 3 additions & 10 deletions firebase/firebase.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initializeApp } from "firebase/app";
import { initializeApp, getApp } from "firebase/app";
import {
connectAuthEmulator,
initializeAuth,
Expand All @@ -10,7 +10,6 @@ import Constants from "expo-constants";
import ReactNativeAsyncStorage from "@react-native-async-storage/async-storage";
import { getFunctions, connectFunctionsEmulator } from "firebase/functions";

// Configure Firebase.
const firebaseConfig = {
apiKey: Constants.expoConfig.extra.apiKey,
authDomain: Constants.expoConfig.extra.authDomain,
Expand All @@ -20,15 +19,10 @@ const firebaseConfig = {
appId: Constants.expoConfig.extra.appId,
};

/**
* https://stackoverflow.com/questions/67781589/how-to-setup-a-firebase-demo-project
*
* cant get firestore to work though
*/
// const firebaseDemoConfig = {
// const firebaseConfig = {
// apiKey: "any",
// authDomain: "any",
// projectId: "demo-project", // project name from .firebaserc
// projectId: "demo-the-scene",
// storageBucket: "any",
// messagingSenderId: "any",
// appId: "any",
Expand All @@ -44,7 +38,6 @@ const functions = getFunctions(app);

if (process.env.NODE_ENV === "development") {
console.log("Enabling emulators since in development mode");

connectAuthEmulator(auth, "http://127.0.0.1:9099");
connectFirestoreEmulator(db, "127.0.0.1", 8080);
connectStorageEmulator(storage, "127.0.0.1", 9199);
Expand Down
70 changes: 34 additions & 36 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
//Cloud functions

const { logger } = require("firebase-functions");
const { onRequest } = require("firebase-functions/v2/https");
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
const { onCall } = require("firebase-functions/v2/https");

// The Firebase Admin SDK to access Firestore.
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");

initializeApp();

// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started
const db = getFirestore();
const usersCollection = db.collection("users");

// exports.helloWorld = onRequest((request, response) => {
// logger.info("Hello logs!", { structuredData: true });
// response.send("Hello from Firebase!");
// });
exports.isEmailUsed = onCall(async (request) => {
const email = request.data.email;

exports.isEmailUsed = onRequest(async (request, response) => {
const userEmail = request.query.email;
try {
//get user by email
const usersSnapshot = await getFirestore()
.collection("users")
.where("email", "==", userEmail)
.get();
const snapshot = await usersCollection.where("email", "==", email).get();

//if email exists return true
if (!usersSnapshot.empty) {
response.status(200).send(true);
if (snapshot.empty) {
console.log("No matching email.");
return {
emailUsed: false,
};
}

if (usersSnapshot.empty) {
response.status(200).send(false);
}
return {
emailUsed: true,
};
} catch (error) {
console.error("Error getting users collection:", error);
response.status(500).send("Internal Server Error");

return {
emailUsed: "bad",
};
}
});

exports.isUsernameUsed = onRequest(async (request, response) => {
const username = request.query.username;
exports.isUsernameUsed = onCall(async (request) => {
const username = request.data.username;

try {
//get user by username
const usersSnapshot = await getFirestore()
.collection("users")
//get user by email
const snapshot = await usersCollection
.where("username", "==", username)
.get();

//if username exists return true
if (!usersSnapshot.empty) {
response.status(200).send(true);
if (snapshot.empty) {
console.log("No matching username.");
return {
isUsernameUsed: false,
};
}

if (usersSnapshot.empty) {
response.status(200).send(false);
}
return {
isUsernameUsed: true,
};
} catch (error) {
console.error("Error getting users collection:", error);
response.status(500).send("Internal Server Error");
return {
isUsernameUsed: "bad",
};
}
});
Loading

0 comments on commit c725288

Please sign in to comment.