Revert "Merge pull request #2973 from bluewave-labs/feat/v2/auth"
This reverts commit72882fded9, reversing changes made tobfb299cfca.
This commit is contained in:
1 parent
003d419a64
commit
990aed9f2e
25 files changed
+69
-894
No files matched your search
Executable
+35
@@ -0,0 +1,35 @@
|
||||
import mongoose from "mongoose";
|
||||
const InviteTokenSchema = mongoose.Schema(
|
||||
{
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
teamId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Team",
|
||||
immutable: true,
|
||||
required: true,
|
||||
},
|
||||
role: {
|
||||
type: Array,
|
||||
required: true,
|
||||
default: ["user"],
|
||||
},
|
||||
token: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
expires: 3600,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
export default mongoose.model("InviteToken", InviteTokenSchema);
|
||||
@@ -1,47 +1,11 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
import mongoose from "mongoose";
|
||||
import bcrypt from "bcryptjs";
|
||||
import logger from "../../../utils/logger.js";
|
||||
import Monitor from "../Monitor.js";
|
||||
import Team from "../Team.js";
|
||||
import Notification from "../Notification.js";
|
||||
import { subscribe } from "diagnostics_channel";
|
||||
import logger from "../../utils/logger.js";
|
||||
import Monitor from "./Monitor.js";
|
||||
import Team from "./Team.js";
|
||||
import Notification from "./Notification.js";
|
||||
|
||||
export const RoleTypes = ["user", "admin", "superadmin", "demo"] as const;
|
||||
export type RoleType = (typeof RoleTypes)[number];
|
||||
|
||||
export interface ITokenizedUser {
|
||||
sub: string;
|
||||
roles: string[];
|
||||
}
|
||||
|
||||
export interface IUser extends Document {
|
||||
// V1
|
||||
_id: Types.ObjectId;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
password: string;
|
||||
avatarImage: string;
|
||||
profileImage: {
|
||||
data: Buffer;
|
||||
contentType: string;
|
||||
};
|
||||
isActive: boolean;
|
||||
isVerified: boolean;
|
||||
role?: RoleType[];
|
||||
teamId: Types.ObjectId;
|
||||
checkTTL: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
comparePassword: (submittedPassword: string) => Promise<boolean>;
|
||||
// V2
|
||||
roles: Types.ObjectId[];
|
||||
lastLoginAt?: Date;
|
||||
version: number;
|
||||
}
|
||||
|
||||
const UserSchema = new Schema<IUser>(
|
||||
const UserSchema = mongoose.Schema(
|
||||
{
|
||||
firstName: {
|
||||
type: String,
|
||||
@@ -58,7 +22,7 @@ const UserSchema = new Schema<IUser>(
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
required: false,
|
||||
required: true,
|
||||
},
|
||||
avatarImage: {
|
||||
type: String,
|
||||
@@ -77,8 +41,8 @@ const UserSchema = new Schema<IUser>(
|
||||
},
|
||||
role: {
|
||||
type: [String],
|
||||
default: ["user"],
|
||||
enum: RoleTypes,
|
||||
default: "user",
|
||||
enum: ["user", "admin", "superadmin", "demo"],
|
||||
},
|
||||
teamId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
@@ -88,22 +52,6 @@ const UserSchema = new Schema<IUser>(
|
||||
checkTTL: {
|
||||
type: Number,
|
||||
},
|
||||
|
||||
// v2
|
||||
roles: [
|
||||
{
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "Role",
|
||||
default: [],
|
||||
},
|
||||
],
|
||||
lastLoginAt: {
|
||||
type: Date,
|
||||
},
|
||||
version: {
|
||||
type: Number,
|
||||
default: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
@@ -121,7 +69,7 @@ UserSchema.pre("save", function (next) {
|
||||
|
||||
UserSchema.pre("findOneAndUpdate", function (next) {
|
||||
const update = this.getUpdate();
|
||||
if (update && "password" in update) {
|
||||
if ("password" in update) {
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
update.password = bcrypt.hashSync(update.password, salt);
|
||||
}
|
||||
@@ -144,13 +92,15 @@ UserSchema.pre("findOneAndDelete", async function (next) {
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
next(error as Error);
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
UserSchema.methods.comparePassword = async function (submittedPassword: string) {
|
||||
UserSchema.methods.comparePassword = async function (submittedPassword) {
|
||||
const res = await bcrypt.compare(submittedPassword, this.password);
|
||||
return res;
|
||||
};
|
||||
|
||||
export const User = mongoose.model<IUser>("User", UserSchema);
|
||||
const User = mongoose.model("User", UserSchema);
|
||||
|
||||
export default User;
|
||||
@@ -1,68 +0,0 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IInvite extends Document {
|
||||
// v1
|
||||
_id: Types.ObjectId;
|
||||
email: string;
|
||||
teamId: Types.ObjectId;
|
||||
role: string[];
|
||||
token: string;
|
||||
expiry: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
|
||||
// v2
|
||||
tokenHash: string;
|
||||
roles: Types.ObjectId[];
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
}
|
||||
|
||||
const InviteSchema = new Schema<IInvite>(
|
||||
{
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true,
|
||||
lowercase: true,
|
||||
},
|
||||
teamId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Team",
|
||||
immutable: true,
|
||||
required: true,
|
||||
},
|
||||
role: {
|
||||
type: [String],
|
||||
required: true,
|
||||
default: ["user"],
|
||||
},
|
||||
token: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
expires: 3600,
|
||||
},
|
||||
|
||||
// v2
|
||||
roles: [
|
||||
{
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "Role",
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
tokenHash: { type: String, required: true, unique: true },
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
export const Invite = mongoose.model<IInvite>("Invite", InviteSchema);
|
||||
@@ -1,45 +0,0 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IRole extends Document {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
description?: string;
|
||||
permissions: string[];
|
||||
isActive: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const roleSchema = new Schema<IRole>(
|
||||
{
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
maxlength: 50,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
trim: true,
|
||||
maxlength: 200,
|
||||
},
|
||||
|
||||
permissions: [
|
||||
{
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
isActive: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
roleSchema.index({ name: 1 }, { unique: true });
|
||||
|
||||
export const Role = mongoose.model<IRole>("Role", roleSchema);
|
||||
@@ -1,9 +0,0 @@
|
||||
export { User } from "./auth/User.js";
|
||||
export type { IUser } from "./auth/User.js";
|
||||
export type { ITokenizedUser } from "./auth/User.js";
|
||||
export { Role } from "./auth/Role.js";
|
||||
export type { IRole } from "./auth/Role.js";
|
||||
export type { IInvite } from "./auth/Invite.js";
|
||||
export { Invite } from "./auth/Invite.js";
|
||||
export { Migration } from "./migration/Migration.js";
|
||||
export type { IMigration } from "./migration/Migration.js";
|
||||
@@ -1,34 +0,0 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IMigration extends Document {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
runAt: Date;
|
||||
success: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const migrationSchema = new Schema<IMigration>(
|
||||
{
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
runAt: {
|
||||
type: Date,
|
||||
required: false,
|
||||
},
|
||||
success: {
|
||||
type: Boolean,
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
migrationSchema.index({ name: 1 }, { unique: true });
|
||||
|
||||
export const Migration = mongoose.model<IMigration>("Migration", migrationSchema);
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
import Monitor from "../../models/Monitor.js";
|
||||
export const MIGRATION_NAME = "0001_migrateStatusWindowThreshold";
|
||||
async function migrateStatusWindowThreshold() {
|
||||
try {
|
||||
const monitors = await Monitor.find({ statusWindowThreshold: { $lt: 1 } });
|
||||
@@ -1,56 +0,0 @@
|
||||
import { User, Role } from "../../models/index.js";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
import { DEFAULT_ROLES } from "../../../service/v2/business/AuthService.js";
|
||||
export const MIGRATION_NAME = "0002_migrateUsers";
|
||||
async function migrateUsers() {
|
||||
try {
|
||||
// Create roles if they don't exist
|
||||
const roleCount = await Role.countDocuments();
|
||||
let roles;
|
||||
if (roleCount === 0) {
|
||||
const rolePromises = DEFAULT_ROLES.map((roleData) =>
|
||||
new Role({
|
||||
...roleData,
|
||||
}).save()
|
||||
);
|
||||
roles = await Promise.all(rolePromises);
|
||||
} else {
|
||||
roles = await Role.find();
|
||||
}
|
||||
|
||||
// Migrate users
|
||||
const users = await User.find({
|
||||
$or: [{ version: { $exists: false } }, { version: { $lt: 2 } }],
|
||||
});
|
||||
for (const user of users) {
|
||||
const newRoleIds: Types.ObjectId[] = [];
|
||||
|
||||
for (const role of user.role || []) {
|
||||
if (role === "superadmin") {
|
||||
const superAdminRole = roles.find((role) => role.name === "SuperAdmin");
|
||||
newRoleIds.push(superAdminRole!._id);
|
||||
}
|
||||
if (role === "admin") {
|
||||
const managerRole = roles.find((role) => role.name === "Manager");
|
||||
newRoleIds.push(managerRole!._id);
|
||||
}
|
||||
if (role === "user") {
|
||||
const memberRole = roles.find((role) => role.name === "Member");
|
||||
newRoleIds.push(memberRole!._id);
|
||||
}
|
||||
}
|
||||
const merged = [...new Set([...user.roles, ...newRoleIds])];
|
||||
user.roles = merged;
|
||||
user.version = 2;
|
||||
await user.save();
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error("Migration error:", err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export { migrateUsers };
|
||||
@@ -0,0 +1,7 @@
|
||||
import { migrateStatusWindowThreshold } from "./0001_migrateStatusWindowThreshold.js";
|
||||
|
||||
const runMigrations = async () => {
|
||||
await migrateStatusWindowThreshold();
|
||||
};
|
||||
|
||||
export { runMigrations };
|
||||
@@ -1,25 +0,0 @@
|
||||
import { migrateStatusWindowThreshold, MIGRATION_NAME as MIGRATION_0001 } from "./0001_migrateStatusWindowThreshold.js";
|
||||
import { migrateUsers, MIGRATION_NAME as MIGRATION_0002 } from "./0002_migrateUsers.js";
|
||||
import { Migration, IMigration } from "../../models/index.js";
|
||||
|
||||
const runMigrations = async () => {
|
||||
const migrations = [
|
||||
{ name: MIGRATION_0001, migration: migrateStatusWindowThreshold },
|
||||
{ name: MIGRATION_0002, migration: migrateUsers },
|
||||
];
|
||||
|
||||
const migrationNames = migrations.map((m) => m.name);
|
||||
|
||||
const appliedMigrations = await Migration.find({ name: { $in: migrationNames }, success: true });
|
||||
|
||||
const migrationsToRun = migrations.filter((m) => !appliedMigrations.some((am: IMigration) => am.name === m.name));
|
||||
|
||||
for (const { name, migration } of migrationsToRun) {
|
||||
console.log(`Running migration: ${name}`);
|
||||
const success = await migration();
|
||||
await Migration.updateOne({ name }, { $set: { runAt: new Date(), success } }, { upsert: true });
|
||||
console.log(`Migration ${name} completed with status: ${success ? "success" : "failure"}`);
|
||||
}
|
||||
};
|
||||
|
||||
export { runMigrations };
|
||||
@@ -1,17 +1,17 @@
|
||||
const SERVICE_NAME = "inviteModule";
|
||||
|
||||
class InviteModule {
|
||||
constructor({ Invite, crypto, stringService }) {
|
||||
this.Invite = Invite;
|
||||
constructor({ InviteToken, crypto, stringService }) {
|
||||
this.InviteToken = InviteToken;
|
||||
this.crypto = crypto;
|
||||
this.stringService = stringService;
|
||||
}
|
||||
|
||||
requestInviteToken = async (userData) => {
|
||||
try {
|
||||
await this.Invite.deleteMany({ email: userData.email });
|
||||
await this.InviteToken.deleteMany({ email: userData.email });
|
||||
userData.token = this.crypto.randomBytes(32).toString("hex");
|
||||
let inviteToken = new this.Invite(userData);
|
||||
let inviteToken = new this.InviteToken(userData);
|
||||
await inviteToken.save();
|
||||
return inviteToken;
|
||||
} catch (error) {
|
||||
@@ -23,7 +23,7 @@ class InviteModule {
|
||||
|
||||
getInviteToken = async (token) => {
|
||||
try {
|
||||
const invite = await this.Invite.findOne({
|
||||
const invite = await this.InviteToken.findOne({
|
||||
token,
|
||||
});
|
||||
if (invite === null) {
|
||||
@@ -38,7 +38,7 @@ class InviteModule {
|
||||
};
|
||||
getInviteTokenAndDelete = async (token) => {
|
||||
try {
|
||||
const invite = await this.Invite.findOneAndDelete({
|
||||
const invite = await this.InviteToken.findOneAndDelete({
|
||||
token,
|
||||
});
|
||||
if (invite === null) {
|
||||
|
||||
Reference in new issue
Block a user