forked from rahulJalan23/groq-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.json
60 lines (51 loc) · 1.67 KB
/
storage.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
changes:
// pages/api/conversations.js
import { NextApiRequest, NextApiResponse } from 'next';
import { fauna } from '../../lib/fauna';
import { cryptoJs } from 'crypto-js';
import { NextAuth } from 'next-auth';
const conversations = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method === 'POST') {
const { conversationId, message } = req.body;
const user = req.user; // authenticated user
// Input validation
if (!conversationId || !message) {
return res.status(400).json({ error: 'Invalid input' });
}
// User authentication
if (!user) {
return res.status(401).json({ error: 'Unauthorized' });
}
// Encrypt conversation data
const encryptionKey = process.env.ENCRYPTION_KEY;
const encryptedMessage = await encryptMessage(message, encryptionKey);
// Store conversation data in FaunaDB
const conversation = await fauna.query(
q.Create(q.Collection('conversations'), {
data: {
conversationId,
message: encryptedMessage,
userId: user.id,
},
})
);
res.status(201).json(conversation);
} else {
res.status(405).json({ error: 'Method not allowed' });
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
};
const encryptMessage = async (message: string, encryptionKey: string) => {
try {
const encryptedMessage = await cryptoJs.AES.encrypt(message, encryptionKey);
return encryptedMessage.toString();
} catch (error) {
console.error(error);
throw error;
}
};
export default NextAuth(conversations);