Skip to content

Commit

Permalink
Merge pull request #32 from rapidosaas/develop
Browse files Browse the repository at this point in the history
added the profile with a choice of avatars
  • Loading branch information
nazimboudeffa authored Dec 30, 2024
2 parents 026a1ee + 11f105c commit 69c8632
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 20 deletions.
111 changes: 98 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"@radix-ui/react-avatar": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.1.4",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-radio-group": "^1.2.2",
"@radix-ui/react-select": "^2.1.2",
"@radix-ui/react-slot": "^1.1.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"date-fns": "^4.1.0",
"lucide-react": "^0.468.0",
"mongodb": "^5.9.2",
"mongoose": "^8.8.4",
"nanoid": "^5.0.9",
"next": "15.0.4",
Expand Down
39 changes: 39 additions & 0 deletions src/app/api/dashboard/avatar/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NextResponse } from 'next/server';
import { connectDB } from "@/lib/db";
import Profile from '@/lib/models/Profile';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

export async function GET() {
try {

const session = await getServerSession(authOptions);

// Vérification de la session utilisateur
console.log('Session:', session);

if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

// Vérification supplémentaire de l'ID utilisateur
if (!session.user.id) {
return NextResponse.json({ error: 'User ID not found' }, { status: 403 });
}

await connectDB();

// Filtrer explicitement par userId
const profile = await Profile.findOne({
userId: session.user.id // Utilisez directement l'ID de la session
});

console.log('Profile:', profile.image);

return NextResponse.json({ avatar : profile.image }, { status: 200 });

} catch (error) {
console.error('Error fetching user profile:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
72 changes: 72 additions & 0 deletions src/app/api/dashboard/profile/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { NextRequest, NextResponse } from 'next/server';
import { connectDB } from "@/lib/db";
import Profile from '@/lib/models/Profile';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

export async function GET() {
try {

const session = await getServerSession(authOptions);

// Vérification de la session utilisateur
console.log('Session:', session);

if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

// Vérification supplémentaire de l'ID utilisateur
if (!session.user.id) {
return NextResponse.json({ error: 'User ID not found' }, { status: 403 });
}

await connectDB();

// Filtrer explicitement par userId
const profile = await Profile.findOne({
userId: session.user.id // Utilisez directement l'ID de la session
});

console.log('Profile:', profile);

return NextResponse.json({ profile }, { status: 200 });

} catch (error) {
console.error('Error fetching user profile:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}

export async function POST(request: NextRequest) {
try {

const session = await getServerSession(authOptions);

if (!session?.user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

// Vérification supplémentaire de l'ID utilisateur
if (!session.user.id) {
return NextResponse.json({ error: 'User ID not found' }, { status: 403 });
}

const body = await request.json();

await connectDB();

// Find the profile by userId and update it, or create a new one if it doesn't exist
const profile = await Profile.findOneAndUpdate(
{ userId: session.user.id },
{ ...body, userId: session.user.id },
{ new: true, upsert: true, setDefaultsOnInsert: true }
);

return NextResponse.json({ profile }, { status: 200 });

} catch (error) {
console.error('Error saving user profile:', error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
Loading

0 comments on commit 69c8632

Please sign in to comment.