From a9eabb0b14739778987e30f8491637ed0be340e5 Mon Sep 17 00:00:00 2001
From: Federico Hurtado <fed_home@Federicos-Mac-mini.local>
Date: Wed, 23 Oct 2024 11:55:52 -0400
Subject: [PATCH] Bypass cas authentication when using localhost

---
 backend/.env                      |  2 +-
 backend/config/auth/auth.login.js | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/backend/.env b/backend/.env
index b2f01e3..ce7e797 100644
--- a/backend/.env
+++ b/backend/.env
@@ -5,4 +5,4 @@ DB_PASSWORD=corps_db_password
 DB_NAME=corps_directory_db
 PORT=3000
 
-FRONTEND_URL=localhost:8080
\ No newline at end of file
+FRONTEND_URL=http://localhost:8080
\ No newline at end of file
diff --git a/backend/config/auth/auth.login.js b/backend/config/auth/auth.login.js
index 022d882..ebf5d6e 100644
--- a/backend/config/auth/auth.login.js
+++ b/backend/config/auth/auth.login.js
@@ -9,6 +9,28 @@ const casLogin = function (req, res, next) {
   // hard-coded for now.
   const JWT_SECRET = "secret_key";
 
+  // if on localhost, accept the authentication and give a key
+  console.log("user: ", process.env.DB_USER);
+
+  if (process.env.DB_USER === "corps_directory_dev") {
+    // payload for the JWT token
+    const payload = {
+      id: 1,
+      role: "admin",
+      username: "localhostuser",
+    };
+
+    // sign the token with secret key
+    const token = jwt.sign(payload, JWT_SECRET, { expiresIn: "1h" });
+
+    console.log("sending token for use in localhost");
+
+    console.log("Frontend: ", process.env.FRONTEND_URL);
+
+    res.redirect(`${process.env.FRONTEND_URL}/home?token=${token}`);
+    return;
+  }
+
   // call the authenticate function with CAS
   passport.authenticate("cas", function (err, user, info) {
     if (err) {
-- 
GitLab