2020-07-21 14:36:50 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-17 13:24:20 +03:00
|
|
|
import * as MW from "~/node_common/middleware";
|
|
|
|
import * as Data from "~/node_common/data";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-18 02:45:49 +03:00
|
|
|
import { Buckets } from "@textile/hub";
|
|
|
|
import { Libp2pCryptoIdentity } from "@textile/threads-core";
|
|
|
|
|
2020-07-22 08:53:29 +03:00
|
|
|
import JWT from "jsonwebtoken";
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
const initCORS = MW.init(MW.CORS);
|
2020-07-22 07:11:13 +03:00
|
|
|
const initAuth = MW.init(MW.RequireCookieAuthentication);
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-18 02:45:49 +03:00
|
|
|
const TEXTILE_KEY_INFO = {
|
2020-07-21 14:36:50 +03:00
|
|
|
key: Environment.TEXTILE_HUB_KEY,
|
|
|
|
secret: Environment.TEXTILE_HUB_SECRET,
|
2020-07-18 02:45:49 +03:00
|
|
|
};
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
export default async (req, res) => {
|
|
|
|
initCORS(req, res);
|
2020-07-22 07:11:13 +03:00
|
|
|
initAuth(req, res);
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
2020-08-03 01:06:46 +03:00
|
|
|
return res.status(500).json({ decorator: "SERVER_USER_DELETE", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
2020-07-18 02:45:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2020-08-03 01:06:46 +03:00
|
|
|
return res.status(404).json({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
|
2020-07-18 02:45:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2020-08-03 01:06:46 +03:00
|
|
|
return res.status(500).json({ decorator: "SERVER_USER_DELETE_USER_NOT_FOUND", error: true });
|
2020-07-18 02:45:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-28 09:54:15 +03:00
|
|
|
await Data.deleteAPIKeysForUserId({ userId: user.id });
|
2020-08-03 01:06:46 +03:00
|
|
|
await Data.deleteSlatesForUserId({ userId: user.id });
|
2020-07-28 09:54:15 +03:00
|
|
|
|
2020-07-18 02:45:49 +03:00
|
|
|
const i = await Libp2pCryptoIdentity.fromString(user.data.tokens.api);
|
|
|
|
const b = await Buckets.withKeyInfo(TEXTILE_KEY_INFO);
|
2020-08-03 01:06:46 +03:00
|
|
|
const tokenResponse = await b.getToken(i);
|
|
|
|
const openResponse = await b.open("data");
|
2020-07-20 09:06:36 +03:00
|
|
|
|
2020-07-18 02:45:49 +03:00
|
|
|
try {
|
2020-08-03 01:06:46 +03:00
|
|
|
const response = await b.remove(openResponse.key);
|
2020-07-18 02:45:49 +03:00
|
|
|
console.log({ response });
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
const deleted = await Data.deleteUserByUsername({
|
2020-07-22 13:59:39 +03:00
|
|
|
username: user.username,
|
2020-07-17 13:24:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!deleted) {
|
2020-08-03 01:06:46 +03:00
|
|
|
return res.status(200).json({ decorator: "SERVER_USER_DELETE", error: true });
|
2020-07-17 13:24:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).json({ decorator: "SERVER_USER_DELETE", deleted });
|
|
|
|
};
|