2023-04-18 03:41:36 +02:00
|
|
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
2023-04-18 07:40:11 +02:00
|
|
|
import {databasePromise} from "../../../libs/cloud/mongodb";
|
2023-04-18 05:21:16 +02:00
|
|
|
import {getServerSession} from "next-auth"
|
|
|
|
import {authOptions} from "../auth/[...nextauth]";
|
2023-04-18 03:41:36 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2023-04-18 05:21:16 +02:00
|
|
|
const session = await getServerSession(req, res, authOptions)
|
2023-04-18 04:39:39 +02:00
|
|
|
let db = await databasePromise;
|
|
|
|
|
2023-04-18 05:21:16 +02:00
|
|
|
const { method, body } = req;
|
2023-04-18 04:39:39 +02:00
|
|
|
|
2023-04-18 05:21:16 +02:00
|
|
|
switch (method) {
|
|
|
|
case 'GET':
|
2023-04-18 07:40:11 +02:00
|
|
|
let cloudConfig = await db.collection('cloud_configs').findOne({ userId: session.userId });
|
|
|
|
res.send(JSON.stringify(cloudConfig, null, 2))
|
2023-04-18 05:21:16 +02:00
|
|
|
break;
|
|
|
|
case 'POST': // set
|
|
|
|
let { config } = body;
|
|
|
|
await db.collection('cloud_configs').updateOne(
|
|
|
|
{ userId: session.userId },
|
|
|
|
{
|
|
|
|
$set: { config },
|
|
|
|
},
|
|
|
|
{ upsert: true },
|
|
|
|
);
|
|
|
|
res.send(JSON.stringify(session, null, 2))
|
|
|
|
break;
|
2023-04-18 03:41:36 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 04:39:39 +02:00
|
|
|
|
2023-04-18 03:41:36 +02:00
|
|
|
};
|