Created a route for the Stripe webhook
This commit is contained in:
@@ -37,7 +37,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const codeData = codeDocs[0].data() as {code: string; type: Type; creator: string; expiryDate: Date | null};
|
||||
const codeData = codeDocs[0].data() as {code: string; type: Type; creator?: string; expiryDate: Date | null};
|
||||
|
||||
createUserWithEmailAndPassword(auth, email, password)
|
||||
.then(async (userCredentials) => {
|
||||
@@ -57,7 +57,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
await setDoc(doc(db, "users", userId), user);
|
||||
await setDoc(codeDocs[0].ref, {userId: userId}, {merge: true});
|
||||
await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
|
||||
if (codeData.creator) await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
|
||||
|
||||
req.session.user = {...user, id: userId};
|
||||
await req.session.save();
|
||||
|
||||
43
src/pages/api/stripe.ts
Normal file
43
src/pages/api/stripe.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, setDoc, doc} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {Type} from "@/interfaces/user";
|
||||
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||
import {uuidv4} from "@firebase/util";
|
||||
import {prepareMailer, prepareMailOptions} from "@/email";
|
||||
import * as Stripe from "stripe";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, expiryDate, key} = req.body as {email: string; expiryDate: Date; key: string};
|
||||
if (!key || key !== process.env.STRIPE_KEY) {
|
||||
res.status(403).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const uid = new ShortUniqueId();
|
||||
const code = uid.randomUUID(6);
|
||||
|
||||
const codeRef = doc(db, "codes", code);
|
||||
await setDoc(codeRef, {type: "student", code, expiryDate});
|
||||
|
||||
const transport = prepareMailer();
|
||||
const mailOptions = prepareMailOptions(
|
||||
{
|
||||
type: "student",
|
||||
code,
|
||||
},
|
||||
[email],
|
||||
"EnCoach Registration",
|
||||
"main",
|
||||
);
|
||||
|
||||
await transport.sendMail(mailOptions);
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
}
|
||||
Reference in New Issue
Block a user