Backend Database Struct Fix
This commit is contained in:
@@ -3,15 +3,13 @@
|
|||||||
import { useDevice } from "@/lib/context/DeviceContext";
|
import { useDevice } from "@/lib/context/DeviceContext";
|
||||||
|
|
||||||
function Mobile() {
|
function Mobile() {
|
||||||
const { session } = useDevice();
|
const { isAuthenticated, session } = useDevice();
|
||||||
let isAuthenticated = session == null ? false : true;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="flex flex-col gap-[32px] row-start-2 items-center mt-10">
|
<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">
|
<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>
|
</h1>
|
||||||
|
|
||||||
{isAuthenticated ? (
|
{isAuthenticated ? (
|
||||||
<div>
|
<div>
|
||||||
<button type="button" className="btn bg-surface-500">
|
<button type="button" className="btn bg-surface-500">
|
||||||
@@ -33,13 +31,12 @@ function Mobile() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Web() {
|
function Web() {
|
||||||
const { session } = useDevice();
|
const { isAuthenticated, session } = useDevice();
|
||||||
let isAuthenticated = session == null ? false : true;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="flex flex-col row-start-2 items-center mt-10">
|
<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">
|
<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>
|
</h1>
|
||||||
|
|
||||||
{isAuthenticated ? (
|
{isAuthenticated ? (
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import { db } from "../../lib/scripts/db";
|
||||||
|
|
||||||
export default function handler(req, res) {
|
export default function handler(req, res) {
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
|
|
||||||
// Handle GET request
|
// Handle GET request
|
||||||
res.status(200).json({ message: 'Hello, this is a GET request!' });
|
res.status(200).json({ message: 'Hello, this is a GET request!' });
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth0 } from "../../../lib/auth0";
|
import { auth0 } from "../../../lib/scripts/auth0";
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
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;
|
isSafari: boolean;
|
||||||
isMobile: boolean;
|
isMobile: boolean;
|
||||||
session: any | null;
|
session: any | null;
|
||||||
|
isAuthenticated: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DeviceContext = createContext<DeviceContextProps | undefined>(undefined);
|
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 [isMobile, setIsMobile] = useState<boolean>(false);
|
||||||
const [session, setSession] = useState<any>(null);
|
const [session, setSession] = useState<any>(null);
|
||||||
|
|
||||||
|
const isAuthenticated = !!session;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsSafari(rdd.isSafari);
|
setIsSafari(rdd.isSafari);
|
||||||
setIsMobile(rdd.isMobile);
|
setIsMobile(rdd.isMobile);
|
||||||
@@ -35,7 +38,7 @@ export const DeviceProvider: React.FC<{ children: React.ReactNode }> = ({ childr
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DeviceContext.Provider value={{ isSafari, isMobile, session }}>
|
<DeviceContext.Provider value={{ isSafari, isMobile, session, isAuthenticated }}>
|
||||||
{children}
|
{children}
|
||||||
</DeviceContext.Provider>
|
</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 type { NextRequest } from "next/server";
|
||||||
import { auth0 } from "./lib/auth0";
|
import { auth0 } from "./lib/scripts/auth0";
|
||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
export async function middleware(request: NextRequest) {
|
||||||
return await auth0.middleware(request);
|
return await auth0.middleware(request);
|
||||||
|
|||||||
Reference in New Issue
Block a user