-
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.
Merge pull request #45 from dallen4/feat/premium
Feature: Premium License
- Loading branch information
Showing
8 changed files
with
546 additions
and
3 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,37 @@ | ||
import { ManagementClient } from 'auth0'; | ||
import { UserMetadata } from 'types/users'; | ||
|
||
const { host } = new URL(process.env.AUTH0_ISSUER_BASE_URL!); | ||
|
||
export const auth0 = new ManagementClient<{ [key: string]: any }, UserMetadata>( | ||
{ | ||
domain: host, | ||
token: process.env.AUTH0_TOKEN!, | ||
}, | ||
); | ||
|
||
export const getUserById = async (id: string) => { | ||
const user = await auth0.getUser({ id }); | ||
|
||
return { | ||
username: user.nickname, | ||
email: user.email, | ||
metadata: user.user_metadata, | ||
}; | ||
}; | ||
|
||
export const getUserIdsByEmail = async (email: string) => { | ||
const users = await auth0.getUsersByEmail(email); | ||
|
||
return users.map((user) => user.user_id!); | ||
}; | ||
|
||
export const updateUser = async (id: string, data: UserMetadata) => { | ||
const updatedUser = await auth0.updateUserMetadata({ id }, data); | ||
|
||
return { | ||
username: updatedUser.nickname, | ||
email: updatedUser.email, | ||
metadata: updatedUser.user_metadata, | ||
}; | ||
}; |
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,28 @@ | ||
import { NextApiRequest } from 'next'; | ||
import Stripe from 'stripe'; | ||
|
||
const client = new Stripe(process.env.STRIPE_SECRET_KEY!, { | ||
apiVersion: '2022-11-15', | ||
}); | ||
|
||
export function buildEvent(request: NextApiRequest) { | ||
const sig = request.headers['stripe-signature']!; | ||
|
||
const event = client.webhooks.constructEvent( | ||
request.body, | ||
sig, | ||
process.env.STRIPE_WEBHOOK_SECRET!, | ||
); | ||
|
||
return event; | ||
} | ||
|
||
export async function getEmailForCheckout(id: string) { | ||
const session = await client.checkout.sessions.retrieve(id, { | ||
expand: ['line_items'], | ||
}); | ||
|
||
// session.line_items!.data[0] | ||
|
||
return session.customer_email!; | ||
} |
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
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,23 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { cors } from 'api/middleware/cors'; | ||
import { runMiddleware } from 'api/middleware'; | ||
import { getUserById } 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 getUserById(session!.user.sub); | ||
|
||
return res.status(200).json(user); | ||
} else return res.status(200); | ||
} |
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,31 @@ | ||
import { getUserIdsByEmail, updateUser } from 'api/auth0'; | ||
import { runMiddleware } from 'api/middleware'; | ||
import { cors } from 'api/middleware/cors'; | ||
import { buildEvent, getEmailForCheckout } from 'api/stripe'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function stripeWebhook( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
await runMiddleware(req, res, cors); | ||
|
||
const event = buildEvent(req); | ||
|
||
if (event.type === 'checkout.session.completed') { | ||
const sessionId = (event.data.object as any).id; | ||
const email = await getEmailForCheckout(sessionId); | ||
|
||
console.log(email); | ||
|
||
const usersToUpdate = await getUserIdsByEmail(email); | ||
|
||
const userUpdates = usersToUpdate.map((id) => | ||
updateUser(id, { premium: true }), | ||
); | ||
|
||
await Promise.all(userUpdates); | ||
} | ||
|
||
return; | ||
} |
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
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,3 @@ | ||
export type UserMetadata = { | ||
premium: boolean; | ||
}; |
Oops, something went wrong.