feat: implement business and user CRUD API endpoints with authentication utilities and update feature documentation
All checks were successful
Build and Push App / build (push) Successful in 5m46s
All checks were successful
Build and Push App / build (push) Successful in 5m46s
This commit is contained in:
42
lib/auth-utils.ts
Normal file
42
lib/auth-utils.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { NextRequest } from 'next/server';
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
export const USER_SAFE_SELECT = {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
role: true,
|
||||
avatar: true,
|
||||
bio: true,
|
||||
location: true,
|
||||
phone: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
emailVerified: true,
|
||||
};
|
||||
|
||||
export async function getAuthUser(request: NextRequest) {
|
||||
const userId = request.headers.get('x-user-id');
|
||||
if (!userId) return null;
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
emailVerified: true,
|
||||
isSuspended: true
|
||||
}
|
||||
});
|
||||
return user;
|
||||
} catch (error) {
|
||||
console.error('Error fetching auth user:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function isAdmin(request: NextRequest) {
|
||||
const user = await getAuthUser(request);
|
||||
return user?.role === 'ADMIN';
|
||||
}
|
||||
Reference in New Issue
Block a user