Database and User Creation Update

This commit is contained in:
2025-04-12 23:02:32 -04:00
parent 268250b53b
commit b8259eca00
4 changed files with 98 additions and 39 deletions

View File

@@ -8,7 +8,7 @@ function Mobile() {
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, {isAuthenticated ? session.name : ""} !! Welcome, {isAuthenticated ? session.username : ""} !!
</h1> </h1>
{isAuthenticated ? ( {isAuthenticated ? (
<div> <div>
@@ -36,7 +36,7 @@ function Web() {
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, {isAuthenticated ? session.name : ""} !! Welcome, {isAuthenticated ? session.username : ""} !!
</h1> </h1>
{isAuthenticated ? ( {isAuthenticated ? (

View File

@@ -1,10 +1,27 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { auth0 } from "../../../lib/scripts/auth0"; import { auth0 } from "../../../lib/scripts/auth0";
import { db } from "@/lib/scripts/db";
export async function GET() { export async function GET() {
try { try {
const session = await auth0.getSession(); const session = await auth0.getSession();
return NextResponse.json({ session: session?.user });
if (!session) {
return NextResponse.json({ session: null }, { status: 401 });
}
const sessionUser = session.user;
let userData = await db.users.findByEmail((sessionUser.email as string));
if (!userData) {
console.log("User not found in database, creating new user...");
userData = await db.users.create(sessionUser?.email as string, sessionUser?.nickname as string);
} else {
console.log("User found in database:", userData);
}
return NextResponse.json({ session: userData });
} catch (error) { } catch (error) {
console.error("Error getting session:", error); console.error("Error getting session:", error);
return NextResponse.json({ session: null }, { status: 500 }); return NextResponse.json({ session: null }, { status: 500 });

View File

@@ -1,39 +1,82 @@
import mongoose from "mongoose"; import mongoose from "mongoose";
const reqString = { const reqString = {
type: String, type: String,
required: true, required: true,
} };
const userSchema = new mongoose.Schema({ const userSchema = new mongoose.Schema({
email: reqString, id: reqString,
username: reqString, email: reqString,
points: Number, username: reqString,
inventory : Array, avatar: String,
id: reqString, points: Number,
friends: Array, inventory: Array,
requests: Array, friends: Array<String>,
}) requests: Array<String>,
});
export class User { export class User {
model: mongoose.Model<any>; model: mongoose.Model<any>;
upsert: any; upsert: any;
constructor() { constructor() {
this.model = mongoose.model('user', userSchema); this.model = mongoose.model("users", userSchema);
this.upsert = { upsert: true }; this.upsert = { upsert: true };
} }
async create(email: string, username: string, points: number, inventory: Array<any>, id: string, friends: Array<any>, requests: Array<any>) {
const newEntry = new this.model({ makeId(length: number) {
email: email, var result = [];
username: username, var characters = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789';
points: points, var charactersLength = characters.length;
inventory: inventory, for ( var i = 0; i < length; i++ ) {
id: id, result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));
friends: friends, }
requests: requests, return result.join('');
}
});
await newEntry.save(); async create(
return newEntry; email: string,
} username: string
} ) {
const id = this.makeId(5);
const newEntry = new this.model({
id: id,
email: email,
username: username,
avatar: null,
points: 0,
inventory: [],
friends: [],
requests: []
});
await newEntry.save();
return newEntry;
}
async findById(id: string) {
return await this.model.findOne({ id: id });
}
async findByEmail(email: string) {
return await this.model.findOne({ email: email });
}
async findByUsername(username: string) {
return await this.model.findOne({ username: username });
}
async update(Id: string, data: any) {
return await this.model.updateOne({ id: Id }, { $set: data }, this.upsert);
}
async delete(Id: string) {
return await this.model.deleteOne({ id: Id });
}
async getAll() {
return await this.model.find({});
}
}
export default User;

View File

@@ -1,21 +1,20 @@
import mongoose from "mongoose"; import mongoose from "mongoose";
import User from "./User";
class DB { class DB {
users: User;
constructor() { constructor() {
this.users = new User();
this.connect(); this.connect();
} }
connect() { connect() {
if (!process.env.DATABASE_URL) { if (!process.env.DATABASE_URL) {
throw new Error("Please define the DATABASE_URL environment variable inside .env.local"); 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") }); mongoose.connect(process.env.DATABASE_URL).then(() => { console.log("Connected to DataBase") });
} }
} }
export const db = new DB(); export const db = new DB();