diff --git a/src/app/(app)/page.tsx b/src/app/(app)/page.tsx index 412fc38..6d5ab41 100644 --- a/src/app/(app)/page.tsx +++ b/src/app/(app)/page.tsx @@ -8,7 +8,7 @@ function Mobile() { return (

- Welcome, {isAuthenticated ? session.name : ""} !! + Welcome, {isAuthenticated ? session.username : ""} !!

{isAuthenticated ? (
@@ -36,7 +36,7 @@ function Web() { return (

- Welcome, {isAuthenticated ? session.name : ""} !! + Welcome, {isAuthenticated ? session.username : ""} !!

{isAuthenticated ? ( diff --git a/src/app/auth/session/route.ts b/src/app/auth/session/route.ts index cb74f68..aca6294 100644 --- a/src/app/auth/session/route.ts +++ b/src/app/auth/session/route.ts @@ -1,10 +1,27 @@ import { NextResponse } from "next/server"; import { auth0 } from "../../../lib/scripts/auth0"; +import { db } from "@/lib/scripts/db"; + export async function GET() { try { 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) { console.error("Error getting session:", error); return NextResponse.json({ session: null }, { status: 500 }); diff --git a/src/lib/scripts/User.ts b/src/lib/scripts/User.ts index 14c4671..820e53d 100644 --- a/src/lib/scripts/User.ts +++ b/src/lib/scripts/User.ts @@ -1,39 +1,82 @@ import mongoose from "mongoose"; const reqString = { - type: String, - required: true, -} + type: String, + required: true, +}; const userSchema = new mongoose.Schema({ - email: reqString, - username: reqString, - points: Number, - inventory : Array, - id: reqString, - friends: Array, - requests: Array, -}) + id: reqString, + email: reqString, + username: reqString, + avatar: String, + points: Number, + inventory: Array, + friends: Array, + requests: Array, +}); export class User { - model: mongoose.Model; - upsert: any; - constructor() { - this.model = mongoose.model('user', userSchema); - this.upsert = { upsert: true }; - } - async create(email: string, username: string, points: number, inventory: Array, id: string, friends: Array, requests: Array) { - const newEntry = new this.model({ - email: email, - username: username, - points: points, - inventory: inventory, - id: id, - friends: friends, - requests: requests, - - }); - await newEntry.save(); - return newEntry; - } -} \ No newline at end of file + model: mongoose.Model; + upsert: any; + constructor() { + this.model = mongoose.model("users", userSchema); + this.upsert = { upsert: true }; + } + + makeId(length: number) { + var result = []; + var characters = 'abcdefghijklmnopqrstuvwxyz012345678901234567890123456789'; + var charactersLength = characters.length; + for ( var i = 0; i < length; i++ ) { + result.push(characters.charAt(Math.floor(Math.random() * charactersLength))); + } + return result.join(''); + } + + async create( + 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; diff --git a/src/lib/scripts/db.js b/src/lib/scripts/db.ts similarity index 83% rename from src/lib/scripts/db.js rename to src/lib/scripts/db.ts index 0e7c639..da6f34b 100644 --- a/src/lib/scripts/db.js +++ b/src/lib/scripts/db.ts @@ -1,21 +1,20 @@ import mongoose from "mongoose"; +import User from "./User"; class DB { - + users: User; constructor() { + this.users = new User(); 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(); \ No newline at end of file