Continued updating the e-mail verification and I think I managed to get it working
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
import {getAuth, sendSignInLinkToEmail, User} from "firebase/auth";
|
||||
import {app} from "@/firebase";
|
||||
import {getAuth, sendEmailVerification, sendSignInLinkToEmail, User} from "firebase/auth";
|
||||
import {getAuth as getAdminAuth, UserRecord} from "firebase-admin/auth";
|
||||
import {app, adminApp} from "@/firebase";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
|
||||
@@ -11,7 +12,7 @@ export default withIronSessionApiRoute(sendVerification, sessionOptions);
|
||||
async function sendVerification(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.session.user) {
|
||||
sendSignInLinkToEmail(auth, req.session.user.email, {
|
||||
url: "https://encoach.com/",
|
||||
url: `https://encoach.com/${req.session.user.email}`,
|
||||
handleCodeInApp: true,
|
||||
})
|
||||
.then(() => res.status(200).json({ok: true}))
|
||||
|
||||
@@ -1,32 +1,26 @@
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
import {getAuth, signInWithEmailLink} from "firebase/auth";
|
||||
import {app} from "@/firebase";
|
||||
import {getAuth} from "firebase-admin/auth";
|
||||
import {adminApp, app} from "@/firebase";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {doc, getFirestore, setDoc} from "firebase/firestore";
|
||||
|
||||
const auth = getAuth(app);
|
||||
const auth = getAuth(adminApp);
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(verify, sessionOptions);
|
||||
|
||||
async function verify(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {link, email} = req.body as {link: string; email: string};
|
||||
const {email} = req.body as {email: string};
|
||||
|
||||
console.log("HERE FOR WHEN VERIFY: ", {link, email});
|
||||
const user = await auth.getUserByEmail(email);
|
||||
if (!user) {
|
||||
res.status(404).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
signInWithEmailLink(auth, email, link)
|
||||
.then(async () => {
|
||||
const userRef = doc(db, "users", req.session.user!.id);
|
||||
await setDoc(userRef, {isVerified: true}, {merge: true});
|
||||
const userRef = doc(db, "users", user.uid);
|
||||
await setDoc(userRef, {isVerified: true}, {merge: true});
|
||||
|
||||
req.session.user = {...req.session.user!, isVerified: true};
|
||||
await req.session.save();
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log("HERE FOR WHEN FAIL VERIFY: ", e);
|
||||
res.status(404).json({ok: false});
|
||||
});
|
||||
res.status(200).json({ok: true});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user