2020-07-23 11:57:44 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-17 13:24:20 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
2020-07-21 14:36:50 +03:00
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-10-06 01:33:37 +03:00
|
|
|
import * as SlateManager from "~/node_common/managers/slate";
|
2020-07-24 06:09:58 +03:00
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
2020-09-11 06:28:23 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-07-24 06:09:58 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
import BCrypt from "bcrypt";
|
|
|
|
|
2020-08-19 23:44:53 +03:00
|
|
|
import { PrivateKey } from "@textile/hub";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-22 08:53:29 +03:00
|
|
|
const existing = await Data.getUserByUsername({
|
|
|
|
username: req.body.data.username,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existing) {
|
2020-10-06 01:33:37 +03:00
|
|
|
return res.status(403).send({ decorator: "SERVER_EXISTING_USER_ALREADY", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
if (!Validations.username(req.body.data.username)) {
|
2020-10-06 01:33:37 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_INVALID_USERNAME", error: true });
|
2020-07-17 13:24:20 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
if (!Validations.password(req.body.data.password)) {
|
2020-10-06 01:33:37 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_INVALID_PASSWORD", error: true });
|
2020-07-17 13:24:20 +03:00
|
|
|
}
|
|
|
|
|
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-24 09:50:52 +03:00
|
|
|
// const pg = await Powergate.createNewToken();
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-08-11 08:15:39 +03:00
|
|
|
// TODO(jim):
|
|
|
|
// Single Key Textile Auth.
|
2020-08-19 23:44:53 +03:00
|
|
|
const identity = await PrivateKey.fromRandom();
|
2020-07-17 13:24:20 +03:00
|
|
|
const api = identity.toString();
|
|
|
|
|
2020-07-21 14:36:50 +03:00
|
|
|
// TODO(jim):
|
|
|
|
// Don't do this once you refactor.
|
2020-09-22 03:36:45 +03:00
|
|
|
const newUsername = req.body.data.username.toLowerCase();
|
|
|
|
|
2020-10-06 01:33:37 +03:00
|
|
|
const { buckets, bucketKey, bucketName } = await Utilities.getBucketAPIFromUserToken({
|
2020-09-23 12:46:59 +03:00
|
|
|
user: {
|
|
|
|
username: newUsername,
|
|
|
|
data: { tokens: { api } },
|
|
|
|
},
|
2020-09-22 03:36:45 +03:00
|
|
|
});
|
2020-07-21 14:36:50 +03:00
|
|
|
|
2020-09-22 10:01:48 +03:00
|
|
|
if (!buckets) {
|
2020-10-06 01:33:37 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_BUCKET_INIT_FAILURE", error: true });
|
2020-09-22 10:01:48 +03:00
|
|
|
}
|
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
const user = await Data.createUser({
|
2020-08-11 08:15:39 +03:00
|
|
|
password: hash,
|
2020-07-17 13:24:20 +03:00
|
|
|
salt,
|
2020-09-22 03:36:45 +03:00
|
|
|
username: newUsername,
|
2020-07-21 14:36:50 +03:00
|
|
|
data: {
|
2020-10-06 01:33:37 +03:00
|
|
|
photo: SlateManager.getRandomSlateElementURL({
|
|
|
|
id: Environment.AVATAR_SLATE_ID,
|
|
|
|
fallback:
|
|
|
|
"https://slate.textile.io/ipfs/bafkreick3nscgixwfpq736forz7kzxvvhuej6kszevpsgmcubyhsx2pf7i",
|
|
|
|
}),
|
2020-08-22 08:45:50 +03:00
|
|
|
body: "A user of Slate.",
|
2020-07-22 10:41:29 +03:00
|
|
|
settings_deals_auto_approve: false,
|
2020-09-29 00:30:12 +03:00
|
|
|
allow_filecoin_directory_listing: false,
|
2020-09-28 20:48:44 +03:00
|
|
|
allow_automatic_data_storage: true,
|
|
|
|
allow_encrypted_data_storage: true,
|
2020-08-24 09:50:52 +03:00
|
|
|
tokens: { api },
|
2020-07-24 10:45:21 +03:00
|
|
|
library: LibraryManager.init({ bucketName, readableName: "Data" }),
|
2020-07-21 14:36:50 +03:00
|
|
|
},
|
2020-07-17 13:24:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2020-10-06 01:33:37 +03:00
|
|
|
return res.status(404).send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true });
|
2020-07-17 13:24:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2020-10-06 01:33:37 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true });
|
2020-07-17 13:24:20 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 06:28:23 +03:00
|
|
|
const userProfileURL = `https://slate.host/${user.username}`;
|
|
|
|
const userURL = `<${userProfileURL}|${user.username}>`;
|
|
|
|
Social.sendSlackMessage(`*${userURL}* joined the movement.`);
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({
|
2020-07-17 13:24:20 +03:00
|
|
|
decorator: "SERVER_USER_CREATE",
|
2020-07-22 13:51:40 +03:00
|
|
|
user: { username: user.username, id: user.id },
|
2020-07-17 13:24:20 +03:00
|
|
|
});
|
|
|
|
};
|