feat: added the auth guard script

main
Kirubakaran 2025-09-13 02:08:41 +05:30
parent 1054d3dc0b
commit 6d3899e8e1
11 changed files with 31 additions and 11 deletions

View File

@ -38,7 +38,7 @@ app = Flask(__name__)
# CORS(app, resources={r"/api/*": {"origins": "http://127.0.0.1:5500"}}, supports_credentials=True, expose_headers='Content-Disposition')
CORS(app, resources={r"/api/*": {"origins": ["http://192.168.1.8:5500","http://127.0.0.1:5500"]}}, supports_credentials=True, expose_headers='Content-Disposition')
CORS(app, resources={r"/api/*": {"origins": ["http://192.168.1.12:5500","http://127.0.0.1:5500"]}}, supports_credentials=True, expose_headers='Content-Disposition')
# CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}}) , "http://127.0.0.1:5500"
# This tells Flask: "For any route starting with /api/, allow requests
@ -484,5 +484,5 @@ if __name__ == '__main__':
mqtt_thread = threading.Thread(target=start_mqtt_clients, daemon=True)
mqtt_thread.start()
print(f"Starting Flask-SocketIO server on http://192.168.1.8:5000")
socketio.run(app, host='192.168.1.8', port=5000)
print(f"Starting Flask-SocketIO server on http://192.168.1.12:5000")
socketio.run(app, host='192.168.1.12', port=5000)

View File

@ -5,6 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Swap Station Analytics</title>
<script src="js/auth-guard.js"></script>
<!-- Inter + Tailwind -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

View File

@ -5,6 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Swap Station Dashboard</title>
<script src="js/auth-guard.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">

11
frontend/js/auth-guard.js Normal file
View File

@ -0,0 +1,11 @@
(function() {
// Get the user from localStorage
const user = localStorage.getItem('user');
// Check if the user object exists. If not, redirect to the login page.
if (!user) {
// Use window.location.replace() to prevent the user from clicking the "back" button
// and re-accessing the protected page.
window.location.replace('/frontend/index.html');
}
})();

View File

@ -11,7 +11,7 @@ document.addEventListener('DOMContentLoaded', () => {
const password = document.getElementById('password').value;
try {
const response = await fetch('http://192.168.1.8:5000/api/login', {
const response = await fetch('http://192.168.1.12:5000/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),

View File

@ -1,7 +1,7 @@
document.addEventListener('DOMContentLoaded', () => {
// --- CONFIGURATION ---
const SOCKET_URL = "http://192.168.1.8:5000";
const API_BASE = "http://192.168.1.8:5000/api"; // Added for API calls
const SOCKET_URL = "http://192.168.1.12:5000";
const API_BASE = "http://192.168.1.12:5000/api"; // Added for API calls
// --- DOM ELEMENT REFERENCES ---
const grid = document.getElementById('chambersGrid');

View File

@ -1,7 +1,7 @@
document.addEventListener('DOMContentLoaded', () => {
// --- CONFIGURATION ---
const SOCKET_URL = "http://192.168.1.8:5000";
const API_BASE = "http://192.168.1.8:5000/api";
const SOCKET_URL = "http://192.168.1.12:5000";
const API_BASE = "http://192.168.1.12:5000/api";
// --- DOM ELEMENT REFERENCES ---
const stationNameEl = document.getElementById('station-name');

View File

@ -86,7 +86,7 @@ document.addEventListener('DOMContentLoaded', () => {
// --- DATA FETCHING & STATUS POLLING ---
const loadAndPollStations = async () => {
try {
const response = await fetch('http://192.168.1.8:5000/api/stations');
const response = await fetch('http://192.168.1.12:5000/api/stations');
if (!response.ok) throw new Error('Failed to fetch stations');
const stations = await response.json();

View File

@ -5,10 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Swap Station Logs</title>
<script src="js/auth-guard.js"></script>
<!-- Font + Tailwind -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {

View File

@ -5,6 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Swap Station Select a Station</title>
<script src="js/auth-guard.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
@ -195,7 +197,7 @@
</div>
<script>
const API_BASE = 'http://192.168.1.8:5000/api';
const API_BASE = 'http://192.168.1.12:5000/api';
const grid = document.getElementById('stations-grid');
const addStationCardTmpl = document.getElementById('add-station-card-template');
@ -248,7 +250,7 @@
location: stationLocation.value.trim(),
mqtt_broker: mqttBroker.value.trim(),
mqtt_port: Number(mqttPort.value),
mqtt_username: mqttUsername.value || null,
mqtt_user: mqttUsername.value || null,
mqtt_password: mqttPassword.value || null,
};
try {