Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d222be7
Add Firebase authentication and user signup functionality to the home…
adityashibu Feb 16, 2025
8ed6191
Update Firebase configuration to use environment variables for sensit…
adityashibu Feb 16, 2025
6dbca39
Implement user login functionality and update Firebase config
adityashibu Feb 16, 2025
2974594
Implement user authentication flow in dashboard and homepage, includi…
adityashibu Feb 16, 2025
0c53f3f
Remove unused profile menu open handler from Navbar component
adityashibu Feb 16, 2025
ed50883
Merge branch 'PowerHouse-Project:main' into auth
adityashibu Feb 16, 2025
ccb0811
Added IDX build options (Should not matter)
adwiiiii Feb 16, 2025
9ac9dc1
Remove unused test file from middleware
adityashibu Feb 18, 2025
38359e2
Refactor dashboard component by commenting out unused authentication …
adityashibu Feb 20, 2025
8343572
Remove commented-out code for AccountMenu in Navbar component
adityashibu Feb 24, 2025
0c93749
Add initial users database with user credentials and roles for base t…
adityashibu Feb 24, 2025
c298d44
Add user data endpoint and corresponding tests for user data retrieval
adityashibu Feb 24, 2025
e9441f1
Add Users component with user list and password authentication dialog
adityashibu Feb 24, 2025
709bf25
Add custom password input and keypad components for user authentication
adityashibu Feb 24, 2025
028b187
Redirect user to the Users page after successful login
adityashibu Feb 24, 2025
1a8bfae
Refactor homepage and users page for improved code clarity by removin…
adityashibu Feb 24, 2025
3370100
Remove unnecessary comments from Keypad component for improved clarity
adityashibu Feb 25, 2025
e1b668f
Implement Google sign-in functionality and remove unused authenticati…
adityashibu Feb 25, 2025
8825251
Enhance Google sign-in error handling and implement user logout funct…
adityashibu Feb 25, 2025
89c8882
Fix sign-out redirection to home page in AccountMenu component
adityashibu Feb 25, 2025
55e0190
Refactor Dashboard component to restore user authentication checks an…
adityashibu Feb 25, 2025
3757f96
Merge branch 'auth'
adityashibu Feb 25, 2025
d63e57f
Add Firebase environment variables to Next.js configuration
adityashibu Feb 25, 2025
6b5b8b9
Refactor Next.js configuration to use a constant for the config objec…
adityashibu Feb 25, 2025
4837403
Add "use client" directive to automations page for client-side rendering
adityashibu Feb 25, 2025
e5dafb3
Refactor Firebase configuration to initialize app and auth only in th…
adityashibu Feb 25, 2025
4c76945
Initialization of automation scheduling
david-g-f Feb 25, 2025
8786b83
Merge branch 'main' of github.com:adityashibu/Production
david-g-f Feb 25, 2025
aba8b9d
Refactor Dashboard component to manage user session state with useEff…
adityashibu Feb 25, 2025
b66650b
Merge branch 'main' of https://github.com/adityashibu/Production
adityashibu Feb 25, 2025
f6312a6
Refactor Dashboard component to optimize user session handling and im…
adityashibu Feb 25, 2025
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# VS Code stuff
.vscode/
26 changes: 26 additions & 0 deletions .idx/dev.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{pkgs}: {
channel = "stable-24.05";
packages = [
pkgs.nodejs_20
];
idx.extensions = [

];
idx.previews = {
previews = {
web = {
command = [
"npm"
"run"
"dev"
"--"
"--port"
"$PORT"
"--hostname"
"0.0.0.0"
];
manager = "web";
};
};
};
}
File renamed without changes.
20 changes: 19 additions & 1 deletion backend/fastAPI.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import devices_json as dj
import asyncio
import os
import json

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

# Serve user data (Only for testing purposes)
USER_DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "database", "users_db.json")
# print(USER_DB_PATH) # For testing purposes

# FastAPI initialization and routes
app = FastAPI()

Expand Down Expand Up @@ -52,4 +58,16 @@ def get_updates():
async def change_connection_status(id: int):
"""Toggle the connection status of a device."""
result = await dj.changeConnection(id)
return result
return result

@app.get("/user_data")
def get_user_data():
"""Loads and returns the user data from the JSON file."""
try:
with open(USER_DB_PATH, "r") as file:
user_data = json.load(file)
return user_data
except FileNotFoundError:
return {"error": f"User database file not found"}
except json.JSONDecodeError:
return {"error": "Error decoding JSON data"}
62 changes: 61 additions & 1 deletion backend/tests/test_fastAPI.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
from fastapi.testclient import TestClient
from backend.fastAPI import app
import pytest
import json

client = TestClient(app)

