From dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 8 Jan 2023 21:05:34 -0800 Subject: tech debt jubilee --- lib/register.js | 56 +++++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) (limited to 'lib/register.js') 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; }; -- cgit v1.2.3