Started migrating the DB to MongoDB
This commit is contained in:
30
src/lib/mongodb.ts
Normal file
30
src/lib/mongodb.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import {MongoClient} from "mongodb";
|
||||
|
||||
if (!process.env.MONGODB_URI) {
|
||||
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
|
||||
}
|
||||
|
||||
const uri = process.env.MONGODB_URI;
|
||||
const options = {};
|
||||
|
||||
let client: MongoClient;
|
||||
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
// In development mode, use a global variable so that the value
|
||||
// is preserved across module reloads caused by HMR (Hot Module Replacement).
|
||||
let globalWithMongo = global as typeof globalThis & {
|
||||
_mongoClient?: MongoClient;
|
||||
};
|
||||
|
||||
if (!globalWithMongo._mongoClient) {
|
||||
globalWithMongo._mongoClient = new MongoClient(uri, options);
|
||||
}
|
||||
client = globalWithMongo._mongoClient;
|
||||
} else {
|
||||
// In production mode, it's best to not use a global variable.
|
||||
client = new MongoClient(uri, options);
|
||||
}
|
||||
|
||||
// Export a module-scoped MongoClient. By doing this in a
|
||||
// separate module, the client can be shared across functions.
|
||||
export default client;
|
||||
5
src/mongodb.d.ts
vendored
Normal file
5
src/mongodb.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import {MongoClient} from "mongodb";
|
||||
|
||||
declare global {
|
||||
var _mongoClientPromise: Promise<MongoClient>;
|
||||
}
|
||||
@@ -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