Skip to content

Commit

Permalink
extend auth & stripe utilities, draft webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Jul 24, 2023
1 parent 10768a6 commit 0b70457
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
40 changes: 33 additions & 7 deletions web/api/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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 },
{ premium: true }
>({
domain: host,
token: process.env.AUTH0_TOKEN!,
});
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,
};
};
10 changes: 10 additions & 0 deletions web/api/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ export function buildEvent(request: NextApiRequest) {

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!;
}
12 changes: 4 additions & 8 deletions web/pages/api/me.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { cors } from 'api/middleware/cors';
import { runMiddleware } from 'api/middleware';
import { auth0 } from 'api/auth0';
import { getUserById } from 'api/auth0';
import { getSession } from '@auth0/nextjs-auth0';

export default async function me(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -16,12 +16,8 @@ export default async function me(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession(req, res);

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

return res.status(200).json({
username: user.nickname,
email: user.email,
metadata: user.user_metadata,
});
}
return res.status(200).json(user);
} else return res.status(200);
}
23 changes: 19 additions & 4 deletions web/pages/api/stripe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getUserIdsByEmail, updateUser } from 'api/auth0';
import { runMiddleware } from 'api/middleware';
import { cors } from 'api/middleware/cors';
import { buildEvent } from 'api/stripe';
import { buildEvent, getEmailForCheckout } from 'api/stripe';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function stripeWebhook(
Expand All @@ -9,8 +10,22 @@ export default async function stripeWebhook(
) {
await runMiddleware(req, res, cors);

const stripeEvent = buildEvent(req);
const event = buildEvent(req);

if (stripeEvent.type === 'checkout.session.completed') {}
else return;
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;
}
3 changes: 3 additions & 0 deletions web/types/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type UserMetadata = {
premium: boolean;
};

0 comments on commit 0b70457

Please sign in to comment.