Переглянути джерело

refactor: Allow null password

Owen Diffey 3 тижнів тому
батько
коміт
240fac8ec1

+ 1 - 1
backend/src/modules/DataModule/migrations/1725485641-create-users-table.ts

@@ -54,7 +54,7 @@ export const up = async ({
 		},
 		password: {
 			type: DataTypes.STRING,
-			allowNull: false
+			allowNull: true
 		},
 		passwordResetCode: {
 			type: DataTypes.STRING,

+ 2 - 2
backend/src/modules/DataModule/models/User.ts

@@ -49,7 +49,7 @@ export class User extends Model<
 	declare avatarColor: CreationOptional<UserAvatarColor | null>;
 
 	// Services
-	declare password: string;
+	declare password: CreationOptional<string | null>;
 
 	declare passwordResetCode: CreationOptional<string | null>;
 
@@ -204,7 +204,7 @@ export const schema = {
 	},
 	password: {
 		type: DataTypes.STRING,
-		allowNull: false
+		allowNull: true
 	},
 	passwordResetCode: {
 		type: DataTypes.STRING,

+ 11 - 9
backend/src/modules/DataModule/models/User/jobs/Login.ts

@@ -4,6 +4,7 @@ import bcrypt from "bcrypt";
 import sha256 from "sha256";
 import isLoggedOut from "@/modules/DataModule/permissions/isLoggedOut";
 import DataModuleJob from "@/modules/DataModule/DataModuleJob";
+import { Op } from "sequelize";
 
 export default class Login extends DataModuleJob {
 	protected static _model = User;
@@ -20,23 +21,24 @@ export default class Login extends DataModuleJob {
 	protected async _execute() {
 		const { query } = this._payload;
 
-		const where: Record<string, string> = {};
-
-		if (query.identifier.includes("@")) {
-			where.emailAddress = query.identifier;
-		} else {
-			where.username = query.identifier;
-		}
+		const attribute = query.identifier.includes("@")
+			? 'emailAddress'
+			: 'username';
 
 		const user = await User.unscoped().findOne({
-			where
+			where: {
+				[attribute]: query.identifier,
+				password: {
+					[Op.not]: null
+				}
+			}
 		});
 
 		if (!user) throw new Error("User not found with provided credentials");
 
 		const isValid = await bcrypt.compare(
 			sha256(query.password),
-			user.password
+			user.password!
 		);
 
 		if (!isValid)