-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement /me endpoint for checking premium status
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |