mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-11 05:02:37 +01:00
Merge pull request #428 from fosscord/dev
Various fixes and guest account enhancements
This commit is contained in:
commit
2599c5769b
@ -47,7 +47,7 @@
|
|||||||
|
|
||||||
// Auto register guest account:
|
// Auto register guest account:
|
||||||
const token = JSON.parse(localStorage.getItem("token"));
|
const token = JSON.parse(localStorage.getItem("token"));
|
||||||
if (!token) {
|
if (!token && location.pathname !== "/login" && location.pathname !== "/register") {
|
||||||
fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/auth/register`, {
|
fetch(`${window.GLOBAL_ENV.API_ENDPOINT}/auth/register`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
|
@ -154,16 +154,18 @@ router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Re
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!body.invite && (register.requireInvite || (register.guestsRequireInvite && !register.email))) {
|
||||||
|
// require invite to register -> e.g. for organizations to send invites to their employees
|
||||||
|
throw FieldErrors({
|
||||||
|
email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const user = await User.register({ ...body, req });
|
const user = await User.register({ ...body, req });
|
||||||
|
|
||||||
if (body.invite) {
|
if (body.invite) {
|
||||||
// await to fail if the invite doesn't exist (necessary for requireInvite to work properly) (username only signups are possible)
|
// await to fail if the invite doesn't exist (necessary for requireInvite to work properly) (username only signups are possible)
|
||||||
await Invite.joinGuild(user.id, body.invite);
|
await Invite.joinGuild(user.id, body.invite);
|
||||||
} else if (register.requireInvite) {
|
|
||||||
// require invite to register -> e.g. for organizations to send invites to their employees
|
|
||||||
throw FieldErrors({
|
|
||||||
email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json({ token: await generateToken(user.id) });
|
return res.json({ token: await generateToken(user.id) });
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Router, Request, Response } from "express";
|
import { Router, Request, Response } from "express";
|
||||||
import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile } from "@fosscord/util";
|
import { User, PrivateUserProjection, emitEvent, UserUpdateEvent, handleFile, FieldErrors } from "@fosscord/util";
|
||||||
import { route } from "@fosscord/api";
|
import { route } from "@fosscord/api";
|
||||||
|
import bcrypt from "bcrypt";
|
||||||
|
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
@ -32,10 +33,35 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
|
|||||||
if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
|
if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
|
||||||
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
|
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
|
||||||
|
|
||||||
await new User({ ...body, id: req.user_id }).save();
|
const user = await User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] });
|
||||||
|
|
||||||
|
if (body.password) {
|
||||||
|
if (user.data?.hash) {
|
||||||
|
const same_password = await bcrypt.compare(body.password, user.data.hash || "");
|
||||||
|
if (!same_password) {
|
||||||
|
throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
user.data.hash = await bcrypt.hash(body.password, 12);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
user.assign(body);
|
||||||
|
|
||||||
|
if (body.new_password) {
|
||||||
|
if (!body.password && !user.email) {
|
||||||
|
throw FieldErrors({
|
||||||
|
password: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
user.data.hash = await bcrypt.hash(body.new_password, 12);
|
||||||
|
}
|
||||||
|
|
||||||
|
await user.save();
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
delete user.data;
|
||||||
|
|
||||||
//Need to reload user from db due to https://github.com/typeorm/typeorm/issues/3490
|
|
||||||
const user = await User.findOneOrFail({ where: { id: req.user_id }, select: PrivateUserProjection });
|
|
||||||
// TODO: send update member list event in gateway
|
// TODO: send update member list event in gateway
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "USER_UPDATE",
|
event: "USER_UPDATE",
|
||||||
|
@ -37,7 +37,6 @@ function transpileFiles() {
|
|||||||
const files = walk(path.join(__dirname, "..", "..", part, "dist"));
|
const files = walk(path.join(__dirname, "..", "..", part, "dist"));
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
let content = fs.readFileSync(file, { encoding: "utf8" });
|
let content = fs.readFileSync(file, { encoding: "utf8" });
|
||||||
console.log(file);
|
|
||||||
content = content
|
content = content
|
||||||
.replace(
|
.replace(
|
||||||
new RegExp(`@fosscord/${part}`),
|
new RegExp(`@fosscord/${part}`),
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { Router, Response, Request } from "express";
|
import { Router, Response, Request } from "express";
|
||||||
import { Config, Snowflake } from "@fosscord/util";
|
import { Config, Snowflake } from "@fosscord/util";
|
||||||
import { storage } from "@fosscord/cdn";
|
import { storage } from "../util/Storage";
|
||||||
import FileType from "file-type";
|
import FileType from "file-type";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { multer } from "@fosscord/cdn";
|
import { multer } from "../util/multer";
|
||||||
import imageSize from "image-size";
|
import imageSize from "image-size";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Router, Response, Request } from "express";
|
import { Router, Response, Request } from "express";
|
||||||
import { Config, Snowflake } from "@fosscord/util";
|
import { Config, Snowflake } from "@fosscord/util";
|
||||||
import { storage } from "@fosscord/cdn";
|
import { storage } from "../util/Storage";
|
||||||
import FileType from "file-type";
|
import FileType from "file-type";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
@ -2,7 +2,7 @@ import { Router, Response, Request } from "express";
|
|||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { Snowflake } from "@fosscord/util";
|
import { Snowflake } from "@fosscord/util";
|
||||||
import { storage } from "@fosscord/cdn";
|
import { storage } from "../util/Storage";
|
||||||
import FileType from "file-type";
|
import FileType from "file-type";
|
||||||
import { Config } from "@fosscord/util";
|
import { Config } from "@fosscord/util";
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ try {
|
|||||||
erlpack = require("@yukikaze-bot/erlpack");
|
erlpack = require("@yukikaze-bot/erlpack");
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
import OPCodeHandlers from "../opcodes";
|
import OPCodeHandlers from "../opcodes";
|
||||||
import { instanceOf, Tuple } from "lambert-server";
|
import { Tuple } from "lambert-server";
|
||||||
import { check } from "../opcodes/instanceOf";
|
import { check } from "../opcodes/instanceOf";
|
||||||
import WS from "ws";
|
import WS from "ws";
|
||||||
|
|
||||||
|
@ -128,6 +128,7 @@ export interface ConfigValue {
|
|||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
requireCaptcha: boolean;
|
requireCaptcha: boolean;
|
||||||
requireInvite: boolean;
|
requireInvite: boolean;
|
||||||
|
guestsRequireInvite: boolean;
|
||||||
allowNewRegistration: boolean;
|
allowNewRegistration: boolean;
|
||||||
allowMultipleAccounts: boolean;
|
allowMultipleAccounts: boolean;
|
||||||
blockProxies: boolean;
|
blockProxies: boolean;
|
||||||
@ -277,6 +278,7 @@ export const DefaultConfigOptions: ConfigValue = {
|
|||||||
},
|
},
|
||||||
disabled: false,
|
disabled: false,
|
||||||
requireInvite: false,
|
requireInvite: false,
|
||||||
|
guestsRequireInvite: true,
|
||||||
requireCaptcha: true,
|
requireCaptcha: true,
|
||||||
allowNewRegistration: true,
|
allowNewRegistration: true,
|
||||||
allowMultipleAccounts: true,
|
allowMultipleAccounts: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user