diff --git a/src/api/routes/auth/verify/index.ts b/src/api/routes/auth/verify/index.ts
index d61b8d16..7809bc26 100644
--- a/src/api/routes/auth/verify/index.ts
+++ b/src/api/routes/auth/verify/index.ts
@@ -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);
}
},
);
diff --git a/src/util/entities/User.ts b/src/util/entities/User.ts
index 4a399ed9..42f74fb4 100644
--- a/src/util/entities/User.ts
+++ b/src/util/entities/User.ts
@@ -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";
diff --git a/src/util/util/Email.ts b/src/util/util/Email.ts
index 8899b3c2..cbcc5b60 100644
--- a/src/util/util/Email.ts
+++ b/src/util/util/Email.ts
@@ -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
diff --git a/src/util/util/Token.ts b/src/util/util/Token.ts
index e4b1fe41..12e4a79a 100644
--- a/src/util/util/Token.ts
+++ b/src/util/util/Token.ts
@@ -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,
|