slate/pages/api/users/create.js

108 lines
3.2 KiB
JavaScript
Raw Normal View History

import * as Environment from "~/node_common/environment";
import * as Data from "~/node_common/data";
import * as Utilities from "~/node_common/utilities";
2020-10-06 01:33:37 +03:00
import * as SlateManager from "~/node_common/managers/slate";
import * as LibraryManager from "~/node_common/managers/library";
import * as Monitor from "~/node_common/monitor";
import * as Validations from "~/common/validations";
2020-10-29 21:44:03 +03:00
import * as Strings from "~/common/strings";
import BCrypt from "bcrypt";
import { PrivateKey } from "@textile/hub";
export default async (req, res) => {
2020-10-29 21:39:40 +03:00
if (!Strings.isEmpty(Environment.ALLOWED_HOST) && req.headers.host !== Environment.ALLOWED_HOST) {
return res.status(403).send({ decorator: "YOU_ARE_NOT_ALLOWED", error: true });
}
if (Strings.isEmpty(req.body.data.accepted)) {
return res.status(403).send({ decorator: "YOU_MUST_ACCEPT_TERMS", error: true });
}
const existing = await Data.getUserByUsername({
username: req.body.data.username,
});
if (existing) {
2020-10-05 00:30:28 +03:00
return res.status(403).send({ decorator: "SERVER_EXISTING_USER_ALREADY", error: true });
}
if (!Validations.username(req.body.data.username)) {
2020-10-05 00:30:28 +03:00
return res.status(500).send({ decorator: "SERVER_INVALID_USERNAME", error: true });
}
if (!Validations.password(req.body.data.password)) {
2020-10-05 00:30:28 +03:00
return res.status(500).send({ decorator: "SERVER_INVALID_PASSWORD", error: true });
}
2020-08-11 08:15:39 +03:00
const rounds = Number(Environment.LOCAL_PASSWORD_ROUNDS);
const salt = await BCrypt.genSalt(rounds);
const hash = await Utilities.encryptPassword(req.body.data.password, salt);
2020-08-11 08:15:39 +03:00
// TODO(jim):
// Single Key Textile Auth.
const identity = await PrivateKey.fromRandom();
const api = identity.toString();
// TODO(jim):
// Don't do this once you refactor.
const newUsername = req.body.data.username.toLowerCase();
2020-10-05 00:30:28 +03:00
const { buckets, bucketKey, bucketName } = await Utilities.getBucketAPIFromUserToken({
2020-09-23 12:46:59 +03:00
user: {
username: newUsername,
data: { tokens: { api } },
},
});
if (!buckets) {
2020-10-05 00:30:28 +03:00
return res.status(500).send({ decorator: "SERVER_BUCKET_INIT_FAILURE", error: true });
}
const photo = await SlateManager.getRandomSlateElementURL({
id: Environment.AVATAR_SLATE_ID,
fallback:
"https://slate.textile.io/ipfs/bafkreick3nscgixwfpq736forz7kzxvvhuej6kszevpsgmcubyhsx2pf7i",
});
const user = await Data.createUser({
2020-08-11 08:15:39 +03:00
password: hash,
salt,
username: newUsername,
data: {
photo,
2020-10-05 00:30:28 +03:00
body: "",
settings_deals_auto_approve: false,
2020-09-29 00:30:12 +03:00
allow_filecoin_directory_listing: false,
allow_automatic_data_storage: true,
allow_encrypted_data_storage: true,
2020-08-24 09:50:52 +03:00
tokens: { api },
library: LibraryManager.init({ bucketName, readableName: "Data" }),
},
});
if (!user) {
2020-10-05 00:30:28 +03:00
return res.status(404).send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true });
}
if (user.error) {
2020-10-05 00:30:28 +03:00
return res.status(500).send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true });
}
Monitor.createUser({
userId: user.id,
data: {
actorUserId: user.id,
context: {
username: user.username,
},
},
});
return res.status(200).send({
decorator: "SERVER_USER_CREATE",
user: { username: user.username, id: user.id },
});
};