fix linting errors

This commit is contained in:
Puyodead1 2023-01-21 14:45:13 -05:00 committed by Puyodead1
parent 6b8b42ce9a
commit 97bafa81fc
4 changed files with 19 additions and 64 deletions

View File

@ -17,11 +17,7 @@
*/
import { route, verifyCaptcha } from "@fosscord/api";
import {
Config,
FieldErrors,
verifyTokenEmailVerification,
} from "@fosscord/util";
import { checkToken, Config, FieldErrors } from "@fosscord/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router = Router();
@ -47,10 +43,7 @@ router.post(
try {
const { jwtSecret } = Config.get().security;
const { decoded, user } = await verifyTokenEmailVerification(
token,
jwtSecret,
);
const { decoded, user } = await checkToken(token, jwtSecret);
// toksn should last for 24 hours from the time they were issued
if (new Date().getTime() > decoded.iat * 1000 + 86400 * 1000) {
@ -71,8 +64,8 @@ router.post(
// TODO: invalidate token after use?
return res.send(user);
} catch (error: any) {
throw new HTTPError(error?.toString(), 400);
} catch (error) {
throw new HTTPError((error as Error).toString(), 400);
}
},
);

View File

@ -31,7 +31,14 @@ import { ConnectedAccount } from "./ConnectedAccount";
import { Member } from "./Member";
import { UserSettings } from "./UserSettings";
import { Session } from "./Session";
import { Config, FieldErrors, Snowflake, trimSpecial, adjustEmail, Email, generateToken } from "..";
import {
Config,
FieldErrors,
Snowflake,
trimSpecial,
adjustEmail,
Email,
} from "..";
import { Request } from "express";
import { SecurityKey } from "./SecurityKey";

View File

@ -18,7 +18,7 @@
import fs from "node:fs";
import path from "node:path";
import nodemailer, { Transporter } from "nodemailer";
import nodemailer, { SentMessageInfo, Transporter } from "nodemailer";
import { User } from "../entities";
import { Config } from "./Config";
import { generateToken } from "./Token";
@ -158,7 +158,10 @@ export const Email: {
transporter: Transporter | null;
init: () => Promise<void>;
generateVerificationLink: (id: string, email: string) => Promise<string>;
sendVerificationEmail: (user: User, email: string) => Promise<any>;
sendVerificationEmail: (
user: User,
email: string,
) => Promise<SentMessageInfo>;
doReplacements: (
template: string,
user: User,
@ -254,10 +257,7 @@ export const Email: {
const link = `${instanceUrl}/verify#token=${token}`;
return link;
},
sendVerificationEmail: async function (
user: User,
email: string,
): Promise<any> {
sendVerificationEmail: async function (user: User, email: string) {
if (!this.transporter) return;
// generate a verification link for the user

View File

@ -72,58 +72,13 @@ export function checkToken(
});
}
/**
* Puyodead1 (1/19/2023): I made a copy of this function because I didn't want to break anything with the other one.
* this version of the function doesn't use select, so we can update the user. with select causes constraint errors.
*/
export function verifyTokenEmailVerification(
token: string,
jwtSecret: string,
): Promise<{ decoded: any; user: User }> {
return new Promise((res, rej) => {
jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => {
if (err || !decoded) return rej("Invalid Token");
const user = await User.findOne({
where: { id: decoded.id },
});
if (!user) return rej("Invalid Token");
if (user.disabled) return rej("User disabled");
if (user.deleted) return rej("User not found");
return res({ decoded, user });
});
});
}
export function verifyToken(
token: string,
jwtSecret: string,
): Promise<{ decoded: any; user: User }> {
return new Promise((res, rej) => {
jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => {
if (err || !decoded) return rej("Invalid Token");
const user = await User.findOne({
where: { id: decoded.id },
select: ["data", "bot", "disabled", "deleted", "rights"],
});
if (!user) return rej("Invalid Token");
if (user.disabled) return rej("User disabled");
if (user.deleted) return rej("User not found");
return res({ decoded, user });
});
});
}
export async function generateToken(id: string, email?: string) {
const iat = Math.floor(Date.now() / 1000);
const algorithm = "HS256";
return new Promise((res, rej) => {
jwt.sign(
{ id: id, email: email, iat },
{ id, iat, email },
Config.get().security.jwtSecret,
{
algorithm,