Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase config #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ FACEBOOK_CLIENT_SECRET=
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=

FIREBASE_API_KEY=
FIREBASE_AUTH_DOMAIN=
FIREBASE_DATABASE_URL=
FIREBASE_PROJECT_ID=
FIREBASE_STORAGE_BUCKET=
FIREBASE_MESSAGING_SENDER_ID=
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=

5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "noteups-web-original-backend"
}
}
20 changes: 20 additions & 0 deletions .github/workflows/firebase-hosting-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on merge
'on':
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci && npm run build
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_NOTEUPS_WEB_ORIGINAL_BACKEND }}'
channelId: live
projectId: noteups-web-original-backend
17 changes: 17 additions & 0 deletions .github/workflows/firebase-hosting-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools

name: Deploy to Firebase Hosting on PR
'on': pull_request
jobs:
build_and_preview:
if: '${{ github.event.pull_request.head.repo.full_name == github.repository }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci && npm run build
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_NOTEUPS_WEB_ORIGINAL_BACKEND }}'
projectId: noteups-web-original-backend
7 changes: 7 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
}
}
34 changes: 34 additions & 0 deletions firebase.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Import the functions you need from the SDKs you need
import { getAnalytics } from "firebase/analytics";
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getDatabase } from "firebase/database";
import { getFirestore } from "firebase/firestore";
import { getFunctions } from "firebase/functions";
import { getStorage } from "firebase/storage";

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.FIREBASE_DATABASE_URL,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
measurementId: process.env.FIREBASE_MEASUREMENT_ID,
};

// Initialize Firebase
export const firebaseApp = initializeApp(firebaseConfig);
export const firebaseAnalytics = getAnalytics(app);
export const firebaseAuth = getAuth(app);
export const firebaseDb = getFirestore(app);
export const firebaseRealtimeDb = getDatabase(app);
export const firebaseStorage = getStorage(app);
export const firebaseFunctions = getFunctions(app);

41 changes: 41 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
],
"database": {
"rules": "database.rules.json"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"storage": {
"rules": "storage.rules"
}
}
182 changes: 182 additions & 0 deletions firebase/FirebaseAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
const firebase = require("firebase/app");
const { firebaseAuth, firebaseDb } = require("../firebase.config");

const auth = firebaseAuth;
const db = firebaseDb;

// Google Sign In
const googleProvider = new firebase.auth.GoogleAuthProvider();
googleProvider.setCustomParameters({ prompt: "select_account" });

const signInWithGoogle = async () => {
try {
const result = await auth.signInWithPopup(googleProvider);
const user = result.user;
const { displayName, email, photoURL } = user;

// Check if user already exists in Firestore
const userRef = db.collection("users").doc(user.uid);
const doc = await userRef.get();
if (!doc.exists) {
// Create new user in Firestore
await userRef.set({
name: displayName,
email,
photoURL,
});
}
} catch (error) {
console.error(error);
}
};

// Facebook Sign In
const facebookProvider = new firebase.auth.FacebookAuthProvider();

const signInWithFacebook = async () => {
try {
const result = await auth.signInWithPopup(facebookProvider);
const user = result.user;
const { displayName, email, photoURL } = user;

// Check if user already exists in Firestore
const userRef = db.collection("users").doc(user.uid);
const doc = await userRef.get();
if (!doc.exists) {
// Create new user in Firestore
await userRef.set({
name: displayName,
email,
photoURL,
});
}
} catch (error) {
console.error(error);
}
};

// GitHub Sign In
const githubProvider = new firebase.auth.GithubAuthProvider();

const signInWithGithub = async () => {
try {
const result = await auth.signInWithPopup(githubProvider);
const user = result.user;
const { displayName, email, photoURL } = user;

// Check if user already exists in Firestore
const userRef = db.collection("users").doc(user.uid);
const doc = await userRef.get();
if (!doc.exists) {
// Create new user in Firestore
await userRef.set({
name: displayName,
email,
photoURL,
});
}
} catch (error) {
console.error(error);
}
};

// Email and Password Sign In
const signInWithEmailAndPassword = async (email, password) => {
try {
const result = await auth.signInWithEmailAndPassword(email, password);
const user = result.user;
const { displayName, email, photoURL } = user;

// Check if user already exists in Firestore
const userRef = db.collection("users").doc(user.uid);
const doc = await userRef.get();
if (!doc.exists) {
// Create new user in Firestore
await userRef.set({
name: displayName,
email,
photoURL,
});
}
} catch (error) {
console.error(error);
}
};

// Link Google Account
const linkWithGoogle = async () => {
try {
const result = await auth.signInWithPopup(googleProvider);
const credential = result.credential;
await auth.currentUser.linkWithCredential(credential);

// Update Firestore with additional data from Google account
const { displayName, email, photoURL } = result.user;
await db
.collection("users")
.doc(auth.currentUser.uid)
.update({
name: displayName || "",
email: email || "",
photoURL: photoURL || "",
});
} catch (error) {
if (error.code === "auth/credential-already-in-use") {
// The Google account is already linked to another user
// You can handle this case by merging the accounts
console.error(error.message);
}
}
};

// Link Facebook Account
const linkWithFacebook = async () => {
try {
const result = await auth.signInWithPopup(facebookProvider);
const credential = result.credential;
await auth.currentUser.linkWithCredential(credential);

// Update Firestore with additional data from Facebook account
const { displayName, email, photoURL } = result.user;
await db
.collection("users")
.doc(auth.currentUser.uid)
.update({
name: displayName || "",
email: email || "",
photoURL: photoURL || "",
});
} catch (error) {
if (error.code === "auth/credential-already-in-use") {
// The Facebook account is already linked to another user
// You can handle this case by merging the accounts
console.error(error.message);
}
}
};

// Link GitHub Account
const linkWithGithub = async () => {
try {
const result = await auth.signInWithPopup(githubProvider);
const credential = result.credential;
await auth.currentUser.linkWithCredential(credential);

// Update Firestore with additional data from GitHub account
const { displayName, email, photoURL } = result.user;
await db
.collection("users")
.doc(auth.currentUser.uid)
.update({
name: displayName || "",
email: email || "",
photoURL: photoURL || "",
});
} catch (error) {
if (error.code === "auth/credential-already-in-use") {
// The GitHub account is already linked to another user
// You can handle this case by merging the accounts
console.error(error.message);
}
}
};
4 changes: 4 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"indexes": [],
"fieldOverrides": []
}
8 changes: 8 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
28 changes: 28 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
env: {
es6: true,
node: true,
},
parserOptions: {
"ecmaVersion": 2018,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
"no-restricted-globals": ["error", "name", "length"],
"prefer-arrow-callback": "error",
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
},
overrides: [
{
files: ["**/*.spec.*"],
env: {
mocha: true,
},
rules: {},
},
],
globals: {},
};
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
19 changes: 19 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Import function triggers from their respective submodules:
*
* const {onCall} = require("firebase-functions/v2/https");
* const {onDocumentWritten} = require("firebase-functions/v2/firestore");
*
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/

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

// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started

// exports.helloWorld = onRequest((request, response) => {
// logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
Loading