🚧 api
This commit is contained in:
@@ -2,7 +2,7 @@ import { Request, Response, Router } from "express";
|
||||
import { check, FieldErrors, Length } from "../../util/instanceOf";
|
||||
import bcrypt from "bcrypt";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { Config, UserModel } from "@fosscord/util";
|
||||
import { Config, User } from "@fosscord/util";
|
||||
import { adjustEmail } from "./register";
|
||||
|
||||
const router: Router = Router();
|
||||
@@ -41,27 +41,25 @@ router.post(
|
||||
// TODO: check captcha
|
||||
}
|
||||
|
||||
const user = await UserModel.findOne(
|
||||
const user = await User.findOneOrFail(
|
||||
{ $or: query },
|
||||
{ "user_data.hash": true, id: true, disabled: true, deleted: true, "user_settings.locale": true, "user_settings.theme": true }
|
||||
)
|
||||
.exec()
|
||||
.catch((e) => {
|
||||
console.log(e, query);
|
||||
throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } });
|
||||
});
|
||||
{ "data.hash": true, id: true, disabled: true, deleted: true, "settings.locale": true, "settings.theme": true }
|
||||
).catch((e) => {
|
||||
console.log(e, query);
|
||||
throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } });
|
||||
});
|
||||
|
||||
if (undelete) {
|
||||
// undelete refers to un'disable' here
|
||||
if (user.disabled) await UserModel.updateOne({ id: user.id }, { disabled: false }).exec();
|
||||
if (user.deleted) await UserModel.updateOne({ id: user.id }, { deleted: false }).exec();
|
||||
if (user.disabled) await User.update({ id: user.id }, { disabled: false });
|
||||
if (user.deleted) await User.update({ id: user.id }, { deleted: false });
|
||||
} else {
|
||||
if (user.deleted) return res.status(400).json({ message: "This account is scheduled for deletion.", code: 20011 });
|
||||
if (user.disabled) return res.status(400).json({ message: req.t("auth:login.ACCOUNT_DISABLED"), code: 20013 });
|
||||
}
|
||||
|
||||
// the salt is saved in the password refer to bcrypt docs
|
||||
const same_password = await bcrypt.compare(password, user.user_data.hash || "");
|
||||
const same_password = await bcrypt.compare(password, user.data.hash || "");
|
||||
if (!same_password) {
|
||||
throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } });
|
||||
}
|
||||
@@ -72,7 +70,7 @@ router.post(
|
||||
// Discord header is just the user id as string, which is not possible with npm-jsonwebtoken package
|
||||
// https://user-images.githubusercontent.com/6506416/81051916-dd8c9900-8ec2-11ea-8794-daf12d6f31f0.png
|
||||
|
||||
res.json({ token, user_settings: user.user_settings });
|
||||
res.json({ token, settings: user.settings });
|
||||
}
|
||||
);
|
||||
|
||||
@@ -106,6 +104,6 @@ export async function generateToken(id: string) {
|
||||
* @returns {"captcha_key": ["captcha-required"], "captcha_sitekey": null, "captcha_service": "recaptcha"}
|
||||
|
||||
* Sucess:
|
||||
* @returns {"token": "USERTOKEN", "user_settings": {"locale": "en", "theme": "dark"}}
|
||||
* @returns {"token": "USERTOKEN", "settings": {"locale": "en", "theme": "dark"}}
|
||||
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Request, Response, Router } from "express";
|
||||
import { trimSpecial, User, Snowflake, UserModel, Config } from "@fosscord/util";
|
||||
import { trimSpecial, User, Snowflake, User, Config } from "@fosscord/util";
|
||||
import bcrypt from "bcrypt";
|
||||
import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../util/instanceOf";
|
||||
import "missing-native-js-functions";
|
||||
@@ -92,9 +92,7 @@ router.post(
|
||||
if (!adjusted_email) throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req.t("auth:register.INVALID_EMAIL") } });
|
||||
|
||||
// check if there is already an account with this email
|
||||
const exists = await UserModel.findOne({ email: adjusted_email })
|
||||
.exec()
|
||||
.catch((e) => {});
|
||||
const exists = await User.findOneOrFail({ email: adjusted_email }).catch((e) => {});
|
||||
|
||||
if (exists) {
|
||||
throw FieldErrors({
|
||||
@@ -131,9 +129,7 @@ router.post(
|
||||
|
||||
if (!register.allowMultipleAccounts) {
|
||||
// TODO: check if fingerprint was eligible generated
|
||||
const exists = await UserModel.findOne({ fingerprints: fingerprint })
|
||||
.exec()
|
||||
.catch((e) => {});
|
||||
const exists = await User.findOneOrFail({ fingerprints: fingerprint }).catch((e) => {});
|
||||
|
||||
if (exists) {
|
||||
throw FieldErrors({
|
||||
@@ -169,7 +165,7 @@ router.post(
|
||||
for (let tries = 0; tries < 5; tries++) {
|
||||
discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
|
||||
try {
|
||||
exists = await UserModel.findOne({ discriminator, username: adjusted_username }, "id").exec();
|
||||
exists = await User.findOneOrFail({ discriminator, username: adjusted_username }, "id");
|
||||
} catch (error) {
|
||||
// doesn't exist -> break
|
||||
break;
|
||||
@@ -223,14 +219,14 @@ router.post(
|
||||
public_flags: 0n,
|
||||
flags: 0n, // TODO: generate default flags
|
||||
guilds: [],
|
||||
user_data: {
|
||||
data: {
|
||||
hash: adjusted_password,
|
||||
valid_tokens_since: new Date(),
|
||||
relationships: [],
|
||||
connected_accounts: [],
|
||||
fingerprints: []
|
||||
},
|
||||
user_settings: {
|
||||
settings: {
|
||||
afk_timeout: 300,
|
||||
allow_accessibility_detection: true,
|
||||
animate_emoji: true,
|
||||
@@ -272,7 +268,7 @@ router.post(
|
||||
};
|
||||
|
||||
// insert user into database
|
||||
await new UserModel(user).save();
|
||||
await new User(user).save();
|
||||
|
||||
return res.json({ token: await generateToken(user.id) });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user