fix: add GET handler to user profile API route
Fixed 405 Method Not Allowed error on subscription pages: Issue: - Subscription pages were making GET requests to /api/user/profile - The API route only had a PUT handler (for profile updates) - This caused 405 (Method Not Allowed) errors Solution: - Added GET handler to /api/user/profile/route.ts - Handler authenticates user via Bearer token - Returns complete user data including subscription fields: * subscriptionTier * subscriptionStatus * conversationLimit * conversationCount * limitResetDate * stripeCustomerId * stripeSubscriptionId Result: - Subscription pages can now fetch user data successfully - Settings page subscription widget displays correctly - No more 405 errors in console 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -10,18 +10,83 @@ function getErrorMessages(locale: string = 'ro') {
|
|||||||
unauthorized: 'Nu esti autentificat',
|
unauthorized: 'Nu esti autentificat',
|
||||||
nameRequired: 'Numele este obligatoriu',
|
nameRequired: 'Numele este obligatoriu',
|
||||||
updateFailed: 'Actualizarea a eșuat',
|
updateFailed: 'Actualizarea a eșuat',
|
||||||
success: 'Profil actualizat cu succes'
|
success: 'Profil actualizat cu succes',
|
||||||
|
userNotFound: 'Utilizator negăsit'
|
||||||
},
|
},
|
||||||
en: {
|
en: {
|
||||||
unauthorized: 'Unauthorized',
|
unauthorized: 'Unauthorized',
|
||||||
nameRequired: 'Name is required',
|
nameRequired: 'Name is required',
|
||||||
updateFailed: 'Update failed',
|
updateFailed: 'Update failed',
|
||||||
success: 'Profile updated successfully'
|
success: 'Profile updated successfully',
|
||||||
|
userNotFound: 'User not found'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return messages[locale as keyof typeof messages] || messages.ro
|
return messages[locale as keyof typeof messages] || messages.ro
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
try {
|
||||||
|
const url = new URL(request.url)
|
||||||
|
const locale = url.searchParams.get('locale') || 'ro'
|
||||||
|
const messages = getErrorMessages(locale)
|
||||||
|
|
||||||
|
// Get token from authorization header
|
||||||
|
const authHeader = request.headers.get('authorization')
|
||||||
|
const token = authHeader?.replace('Bearer ', '')
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify token and get user
|
||||||
|
const user = await getUserFromToken(token)
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return NextResponse.json({ error: messages.unauthorized }, { status: 401 })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get full user data including subscription fields
|
||||||
|
const userData = await prisma.user.findUnique({
|
||||||
|
where: { id: user.id },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
email: true,
|
||||||
|
name: true,
|
||||||
|
role: true,
|
||||||
|
theme: true,
|
||||||
|
fontSize: true,
|
||||||
|
subscriptionTier: true,
|
||||||
|
subscriptionStatus: true,
|
||||||
|
conversationLimit: true,
|
||||||
|
conversationCount: true,
|
||||||
|
limitResetDate: true,
|
||||||
|
stripeCustomerId: true,
|
||||||
|
stripeSubscriptionId: true,
|
||||||
|
createdAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
lastLoginAt: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!userData) {
|
||||||
|
return NextResponse.json({ error: messages.userNotFound }, { status: 404 })
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
user: userData
|
||||||
|
})
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Profile fetch error:', error)
|
||||||
|
const url = new URL(request.url)
|
||||||
|
const locale = url.searchParams.get('locale') || 'ro'
|
||||||
|
const messages = getErrorMessages(locale)
|
||||||
|
|
||||||
|
return NextResponse.json({ error: messages.unauthorized }, { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function PUT(request: Request) {
|
export async function PUT(request: Request) {
|
||||||
try {
|
try {
|
||||||
const url = new URL(request.url)
|
const url = new URL(request.url)
|
||||||
|
|||||||
Reference in New Issue
Block a user