project-standalo-todo-super/app/api/auth/me/route.ts

56 lines
1.4 KiB
TypeScript

/**
* GET /api/auth/me - Get current authenticated user
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, sanitizeUser } from '@/app/lib/auth';
import { getUserBalance, getUserCompletedTasks, getUserBadges } from '@/app/lib/db/store';
import { UserProfile, ApiResponse } from '@/app/lib/types';
export async function GET(request: NextRequest) {
try {
// Authenticate user
const user = requireAuth(request);
// Get user statistics
const pointsBalance = getUserBalance(user.id);
const tasksCompleted = getUserCompletedTasks(user.id).length;
const badgesCount = getUserBadges(user.id).length;
// Build user profile
const userProfile: UserProfile = {
...sanitizeUser(user),
pointsBalance,
tasksCompleted,
badgesCount
};
return NextResponse.json<ApiResponse<UserProfile>>(
{
success: true,
data: userProfile
},
{ status: 200 }
);
} catch (error) {
if (error instanceof Error && error.message === 'Unauthorized') {
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Unauthorized'
},
{ status: 401 }
);
}
console.error('Get current user error:', error);
return NextResponse.json<ApiResponse>(
{
success: false,
error: 'Internal server error'
},
{ status: 500 }
);
}
}