Update index.js
Browse files
index.js
CHANGED
@@ -30,6 +30,7 @@ import { Database } from './database/database.js'
|
|
30 |
import { Readable } from "stream";
|
31 |
import { randomBytes } from "crypto";
|
32 |
import { AkenoaiJs } from "akenoaijs";
|
|
|
33 |
|
34 |
import {
|
35 |
CheckMilWare,
|
@@ -128,6 +129,17 @@ const AllJsonReques = {
|
|
128 |
},
|
129 |
},
|
130 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
app.get("/api/v1/json/all", async (req, res) => {
|
132 |
res.json(AllJsonReques);
|
133 |
});
|
@@ -189,6 +201,44 @@ app.get("/runtime", async (req, res) => {
|
|
189 |
res.send("Running lifetime");
|
190 |
})
|
191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
app.post("/api/v1/revoked-key", async (req, res) => {
|
193 |
const dbClient = new Database("AkenoXJs");
|
194 |
const collection = dbClient.collection("api_keys");
|
|
|
30 |
import { Readable } from "stream";
|
31 |
import { randomBytes } from "crypto";
|
32 |
import { AkenoaiJs } from "akenoaijs";
|
33 |
+
import mongoose from "mongoose";
|
34 |
|
35 |
import {
|
36 |
CheckMilWare,
|
|
|
129 |
},
|
130 |
},
|
131 |
}
|
132 |
+
|
133 |
+
const apiKeySchema = new mongoose.Schema({
|
134 |
+
key: { type: String, required: true, unique: true },
|
135 |
+
owner: { type: Number, required: true, unique: true },
|
136 |
+
email: { type: String },
|
137 |
+
createdAt: { type: Date, default: Date.now },
|
138 |
+
expiresAt: { type: Date, required: true, index: { expires: 0 } }
|
139 |
+
});
|
140 |
+
|
141 |
+
const ApiKey = mongoose.model("ApiKey", apiKeySchema);
|
142 |
+
|
143 |
app.get("/api/v1/json/all", async (req, res) => {
|
144 |
res.json(AllJsonReques);
|
145 |
});
|
|
|
201 |
res.send("Running lifetime");
|
202 |
})
|
203 |
|
204 |
+
app.post('/api/v2/generate-key', async (req, res) => {
|
205 |
+
try {
|
206 |
+
const newKey = generateAkenoKey();
|
207 |
+
const userIdNumber = Number(req.query.user_id);
|
208 |
+
const email = req.query.email;
|
209 |
+
|
210 |
+
if (isNaN(userIdNumber)) {
|
211 |
+
return res.status(400).json({ error: "Invalid or missing user_id" });
|
212 |
+
}
|
213 |
+
|
214 |
+
const existingUser = await ApiKey.findOne({ owner: userIdNumber });
|
215 |
+
|
216 |
+
if (existingUser) {
|
217 |
+
return res.status(200).json({
|
218 |
+
apiKey: existingUser.key,
|
219 |
+
createdAt: existingUser.createdAt,
|
220 |
+
expiresAt: existingUser.expiresAt,
|
221 |
+
owner: existingUser.owner
|
222 |
+
});
|
223 |
+
}
|
224 |
+
|
225 |
+
const expiresAt = new Date();
|
226 |
+
expiresAt.setDate(expiresAt.getDate() + 7);
|
227 |
+
|
228 |
+
const userDocument = new ApiKey({
|
229 |
+
key: newKey,
|
230 |
+
createdAt: new Date(),
|
231 |
+
expiresAt,
|
232 |
+
owner: userIdNumber,
|
233 |
+
email: email || null
|
234 |
+
});
|
235 |
+
await userDocument.save();
|
236 |
+
res.json({ apiKey: newKey, expiresAt });
|
237 |
+
} catch (err) {
|
238 |
+
res.status(500).json({ error: `Key generation failed: ${err.message}` });
|
239 |
+
}
|
240 |
+
});
|
241 |
+
|
242 |
app.post("/api/v1/revoked-key", async (req, res) => {
|
243 |
const dbClient = new Database("AkenoXJs");
|
244 |
const collection = dbClient.collection("api_keys");
|