Backend Database Struct Fix
This commit is contained in:
@@ -3,15 +3,13 @@
|
||||
import { useDevice } from "@/lib/context/DeviceContext";
|
||||
|
||||
function Mobile() {
|
||||
const { session } = useDevice();
|
||||
let isAuthenticated = session == null ? false : true;
|
||||
const { isAuthenticated, session } = useDevice();
|
||||
|
||||
return (
|
||||
<main className="flex flex-col gap-[32px] row-start-2 items-center mt-10">
|
||||
<h1 className="text-3xl sm:text-4xl font-bold tracking-[-.01em] text-center sm:text-left">
|
||||
Welcome, {session?.name || "NULL"} !!
|
||||
Welcome, {isAuthenticated ? session.name : ""} !!
|
||||
</h1>
|
||||
|
||||
{isAuthenticated ? (
|
||||
<div>
|
||||
<button type="button" className="btn bg-surface-500">
|
||||
@@ -33,13 +31,12 @@ function Mobile() {
|
||||
}
|
||||
|
||||
function Web() {
|
||||
const { session } = useDevice();
|
||||
let isAuthenticated = session == null ? false : true;
|
||||
const { isAuthenticated, session } = useDevice();
|
||||
|
||||
return (
|
||||
<main className="flex flex-col row-start-2 items-center mt-10">
|
||||
<h1 className="text-3xl sm:text-4xl font-bold tracking-[-.01em] text-center sm:text-left">
|
||||
Welcome, {session?.name || "NULL"} !!
|
||||
Welcome, {isAuthenticated ? session.name : ""} !!
|
||||
</h1>
|
||||
|
||||
{isAuthenticated ? (
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { db } from "../../lib/scripts/db";
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method === 'GET') {
|
||||
// Handle GET request
|
||||
res.status(200).json({ message: 'Hello, this is a GET request!' });
|
||||
} else {
|
||||
// Handle unsupported methods
|
||||
res.setHeader('Allow', ['GET']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
if (req.method === 'GET') {
|
||||
|
||||
// Handle GET request
|
||||
res.status(200).json({ message: 'Hello, this is a GET request!' });
|
||||
} else {
|
||||
// Handle unsupported methods
|
||||
res.setHeader('Allow', ['GET']);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth0 } from "../../../lib/auth0";
|
||||
import { auth0 } from "../../../lib/scripts/auth0";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const DATABASE_URL = process.env.DATABASE_URL;
|
||||
|
||||
if (!DATABASE_URL) {
|
||||
throw new Error("Please define the DATABASE_URL environment variable inside .env.local");
|
||||
}
|
||||
|
||||
let cached = global.mongoose;
|
||||
|
||||
if (!cached) {
|
||||
cached = global.mongoose = { conn: null, promise: null };
|
||||
}
|
||||
|
||||
async function connectDB() {
|
||||
if (cached.conn) {
|
||||
return cached.conn;
|
||||
}
|
||||
|
||||
if (!cached.promise) {
|
||||
const opts = {
|
||||
bufferCommands: false,
|
||||
};
|
||||
|
||||
cached.promise = mongoose.connect(DATABASE_URL, opts).then((mongoose) => {
|
||||
return mongoose;
|
||||
});
|
||||
}
|
||||
cached.conn = await cached.promise;
|
||||
return cached.conn;
|
||||
}
|
||||
|
||||
export default connectDB;
|
||||
@@ -7,6 +7,7 @@ interface DeviceContextProps {
|
||||
isSafari: boolean;
|
||||
isMobile: boolean;
|
||||
session: any | null;
|
||||
isAuthenticated: boolean;
|
||||
}
|
||||
|
||||
const DeviceContext = createContext<DeviceContextProps | undefined>(undefined);
|
||||
@@ -16,6 +17,8 @@ export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
||||
const [isMobile, setIsMobile] = useState<boolean>(false);
|
||||
const [session, setSession] = useState<any>(null);
|
||||
|
||||
const isAuthenticated = !!session;
|
||||
|
||||
useEffect(() => {
|
||||
setIsSafari(rdd.isSafari);
|
||||
setIsMobile(rdd.isMobile);
|
||||
@@ -35,7 +38,7 @@ export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<DeviceContext.Provider value={{ isSafari, isMobile, session }}>
|
||||
<DeviceContext.Provider value={{ isSafari, isMobile, session, isAuthenticated }}>
|
||||
{children}
|
||||
</DeviceContext.Provider>
|
||||
);
|
||||
|
||||
21
src/lib/scripts/db.js
Normal file
21
src/lib/scripts/db.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import mongoose from "mongoose";
|
||||
|
||||
|
||||
class DB {
|
||||
|
||||
constructor() {
|
||||
this.connect();
|
||||
|
||||
}
|
||||
|
||||
connect() {
|
||||
if (!process.env.DATABASE_URL) {
|
||||
throw new Error("Please define the DATABASE_URL environment variable inside .env.local");
|
||||
}
|
||||
mongoose.set('strictQuery', true);
|
||||
mongoose.connect(process.env.DATABASE_URL).then(() => { console.log("Connected to DataBase") });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const db = new DB();
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { auth0 } from "./lib/auth0";
|
||||
import { auth0 } from "./lib/scripts/auth0";
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
return await auth0.middleware(request);
|
||||
|
||||
Reference in New Issue
Block a user