slate/pages/api/keys/delete.js
2020-10-30 16:17:55 -07:00

74 lines
1.7 KiB
JavaScript

import * as Environment from "~/node_common/environment";
import * as Data from "~/node_common/data";
import * as Utilities from "~/node_common/utilities";
import * as ViewerManager from "~/node_common/managers/viewer";
export default async (req, res) => {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res.status(500).send({ decorator: "SERVER_DELETE_API_KEY_AUTH", error: true });
}
const user = await Data.getUserById({
id,
});
if (!user) {
return res.status(404).send({
decorator: "SERVER_DELETE_API_KEY_USER_NOT_FOUND",
error: true,
});
}
if (user.error) {
return res.status(500).send({
decorator: "SERVER_DELETE_API_KEY_USER_NOT_FOUND",
error: true,
});
}
const key = await Data.getAPIKey({ id: req.body.data.id });
if (!key || key.owner_id !== user.id) {
return res.status(403).send({
decorator: "SERVER_DELETE_API_KEY_NOT_FOUND",
error: true,
});
}
if (!key) {
return res.status(404).send({
decorator: "SERVER_DELETE_API_KEY_NOT_FOUND",
error: true,
});
}
if (key.error) {
return res.status(500).send({
decorator: "SERVER_DELETE_API_KEY_NOT_FOUND",
error: true,
});
}
const response = await Data.deleteAPIKeyById({ id: key.id });
if (!response) {
return res.status(404).send({
decorator: "SERVER_DELETE_API_KEY_ERROR",
error: true,
});
}
if (response.error) {
return res.status(500).send({
decorator: "SERVER_DELETE_API_KEY_ERROR",
error: true,
});
}
let keys = await Data.getAPIKeysByUserId({ userId: user.id });
ViewerManager.hydratePartialKeys(keys, user.id);
return res.status(200).send({ decorator: "SERVER_DELETE_API_KEY" });
};