1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-22 10:22:39 +01:00

🐛 update imports

This commit is contained in:
Flam3rboy 2021-02-06 10:09:32 +01:00
parent f802c37559
commit ce07018e30
5 changed files with 16 additions and 20 deletions

View File

@ -1,3 +1,4 @@
import "missing-native-js-functions";
import fs from "fs/promises";
import { Server, ServerOptions } from "lambert-server";
import { Authentication, GlobalRateLimit } from "./middlewares/";

View File

@ -1,8 +1,6 @@
import jwt from "jsonwebtoken";
import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import Config from "../util/Config";
import { JWTOptions } from "../util/Constants";
import { checkToken } from "discord-server-util";
export const NO_AUTHORIZATION_ROUTES = ["/api/v8/auth/login", "/api/v8/auth/register"];
@ -15,16 +13,13 @@ declare global {
}
}
export function Authentication(req: Request, res: Response, next: NextFunction) {
export async function Authentication(req: Request, res: Response, next: NextFunction) {
if (NO_AUTHORIZATION_ROUTES.includes(req.url)) return next();
if (!req.headers.authorization) return next(new HTTPError("Missing Authorization Header", 401));
// TODO: check if user is banned/token expired
return jwt.verify(req.headers.authorization, Config.get().security.jwtSecret, JWTOptions, (err, decoded: any) => {
if (err || !decoded) return next(new HTTPError("Invalid Token", 401));
const decoded: any = await checkToken(req.headers.authorization);
req.token = decoded;
req.userid = decoded.id;
return next();
});
req.token = decoded;
req.userid = decoded.id;
}

View File

@ -2,10 +2,9 @@ import { Request, Response, Router } from "express";
import { check, FieldErrors, Length } from "../../../../util/instanceOf";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { db, User } from "discord-server-util";
import Config from "../../../../util/Config";
import { User } from "../../../../models/User";
import { adjustEmail } from "./register";
import { db } from "discord-server-util";
const router: Router = Router();
export default router;

View File

@ -1,13 +1,10 @@
import { NextFunction, Request, Response, Router } from "express";
import { Request, Response, Router } from "express";
import Config from "../../../../util/Config";
import { db } from "discord-server-util";
import { db, trimSpecial, User, Snowflake } from "discord-server-util";
import bcrypt from "bcrypt";
import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../../../util/instanceOf";
import { Snowflake } from "../../../../util/Snowflake";
import "missing-native-js-functions";
import { User } from "../../../../models/User";
import { generateToken } from "./login";
import { trimSpecial } from "../../../../util/String";
const router: Router = Router();
@ -54,6 +51,7 @@ router.post(
// discriminator will be randomly generated
let discriminator = "";
// get register Config
const { register } = Config.get();
// check if registration is allowed
@ -70,7 +68,7 @@ router.post(
});
}
// require invite to register -> for organizations to send invites to their employees
// require invite to register -> e.g. for organizations to send invites to their employees
if (register.requireInvite && !invite) {
throw FieldErrors({
email: { code: "INVITE_ONLY", message: req.t("auth:register.INVITE_ONLY") },
@ -78,6 +76,7 @@ router.post(
}
if (email) {
// replace all dots and chars after +, if its a gmail.com email
adjusted_email = adjustEmail(email);
// check if there is already an account with this email
@ -164,6 +163,7 @@ router.post(
});
}
// constructing final user object
const user: User = {
id: Snowflake.generate(),
created_at: Date.now(),
@ -218,6 +218,7 @@ router.post(
},
};
// insert user into database
await db.data.users.push(user);
return res.json({ token: await generateToken(user.id) });

View File

@ -1,6 +1,6 @@
import mongoose from "mongoose";
import { Long } from "mongodb";
import { Snowflake } from "../util/Snowflake";
import { Snowflake } from "../../../server-util/src/util/Snowflake";
async function main() {
const conn = await mongoose.createConnection(