Skip to content

Commit

Permalink
feat: add /me endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Jun 10, 2023
1 parent 30f33b7 commit 6e2580b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pages/api/external/me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextApiResponse } from 'next';

import { AppNextApiRequest } from '@app/types/index';
import { ApiError, ApiErrorType } from '@app/utils/api-error';
import { createApiHandler, respond } from '@app/utils/createa-api-handler';
import prisma from '@app/utils/prisma-client';

const handler = createApiHandler();

export const me = async (req: AppNextApiRequest, res: NextApiResponse) => {
// get Bearer token from header
const authHeader = req.headers.authorization;
const apiKey = authHeader && authHeader.split(' ')?.[1];

if (!apiKey) {
throw new ApiError(ApiErrorType.INVALID_REQUEST);
}

const found = await prisma.userApiKey.findUnique({
where: {
key: apiKey,
},
include: {
user: true,
},
});

if (!found) {
throw new ApiError(ApiErrorType.UNAUTHORIZED);
}

return found.user;
};

handler.get(respond(me));

export default handler;

1 comment on commit 6e2580b

@vercel
Copy link

@vercel vercel bot commented on 6e2580b Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.