Skip to content

Commit

Permalink
implement /me endpoint for checking premium status
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Jul 24, 2023
1 parent 18a9408 commit 10768a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions web/api/auth0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ManagementClient } from 'auth0';

const { host } = new URL(process.env.AUTH0_ISSUER_BASE_URL!);

export const auth0 = new ManagementClient<
{ [key: string]: any },
{ premium: true }
>({
domain: host,
token: process.env.AUTH0_TOKEN!,
});
27 changes: 27 additions & 0 deletions web/pages/api/me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { cors } from 'api/middleware/cors';
import { runMiddleware } from 'api/middleware';
import { auth0 } from 'api/auth0';
import { getSession } from '@auth0/nextjs-auth0';

export default async function me(req: NextApiRequest, res: NextApiResponse) {
await runMiddleware(req, res, cors);

if (!['GET'].includes(req.method!)) {
res.setHeader('Allow', 'GET');
res.status(405).end('Method Not Allowed');
return;
}

const session = await getSession(req, res);

if (session) {
const user = await auth0.getUser({ id: session!.user.sub });

return res.status(200).json({
username: user.nickname,
email: user.email,
metadata: user.user_metadata,
});
}
}

0 comments on commit 10768a6

Please sign in to comment.