aboutsummaryrefslogtreecommitdiffstats
path: root/lib/register.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-08 21:05:34 -0800
committerAdam Soltys <[email protected]>2023-01-08 21:05:34 -0800
commitdce07aa93b3c2f47aca0c24cea6adde9ec37f1a7 (patch)
tree6c6b8930ebe9c2cc5b8d1f759e61260f509fdd7a /lib/register.js
parentf7b844883506a8072213245b9b8a2b988694e4f5 (diff)
downloadcoinos-server-dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7.tar.gz
coinos-server-dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7.zip
tech debt jubilee
Diffstat (limited to 'lib/register.js')
-rw-r--r--lib/register.js56
1 files changed, 23 insertions, 33 deletions
diff --git a/lib/register.js b/lib/register.js
index 6f8f366b..fd6f27ae 100644
--- a/lib/register.js
+++ b/lib/register.js
@@ -1,59 +1,49 @@
import countries from "$lib/countries";
import config from "$config";
import { authenticator } from "otplib";
-import db from "$db";
-import axios from "axios";
import bcrypt from "bcrypt";
import { l } from "$lib/logging";
+import { v4 } from "uuid";
+import redis from "$lib/redis";
export default async (user, ip, requireChallenge) => {
- if (!(user && user.username)) throw new Error("Username required");
- user.username = user.username.replace(/ /g, "");
+ let { password, username } = user;
- let exists = await db.User.count({ where: { username: user.username } });
- if (exists) throw new Error(`Username ${user.username} taken`);
+ if (!username) throw new Error("Username required");
- if (user.password) {
- user.password = await bcrypt.hash(user.password, 1);
+ username = username.replace(/ /g, "");
+ let uuid = v4();
+ user.uuid = uuid;
+
+ let exists = await redis.get(`user:${username}`);
+ if (exists) throw new Error(`Username ${username} taken`);
+
+ if (password) {
+ user.password = await bcrypt.hash(password, 1);
}
if (!config.ipxapi || ip.startsWith("127") || ip.startsWith("192")) {
user.currency = "CAD";
} else {
- let { data } = await axios.get(`https://ipxapi.com/api/ip?ip=${ip}`, {
+ let { countryCode } = await got(`https://ipxapi.com/api/ip?ip=${ip}`, {
headers: { authorization: `Bearer ${config.ipxapi}` }
- });
+ }).json();
- user.currency = countries[data.countryCode] || "USD";
+ user.currency = countries[countryCode] || "USD";
}
user.currencies = [...new Set([user.currency, "CAD", "USD"])];
user.fiat = false;
user.otpsecret = authenticator.generateSecret();
- user = await db.User.create(user);
-
- let account = await db.Account.create({
- user_id: user.id,
- asset: config.liquid.btcasset,
- balance: 0,
- pending: 0,
- name: "Bitcoin",
- ticker: "BTC",
- precision: 8
- });
-
- user.accounts = [account];
-
- const d = ip.split(".");
- const numericIp = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
- if (Number.isInteger(numericIp)) {
- user.ip = numericIp;
- const ipExists = await db.User.findOne({ where: { ip: numericIp } });
+ let d = ip.split(".");
+ ip = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
+ if (Number.isInteger(ip)) {
+ user.ip = ip;
}
- user.account_id = account.id;
- await user.save();
- l("new user", user.username, ip);
+ await redis.set(`user:${user.uuid}`, user);
+ await redis.set(`user:${user.username}`, user);
+ l("new user", user.username);
return user;
};