aboutsummaryrefslogtreecommitdiffstats
path: root/lib/register.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/register.js')
-rw-r--r--lib/register.js26
1 files changed, 12 insertions, 14 deletions
diff --git a/lib/register.js b/lib/register.js
index a1489d89..b43a0ce7 100644
--- a/lib/register.js
+++ b/lib/register.js
@@ -1,26 +1,26 @@
-import countries from "$lib/countries";
-import config from "$config";
-import { authenticator } from "otplib";
import bcrypt from "bcrypt";
-import { l, warn } from "$lib/logging";
+import { got } from "got";
+import { authenticator } from "otplib";
import { v4 } from "uuid";
+
+import config from "$config";
+import countries from "$lib/countries";
import { s, g, archive } from "$lib/db";
-import { got } from "got";
+import { l, warn } from "$lib/logging";
+import { fail, getUser } from "$lib/utils";
export default async (user, ip, requireChallenge) => {
let { profile, password, pubkey, username } = user;
l("registering", username);
- if (!username) throw new Error("Username required");
+ if (!username) fail("Username required");
username = username.replace(/ /g, "").toLowerCase();
let id = v4();
user.id = id;
- let exists =
- (await g(`user:${username}`)) || (await archive.get(`user:${username}`));
-
- if (exists) throw new Error(`Username ${username} taken`);
+ let exists = await getUser(username);
+ if (exists) fail(`Username ${username} taken`);
if (password) {
user.password = await bcrypt.hash(password, 1);
@@ -30,11 +30,9 @@ export default async (user, ip, requireChallenge) => {
if (config.ipregistry) {
try {
let {
- location: {
- country: { code },
- },
+ location: { country: { code } },
} = await got(
- `https://api.ipregistry.co/${ip}?key=${config.ipregistry}&fields=location.country.code`
+ `https://api.ipregistry.co/${ip}?key=${config.ipregistry}&fields=location.country.code`,
).json();
user.currency = countries[code];