Database and User Creation Update
This commit is contained in:
@@ -8,7 +8,7 @@ function Mobile() {
|
||||
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, {isAuthenticated ? session.name : ""} !!
|
||||
Welcome, {isAuthenticated ? session.username : ""} !!
|
||||
</h1>
|
||||
{isAuthenticated ? (
|
||||
<div>
|
||||
@@ -36,7 +36,7 @@ function Web() {
|
||||
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, {isAuthenticated ? session.name : ""} !!
|
||||
Welcome, {isAuthenticated ? session.username : ""} !!
|
||||
</h1>
|
||||
|
||||
{isAuthenticated ? (
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -3,37 +3,80 @@ import mongoose from "mongoose";
|
||||
const reqString = {
|
||||
type: String,
|
||||
required: true,
|
||||
}
|
||||
};
|
||||
|
||||
const userSchema = new mongoose.Schema({
|
||||
id: reqString,
|
||||
email: reqString,
|
||||
username: reqString,
|
||||
avatar: String,
|
||||
points: Number,
|
||||
inventory : Array,
|
||||
id: reqString,
|
||||
friends: Array,
|
||||
requests: Array,
|
||||
})
|
||||
inventory: Array,
|
||||
friends: Array<String>,
|
||||
requests: Array<String>,
|
||||
});
|
||||
|
||||
export class User {
|
||||
model: mongoose.Model<any>;
|
||||
upsert: any;
|
||||
constructor() {
|
||||
this.model = mongoose.model('user', userSchema);
|
||||
this.model = mongoose.model("users", userSchema);
|
||||
this.upsert = { upsert: true };
|
||||
}
|
||||
async create(email: string, username: string, points: number, inventory: Array<any>, id: string, friends: Array<any>, requests: Array<any>) {
|
||||
|
||||
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,
|
||||
points: points,
|
||||
inventory: inventory,
|
||||
id: id,
|
||||
friends: friends,
|
||||
requests: requests,
|
||||
|
||||
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;
|
||||
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user