-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aravind V Nair <[email protected]>
- Loading branch information
1 parent
8f29bfe
commit e5851de
Showing
7 changed files
with
556 additions
and
196 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,34 @@ | ||
{ | ||
"functions": { | ||
"predeploy": [ | ||
"npm --prefix \"$RESOURCE_DIR\" run lint" | ||
] | ||
}, | ||
"hosting": { | ||
"public": "public", | ||
"ignore": [ | ||
"firebase.json", | ||
"**/.*", | ||
"**/node_modules/**" | ||
] | ||
} | ||
"hosting": { | ||
"public": "public", | ||
"rewrites": [ | ||
{ | ||
"source": "**", | ||
"function": "app" | ||
} | ||
], | ||
"headers": [ | ||
{ | ||
"source": "**/*.@(eot|otf|ttf|ttc|woff|woff2|css)", | ||
"headers": [ | ||
{ | ||
"key": "Access-Control-Allow-Origin", | ||
"value": "*" | ||
} | ||
] | ||
}, | ||
{ | ||
"source": "**/*.@(js|css|json|jpg|jpeg|gif|png|ico)", | ||
"headers": [ | ||
{ | ||
"key": "Cache-Control", | ||
"value": "max-age=604800" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"functions": { | ||
"predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint"] | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,215 @@ | ||
const functions = require('firebase-functions'); | ||
|
||
// // Create and Deploy Your First Cloud Functions | ||
// // https://firebase.google.com/docs/functions/write-firebase-functions | ||
// | ||
// exports.helloWorld = functions.https.onRequest((request, response) => { | ||
// response.send("Hello from Firebase!"); | ||
// }); | ||
const functions = require("firebase-functions"), | ||
express = require("express"), | ||
app = express(), | ||
bodyParser = require("body-parser"), | ||
admin = require("firebase-admin"), | ||
cookieParser = require("cookie-parser"); | ||
|
||
/*=============================================>>>>> | ||
= init and config = | ||
===============================================>>>>>*/ | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.applicationDefault() | ||
}); | ||
app.use(bodyParser.json()); | ||
app.use( | ||
bodyParser.urlencoded({ | ||
extended: true | ||
}) | ||
); | ||
app.use(cookieParser()); | ||
app.set("views", "./views"); | ||
app.set("view engine", "ejs"); | ||
var db = admin.firestore(); | ||
|
||
/*=============================================>>>>> | ||
= security functions = | ||
===============================================>>>>>*/ | ||
|
||
function checkCookieMiddleware(req, res, next) { | ||
const sessionCookie = req.cookies.__session || ""; | ||
admin | ||
.auth() | ||
.verifySessionCookie(sessionCookie, true) | ||
.then((decodedClaims) => { | ||
req.decodedClaims = decodedClaims; | ||
next(); | ||
return; | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
res.redirect("/login"); | ||
}); | ||
} | ||
function setCookie(idToken, res) { | ||
const expiresIn = 60 * 60 * 24 * 5 * 1000; | ||
admin | ||
.auth() | ||
.createSessionCookie(idToken, { expiresIn }) | ||
.then( | ||
(sessionCookie) => { | ||
const options = { | ||
maxAge: expiresIn, | ||
httpOnly: true, | ||
secure: false //should be true in prod | ||
}; | ||
res.cookie("__session", sessionCookie, options); | ||
admin | ||
.auth() | ||
.verifyIdToken(idToken) | ||
.then((decodedClaims) => { | ||
res.redirect("/uid"); | ||
return console.log(decodedClaims); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
return; | ||
}, | ||
(error) => { | ||
console.log(error); | ||
res.status(401).send("UNAUTHORIZED REQUEST!"); | ||
} | ||
) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
} | ||
|
||
/*=============================================>>>>> | ||
= basic routes = | ||
===============================================>>>>>*/ | ||
|
||
app.get("/", (req, res) => { | ||
res.render("index"); | ||
}); | ||
app.get("/offline", (req, res) => { | ||
res.render("offline"); | ||
}); | ||
|
||
/*=============================================>>>>> | ||
= legal routes = | ||
===============================================>>>>>*/ | ||
|
||
app.get("/EULA", (req, res) => { | ||
res.render("legal/EULA"); | ||
}); | ||
app.get("/disclaimer", (req, res) => { | ||
res.render("legal/disclaimer"); | ||
}); | ||
app.get("/privacyPolicy", (req, res) => { | ||
res.render("legal/privacyPolicy"); | ||
}); | ||
app.get("/termsConditions", (req, res) => { | ||
res.render("legal/termsConditions"); | ||
}); | ||
|
||
/*=============================================>>>>> | ||
= authentication routes = | ||
===============================================>>>>>*/ | ||
|
||
app.get("/login", (req, res) => { | ||
if (req.cookies.__session) { | ||
res.redirect("/uid"); | ||
} else { | ||
res.render("login"); | ||
} | ||
}); | ||
app.get("/sessionLogin", (req, res) => { | ||
setCookie(req.query.idToken, res); | ||
}); | ||
app.get("/signOut", (req, res) => { | ||
res.clearCookie("__session"); | ||
res.redirect("/login"); | ||
}); | ||
app.get("/uid", checkCookieMiddleware, (req, res) => { | ||
res.send(req.decodedClaims.uid); | ||
}); | ||
|
||
app.post("/onLogin", (req, res) => { | ||
admin | ||
.auth() | ||
.verifyIdToken(req.body.idToken, true) | ||
.then((decodedToken) => { | ||
admin | ||
.auth() | ||
.getUser(decodedToken.uid) | ||
.then((userRecord) => { | ||
console.log( | ||
"Successfully fetched user data:", | ||
userRecord.toJSON() | ||
); | ||
if (userRecord.phoneNumber && userRecord.emailVerified) { | ||
return res.send({ path: "/dashboard" }); | ||
} else if (!userRecord.emailVerified) { | ||
return res.send({ path: "/emailVerification" }); | ||
} else { | ||
return res.send({ path: "/updateProfile" }); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.log("Error fetching user data:", error); | ||
res.send("/login"); | ||
}); | ||
return; | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
res.send("/login"); | ||
}); | ||
}); | ||
app.get("/emailVerification", (req, res) => { | ||
res.render("emailVerification"); | ||
}); | ||
app.get("/updateProfile", (req, res) => { | ||
res.render("updateProfile"); | ||
}); | ||
app.post("/onUpdateProfile", (req, res) => { | ||
admin | ||
.auth() | ||
.updateUser(req.body.uid, { | ||
phoneNumber: "+91" + req.body.phoneNumber, | ||
password: req.body.password, | ||
displayName: req.body.firstName + " " + req.body.lastName, | ||
photoURL: req.body.photoURL | ||
}) | ||
.then((userRecord) => { | ||
console.log("Successfully updated user", userRecord.toJSON()); | ||
return res.redirect("/login"); | ||
}) | ||
.catch((error) => { | ||
console.log("Error updating user:", error); | ||
}); | ||
}); | ||
|
||
/*=============================================>>>>> | ||
= errors = | ||
===============================================>>>>>*/ | ||
|
||
app.use((req, res, next) => { | ||
res.status(404).render("errors/404"); | ||
}); | ||
app.use((req, res, next) => { | ||
res.status(500).render("errors/500"); | ||
}); | ||
|
||
/*=============================================>>>>> | ||
= DO NOT PUT ANYTHING AFTER THIS = | ||
===============================================>>>>>*/ | ||
|
||
exports.app = functions.https.onRequest(app); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
GOOGLE_APPLICATION_CREDENTIALS=./serviceAccountKey.json firebase serve |
Oops, something went wrong.