summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManurbhav2024-06-17 13:04:16 +0530
committerManurbhav2024-06-17 13:04:16 +0530
commit8c495febd09303742cca2ff703f9fab6257a5a4f (patch)
tree470c179f15e2278a6f508beda24a21d833d974cc
parent0cbdfccf08f51ecc7ec4b0cc985e84df7a179fd1 (diff)
downloadIotJS-Astro-8c495febd09303742cca2ff703f9fab6257a5a4f.tar.gz
IotJS-Astro-8c495febd09303742cca2ff703f9fab6257a5a4f.tar.bz2
IotJS-Astro-8c495febd09303742cca2ff703f9fab6257a5a4f.zip
Auth fixed
-rw-r--r--src/components/LoginBody.astro9
-rw-r--r--src/lib/databaseUpdates.js2
-rw-r--r--src/middleware.ts1
-rw-r--r--src/pages/admin.astro2
-rw-r--r--src/pages/api/login.ts4
-rw-r--r--src/pages/login.astro7
6 files changed, 16 insertions, 9 deletions
diff --git a/src/components/LoginBody.astro b/src/components/LoginBody.astro
index 188cfd6..5dd25a8 100644
--- a/src/components/LoginBody.astro
+++ b/src/components/LoginBody.astro
@@ -1,10 +1,5 @@
---
-const user = Astro.locals.user; // Fetch the user from session or locals
-if (user) {
- console.log(user);
- return Astro.redirect('/admin');
-}
---
<div class="flex items-center justify-center h-screen">
@@ -12,10 +7,10 @@ if (user) {
<h1 class="text-2xl font-bold text-gray-800 mb-6 text-center">Admin Login</h1>
<form method="post" action="/api/login">
<div class="mb-4">
- <input type="text" id="username" placeholder="Username" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600">
+ <input type="text" id="username" name = "username" placeholder="Username" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600">
</div>
<div class="mb-6">
- <input type="password" id="password" placeholder="Password" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600">
+ <input type="password" id="password" name="password" placeholder="Password" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600">
</div>
<button type="submit" class="w-full bg-blue-500 hover:bg-blue-700 text-white py-2 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-600">
Login
diff --git a/src/lib/databaseUpdates.js b/src/lib/databaseUpdates.js
index 9eb6055..1c8bf79 100644
--- a/src/lib/databaseUpdates.js
+++ b/src/lib/databaseUpdates.js
@@ -1,7 +1,7 @@
import sqlite from "better-sqlite3";
// Path to your SQLite database file
-const dbPath = "./mydatabase.db";
+const dbPath = "../../mydatabase.db";
// Open the SQLite database connection
const db = sqlite(dbPath);
diff --git a/src/middleware.ts b/src/middleware.ts
index 3e8bd01..9b5daf1 100644
--- a/src/middleware.ts
+++ b/src/middleware.ts
@@ -43,5 +43,6 @@ export const onRequest = defineMiddleware(async (context, next) => {
}
context.locals.session = session;
context.locals.user = user;
+
return next();
});
diff --git a/src/pages/admin.astro b/src/pages/admin.astro
index 32aad9b..c3c5cee 100644
--- a/src/pages/admin.astro
+++ b/src/pages/admin.astro
@@ -5,7 +5,9 @@ import Navbar from '../components/Navbar.astro';
import Footer from '../components/Footer.astro';
import IconBar from '../components/IconBar.astro';
const user = Astro.locals.user;
+// console.log(user);
if (!user) {
+ // console.log("redirecting to login");
return Astro.redirect("/login");
}
diff --git a/src/pages/api/login.ts b/src/pages/api/login.ts
index 701c356..484b67b 100644
--- a/src/pages/api/login.ts
+++ b/src/pages/api/login.ts
@@ -8,6 +8,9 @@ import type { APIContext } from "astro";
export async function POST(context: APIContext): Promise<Response> {
const formData = await context.request.formData();
const username = formData.get("username");
+ const password = formData.get("password");
+ // console.log(username);
+ // console.log(password);
if (
typeof username !== "string" ||
username.length < 3 ||
@@ -18,7 +21,6 @@ export async function POST(context: APIContext): Promise<Response> {
status: 400,
});
}
- const password = formData.get("password");
if (
typeof password !== "string" ||
password.length < 6 ||
diff --git a/src/pages/login.astro b/src/pages/login.astro
index 30ccfce..efe31a3 100644
--- a/src/pages/login.astro
+++ b/src/pages/login.astro
@@ -1,4 +1,11 @@
---
+
+const user = Astro.locals.user; // Fetch the user from session or locals
+
+if (user) {
+// console.log(user);
+ return Astro.redirect("/admin");
+}
import Layout from '../layouts/Layout.astro';
import TopBar from '../components/TopBar.astro';
import Navbar from '../components/Navbar.astro';