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-07-24 06:09:58 +03:00
|
|
|
import * as LibraryManager from "~/node_common/managers/library";
|
|
|
|
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-09-03 03:21:29 +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-09-03 03:21:29 +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-09-03 03:21:29 +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-03 03:21:29 +03:00
|
|
|
const {
|
|
|
|
buckets,
|
|
|
|
bucketKey,
|
|
|
|
bucketName,
|
|
|
|
} = await Utilities.getBucketAPIFromUserToken(api);
|
2020-07-21 14:36:50 +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-08-06 23:01:05 +03:00
|
|
|
username: req.body.data.username.toLowerCase(),
|
2020-07-21 14:36:50 +03:00
|
|
|
data: {
|
2020-07-22 14:02:32 +03:00
|
|
|
photo: "https://slate.host/static/a1.jpg",
|
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-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-09-03 03:21:29 +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-09-03 03:21:29 +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-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
|
|
|
});
|
|
|
|
};
|