@pytest.fixture
def mock_user_data():
"""Mock user database data"""
return {
"users": [
{
"user_name": "john_doe",
"user_password": "securepassword123",
"allocated_devices": ["Laptop", "Smartphone", "Tablet"],
"user_privileges": {
"admin_access": True,
"read_access": True,
"write_access": False,
"execute_access": True
}
},
{
"user_name": "jane_smith",
"user_password": "mypassword456",
"allocated_devices": ["Desktop", "Smartphone"],
"user_privileges": {
"admin_access": False,
"read_access": True,
"write_access": True,
"execute_access": False
}
}
]
}


def test_root():
response = client.get("/")
assert response.status_code == 200
Expand All @@ -26,4 +58,32 @@ def test_root():
# mocker.patch("backend.devices_json.getUpdates", return_value=mock_updates, autospec=True)
# response = client.get("/updates")
# assert response.status_code == 200
# assert response.json() == {"updates": mock_updates}
# assert response.json() == {"updates": mock_updates}

def test_get_user_data(mocker, mock_user_data):
"""Test retrieving user data from the API"""
mocker.patch("backend.fastAPI.USER_DB_PATH", "/mock/path/to/users_db.json") # Mock file path
mocker.patch("builtins.open", mocker.mock_open(read_data=json.dumps(mock_user_data))) # Mock file reading

response = client.get("/user_data")
assert response.status_code == 200
assert response.json() == mock_user_data

def test_user_data_file_not_found(mocker):
"""Test when the JSON file is missing"""
mocker.patch("backend.fastAPI.USER_DB_PATH", "/mock/path/to/missing_file.json") # Mock incorrect file path
mocker.patch("builtins.open", side_effect=FileNotFoundError()) # Simulate missing file

response = client.get("/user_data")
assert response.status_code == 200
assert response.json() == {"error": "User database file not found"}

def test_user_data_invalid_json(mocker):
"""Test when the JSON file is corrupted or invalid"""
mocker.patch("backend.fastAPI.USER_DB_PATH", "/mock/path/to/invalid.json") # Mock incorrect file path
mocker.patch("builtins.open", mocker.mock_open(read_data="{invalid_json}")) # Simulate bad JSON data
mocker.patch("json.load", side_effect=json.JSONDecodeError("Expecting value", "invalid.json", 0)) # Mock JSON error

response = client.get("/user_data")
assert response.status_code == 200
assert response.json() == {"error": "Error decoding JSON data"}
35 changes: 35 additions & 0 deletions database/users_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"users": [
{
"user_name": "John Doe",
"user_password": "1415",
"allocated_devices": [
"device1",
"device2",
"device3",
"device4",
"device5",
"device6"
],
"user_role": "super_user"
},
{
"user_name": "Jane Doe",
"user_password": "2005",
"allocated_devices": [
"device3",
"device4"
],
"user_role": "sub_user"
},
{
"user_name": "John Smith",
"user_password": "0007",
"allocated_devices": [
"device5",
"device6"
],
"user_role": "sub_user"
}
]
}
16 changes: 13 additions & 3 deletions frontend/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
const nextConfig = {
env: {
NEXT_PUBLIC_FIREBASE_API_KEY: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
NEXT_PUBLIC_FIREBASE_PROJECT_ID: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
NEXT_PUBLIC_FIREBASE_APP_ID: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
},
};

export default nextConfig;

1 change: 1 addition & 0 deletions frontend/src/app/automations/page.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
import React from "react";
import Breadcrumb from "../ui/dashboard/breadcrumbs";

Expand Down
20 changes: 20 additions & 0 deletions frontend/src/app/dashboard/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,27 @@ import IOSSwitch from "../ui/iosButton";
import EnergyUsageChart from "../ui/energyChart";
import { useTheme } from "@emotion/react";

import { auth } from "@/app/firebase/config";
import { useAuthState } from "react-firebase-hooks/auth";

import { useRouter } from "next/navigation";

const Dashboard = () => {
const router = useRouter();
if(typeof window !== 'undefined'){
const userSession = sessionStorage.getItem("user");
const [user, loading] = useAuthState(auth);
useEffect(() => {
if (!loading && !user && !userSession) {
router.push("/");
}
}, [user, userSession, loading, router]);
}

// if (loading) return null;

// if (!user && !userSession) return null;

const [data, setData] = useState([]);
const [checked, setChecked] = useState([]);
const [timeRange, setTimeRange] = useState("daily");
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/app/firebase/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { initializeApp, getApps, getApp } from "firebase/app";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};

let app, auth;
if (typeof window !== "undefined") {
app = !getApps().length ? initializeApp(firebaseConfig) : getApp();
auth = getAuth(app);
}

export { app, auth };
Loading
Loading