diff options
| author | Adam Soltys <[email protected]> | 2023-02-06 16:48:13 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-02-06 16:48:13 -0800 |
| commit | 9c667055c6367e1b2e239686e72f52995679a47e (patch) | |
| tree | 0b1d4aee51ec2d4e92d08a9d1404ba4e2ecd462a /lib/register.js | |
| parent | 15294844dbc8e73b4d1e92008a7eebc6b86b9ec5 (diff) | |
| parent | 6f5602d112b68adb7452abf3047f20c32e7725b6 (diff) | |
| download | coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.tar.gz coinos-server-9c667055c6367e1b2e239686e72f52995679a47e.zip | |
Merge branch 'master' into test
Diffstat (limited to 'lib/register.js')
| -rw-r--r-- | lib/register.js | 76 |
1 files changed, 39 insertions, 37 deletions
diff --git a/lib/register.js b/lib/register.js index 21c113c0..e4bbf526 100644 --- a/lib/register.js +++ b/lib/register.js @@ -1,60 +1,62 @@ 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 { s, g } from "$lib/db"; +import { got } from "got"; export default async (user, ip, requireChallenge) => { - if (!(user && user.username)) throw new Error("Username required"); - user.username = user.username.replace(/ /g, ""); + let { password, pubkey, username } = user; + l("registering", username); - 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 id = v4(); + user.id = id; + + let exists = await g(`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}`, { - headers: { authorization: `Bearer ${config.ipxapi}` } - }); + user.currency = "USD"; + if (config.ipxapi) { + let countryCode = "US"; + + try { + let r = await got(`https://ipxapi.com/api/ip?ip=${ip}`, { + headers: { authorization: `Bearer ${config.ipxapi}` } + }).json(); + if (r.success) ({ countryCode } = r); + } catch (e) { + console.log(e); + } - user.currency = countries[data.countryCode] || "USD"; + user.currency = countries[countryCode]; } user.currencies = [...new Set([user.currency, "CAD", "USD"])]; user.fiat = false; user.otpsecret = authenticator.generateSecret(); + user.migrated = true; - user = await db.User.create(user); - let balance = config.faucet ? 21000000 : 0; + await s(`balance:${id}`, 21000000); - let account = await db.Account.create({ - user_id: user.id, - asset: config.liquid.btcasset, - balance, - 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 s(`user:${id}`, user); + await s(`user:${user.username}`, id); + await s(`user:${pubkey}`, id); + + l("new user", user.username); return user; }; |
