Exp7 Oose
Exp7 Oose
Date:
Aim:
To draw layered architectural diagrams and package diagrams for the IdeaForge
system and implement the same .
Procedure:
1. UI Layer
● Database Connection
Package Diagram:
Program:
Auth.ts:
import jwt from "jsonwebtoken";
import { type NextRequest, NextResponse } from "next/server";
import { type IUser } from "@/models/User"; // Import only the type, not the model directly
// Ensure JWT_SECRET is set in your environment variables
const JWT_SECRET = process.env.JWT_SECRET ||
"ad989da5e56528c46ceb00a81378a9f5fd90defd1a4fda7ab39dd1ca1d93ba02";
export async function signToken(user: IUser) {
const payload = {
id: user._id,
email: user.email,
role: user.role,
};
return jwt.sign(payload, JWT_SECRET, { expiresIn: "1d" });
}
export async function verifyToken(token: string) {
try { return jwt.verify(token, JWT_SECRET) as jwt.JwtPayload;}
catch (error) {return null;}
}
// This function should only be used in API routes or Server Components
export async function getCurrentUser(req: NextRequest) {
const token = req.cookies.get("token")?.value;
if (!token) return null;
const decoded = await verifyToken(token);
if (!decoded) return null;
try {
// Lazy-load Mongoose and User model
const mongoose = await import("mongoose");
const { default: User } = await import("@/models/User");
const user = await User.findById(decoded.id).select("-password");
return user;
} catch (error) {
console.error("Failed to fetch user:", error);
return null;
}
}
export async function requireAuth(req: NextRequest) {
const token = req.cookies.get("token")?.value;
if (!token) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 });}
const decoded = await verifyToken(token);
if (!decoded) { return NextResponse.json({ error: "Invalid token" }, { status: 401 }); }
return null; // Auth successful
}
export async function requireRole(req: NextRequest, role: string) {
const authError = await requireAuth(req);
if (authError) return authError;
const token = req.cookies.get("token")?.value;
const decoded = await verifyToken(token!);
if (!decoded || decoded.role !== role) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
return null; // Role check passed
}
ai-service.ts:
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || "");
type IdeaFormData = {
areasOfInterest: string;
domainInterest: string;
languagesKnown: string;
additionalInfo?: string;
};
export async function generateProjectIdea(formData: IdeaFormData): Promise<string> {
try {
// Updated model name to the correct version
const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro" });
const prompt = `
Generate a unique project idea based on the following student details:
- Areas of Interest: ${formData.areasOfInterest}
- Domain Interest: ${formData.domainInterest}
- Programming Languages: ${formData.languagesKnown}
- Additional Info: ${formData.additionalInfo || "None"}
db.ts:
import mongoose from "mongoose"
const MONGODB_URI = process.env.MONGODB_URI ||
"mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority&
appName=IdeaForge"
let cached = global.mongoose
if (!cached) { cached = global.mongoose = { conn: null, promise: null }}
async function connectToDatabase() {
if (cached.conn) { return cached.conn }
if (!cached.promise) {
const opts = { bufferCommands: false, }
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose
})
}
try {
cached.conn = await cached.promise
} catch (e) {
cached.promise = null
throw e
}
return cached.conn
}
Output:
Result:
Thus the implementation for the IdeaForge system is successfully completed.