Skip to content

Commit

Permalink
Merge pull request #136 from FahimFBA/116-bug-add-2fa-two-factor-auth…
Browse files Browse the repository at this point in the history
…entication-features-like-authy

116 bug add 2fa two factor authentication features like authy
  • Loading branch information
JoyShaheb committed Dec 3, 2023
2 parents 0b025c4 + 9736823 commit f233080
Show file tree
Hide file tree
Showing 18 changed files with 7,446 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "assistme-f18bb"
}
}
48 changes: 48 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
}
],
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"database": {
"port": 9000
},
"pubsub": {
"port": 8085
},
"storage": {
"port": 9199
},
"ui": {
"enabled": true
},
"singleProjectMode": true,
"auth": {
"port": 9099
}
},
"storage": {
"rules": "storage.rules"
}
}
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": []
}
9 changes: 9 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rules_version = '2';

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
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/
86 changes: 86 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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
*/

import { onRequest } from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";
import admin from "firebase-admin";
admin.initializeApp();
import speakeasy from "speakeasy";
import { onDocumentWritten } from "firebase-functions/v2/firestore";

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

export const registerUser = onRequest((request, response) => {
var secret = speakeasy.generateSecret({ length: 20 });
logger.info("Hello logs!", { structuredData: true });
response.json({ msg: "Hello from Fireb", secret });
});

export const verifyUser = onRequest((request, response) => {
const { secret, token } = request.body;
const verified = speakeasy.totp.verify({
secret: secret.base32,
encoding: "base32",
token,
});
response.json({ verified });
});

export const updateDocumentCounts = onDocumentWritten(
"documents/{documentsId}",
async () => {
const db = admin.firestore();
const documentCountRef = db.collection("documentCount").doc("count");

// Get the total count of documents in the "documents" collection
const documentsCollectionRef = db.collection("documents");
const documentsQuerySnapshot = await documentsCollectionRef.get();
const totalCount = documentsQuerySnapshot.size;

// Update the "count" field in the "documentCount" collection
await documentCountRef.set({ count: totalCount });

logger.info(totalCount, {
structuredData: true,
});
},
);

export const addAuthSecret = onDocumentWritten(
"users/{usersId}",
async (snapshot) => {
const db = admin.firestore();
const singleUserRef = db
.collection("authSecrets")
.doc(snapshot?.params?.usersId);
const getAllUserData = await singleUserRef.get();

// Generate a secret
const secret = speakeasy.generateSecret({ length: 20 });

// console.log(
// "Updating user document with secret:",
// snapshot?.params?.usersId,
// getAllUserData.data(),
// );

if (getAllUserData.data()?.secret) {
return;
} else {
await singleUserRef.set({ secret });
}

logger.info("User Created/ Updated", {
structuredData: true,
});
},
);

0 comments on commit f233080

Please sign in to comment.