Started migrating the DB to MongoDB
This commit is contained in:
@@ -1,38 +1,35 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
|
||||
import { app } from "@/firebase";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { User } from "@/interfaces/user";
|
||||
import { getFirestore, getDoc, doc } from "firebase/firestore";
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
import {getAuth, signInWithEmailAndPassword} from "firebase/auth";
|
||||
import {app} from "@/firebase";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {User} from "@/interfaces/user";
|
||||
import {getFirestore, getDoc, doc} from "firebase/firestore";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const auth = getAuth(app);
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(login, sessionOptions);
|
||||
|
||||
async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { email, password } = req.body as { email: string; password: string };
|
||||
const {email, password} = req.body as {email: string; password: string};
|
||||
|
||||
signInWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
signInWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
|
||||
const docUser = await getDoc(doc(db, "users", userId));
|
||||
if (!docUser.exists()) {
|
||||
res.status(401).json({ error: 401, message: "User does not exist!" });
|
||||
return;
|
||||
}
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
const user = await db.collection("users").findOne<User>({id: userId});
|
||||
|
||||
const user = docUser.data() as User;
|
||||
if (!user) return res.status(401).json({error: 401, message: "User does not exist!"});
|
||||
|
||||
req.session.user = { ...user, id: userId };
|
||||
await req.session.save();
|
||||
req.session.user = {...user, id: userId};
|
||||
await req.session.save();
|
||||
|
||||
res.status(200).json({ user: { ...user, id: userId } });
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
res.status(401).json({ error });
|
||||
});
|
||||
res.status(200).json({user: {...user, id: userId}});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
res.status(401).json({error});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user