blob: fd6f27aeb332c21a42dd17f65c099703ab54d0af (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import countries from "$lib/countries";
import config from "$config";
import { authenticator } from "otplib";
import bcrypt from "bcrypt";
import { l } from "$lib/logging";
import { v4 } from "uuid";
import redis from "$lib/redis";
export default async (user, ip, requireChallenge) => {
let { password, username } = user;
if (!username) throw new Error("Username required");
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 { countryCode } = await got(`https://ipxapi.com/api/ip?ip=${ip}`, {
headers: { authorization: `Bearer ${config.ipxapi}` }
}).json();
user.currency = countries[countryCode] || "USD";
}
user.currencies = [...new Set([user.currency, "CAD", "USD"])];
user.fiat = false;
user.otpsecret = authenticator.generateSecret();
let d = ip.split(".");
ip = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
if (Number.isInteger(ip)) {
user.ip = ip;
}
await redis.set(`user:${user.uuid}`, user);
await redis.set(`user:${user.username}`, user);
l("new user", user.username);
return user;
};
|