aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--backend/.gitignore1
-rw-r--r--backend/index.js77
-rw-r--r--backend/package.json2
-rw-r--r--backend/yarn.lock20
-rw-r--r--package.json1
-rw-r--r--src/firebase.js17
-rw-r--r--yarn.lock5
8 files changed, 100 insertions, 24 deletions
diff --git a/.gitignore b/.gitignore
index a547bf3..a120b79 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,6 +8,7 @@ pnpm-debug.log*
lerna-debug.log*
node_modules
+.env
dist
dist-ssr
*.local
diff --git a/backend/.gitignore b/backend/.gitignore
index a547bf3..8c17ed9 100644
--- a/backend/.gitignore
+++ b/backend/.gitignore
@@ -9,6 +9,7 @@ lerna-debug.log*
node_modules
dist
+.env
dist-ssr
*.local
diff --git a/backend/index.js b/backend/index.js
index d53b07d..5bcc507 100644
--- a/backend/index.js
+++ b/backend/index.js
@@ -2,53 +2,84 @@ const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
-const cors = require('cors'); // Import CORS
+const admin = require('firebase-admin');
+const cors = require('cors'); // Import the cors package
+require('dotenv').config(); // Load environment variables from .env
const app = express();
app.use(bodyParser.json());
-app.use(cors()); // Use the CORS middleware
-const users = []; // In-memory user storage, replace with a database in production
+// Enable CORS for all origins
+app.use(cors());
+
+// Firebase Admin SDK setup
+const serviceAccount = {
+ type: process.env.FIREBASE_TYPE,
+ project_id: process.env.FIREBASE_PROJECT_ID,
+ private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
+ private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
+ client_email: process.env.FIREBASE_CLIENT_EMAIL,
+ client_id: process.env.FIREBASE_CLIENT_ID,
+ auth_uri: process.env.FIREBASE_AUTH_URI,
+ token_uri: process.env.FIREBASE_TOKEN_URI,
+ auth_provider_x509_cert_url: process.env.FIREBASE_AUTH_PROVIDER_X509_CERT_URL,
+ client_x509_cert_url: process.env.FIREBASE_CLIENT_X509_CERT_URL
+};
+
+admin.initializeApp({
+ credential: admin.credential.cert(serviceAccount)
+});
+
+const db = admin.firestore();
// Secret key for JWT
-const JWT_SECRET = 'your_jwt_secret_key';
+const JWT_SECRET = 'your_jwt_secret_key'; // Replace with your actual JWT secret key
// Register route
app.post('/register', async (req, res) => {
const { username, password } = req.body;
- console.log('Register endpoint called with:', { username, password }); // Log the input
-
if (!username || !password) {
return res.status(400).send('Username and password are required');
}
const hashedPassword = await bcrypt.hash(password, 10);
- users.push({ username, password: hashedPassword });
-
- res.status(201).send('User registered');
+ // Save user to Firebase Firestore
+ try {
+ await db.collection('users').doc(username).set({
+ username,
+ password: hashedPassword
+ });
+ res.status(201).send('User registered');
+ } catch (error) {
+ console.error('Error registering user:', error);
+ res.status(500).send('Error registering user');
+ }
});
// Login route
app.post('/login', async (req, res) => {
const { username, password } = req.body;
- console.log('Login endpoint called with:', { username, password }); // Log the input
-
- const user = users.find((u) => u.username === username);
- if (!user) {
- return res.status(400).send('Invalid username or password');
- }
-
- const isPasswordValid = await bcrypt.compare(password, user.password);
- if (!isPasswordValid) {
- return res.status(400).send('Invalid username or password');
+ try {
+ const userDoc = await db.collection('users').doc(username).get();
+ if (!userDoc.exists) {
+ return res.status(400).send('Invalid username or password');
+ }
+
+ const user = userDoc.data();
+ const isPasswordValid = await bcrypt.compare(password, user.password);
+ if (!isPasswordValid) {
+ return res.status(400).send('Invalid username or password');
+ }
+
+ const token = jwt.sign({ username: user.username }, JWT_SECRET, { expiresIn: '1h' });
+ res.json({ token });
+ } catch (error) {
+ console.error('Error during login:', error);
+ res.status(500).send('Error during login');
}
-
- const token = jwt.sign({ username: user.username }, JWT_SECRET, { expiresIn: '1h' });
-
- res.json({ token });
});
const PORT = 5000;
diff --git a/backend/package.json b/backend/package.json
index 0614820..f83b67a 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -6,6 +6,8 @@
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
+ "cors": "^2.8.5",
+ "dotenv": "^16.4.5",
"express": "^4.19.2",
"firebase-admin": "^12.2.0",
"jsonwebtoken": "^9.0.2"
diff --git a/backend/yarn.lock b/backend/yarn.lock
index b8840eb..09b40bf 100644
--- a/backend/yarn.lock
+++ b/backend/yarn.lock
@@ -485,6 +485,14 @@ cookie@0.6.0:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.6.0.tgz#2798b04b071b0ecbff0dbb62a505a8efa4e19051"
integrity sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==
+cors@^2.8.5:
+ version "2.8.5"
+ resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
+ integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
+ dependencies:
+ object-assign "^4"
+ vary "^1"
+
debug@2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -523,6 +531,11 @@ destroy@1.2.0:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.2.0.tgz#4803735509ad8be552934c67df614f94e66fa015"
integrity sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==
+dotenv@^16.4.5:
+ version "16.4.5"
+ resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
+ integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==
+
duplexify@^4.0.0, duplexify@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-4.1.3.tgz#a07e1c0d0a2c001158563d32592ba58bddb0236f"
@@ -1111,6 +1124,11 @@ node-forge@^1.3.1:
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.1.tgz#be8da2af243b2417d5f646a770663a92b7e9ded3"
integrity sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==
+object-assign@^4:
+ version "4.1.1"
+ resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
+ integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==
+
object-hash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/object-hash/-/object-hash-3.0.0.tgz#73f97f753e7baffc0e2cc9d6e079079744ac82e9"
@@ -1425,7 +1443,7 @@ uuid@^9.0.0, uuid@^9.0.1:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30"
integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==
-vary@~1.1.2:
+vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
diff --git a/package.json b/package.json
index 369ea1b..68d5a31 100644
--- a/package.json
+++ b/package.json
@@ -12,6 +12,7 @@
"dependencies": {
"axios": "^1.7.2",
"cors": "^2.8.5",
+ "dotenv": "^16.4.5",
"firebase-admin": "^12.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
diff --git a/src/firebase.js b/src/firebase.js
new file mode 100644
index 0000000..b875eb9
--- /dev/null
+++ b/src/firebase.js
@@ -0,0 +1,17 @@
+import { initializeApp } from "firebase/app";
+import { getAnalytics } from "firebase/analytics";
+
+const firebaseConfig = {
+ apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
+ authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
+ projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
+ storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
+ messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
+ appId: process.env.REACT_APP_FIREBASE_APP_ID,
+ measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID
+};
+
+const app = initializeApp(firebaseConfig);
+const analytics = getAnalytics(app);
+
+export { app, analytics };
diff --git a/yarn.lock b/yarn.lock
index 9b9f71c..02ca203 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1059,6 +1059,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"
+dotenv@^16.4.5:
+ version "16.4.5"
+ resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
+ integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==
+
duplexify@^4.0.0, duplexify@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-4.1.3.tgz#a07e1c0d0a2c001158563d32592ba58bddb0236f"