removed unneeded user functions
This commit is contained in:
@@ -179,26 +179,6 @@ class UserController {
|
||||
}
|
||||
};
|
||||
|
||||
refreshStorageSize = async (
|
||||
req: RequestType,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
if (!req.user) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const userID = req.user._id;
|
||||
|
||||
await UserProvider.refreshStorageSize(userID);
|
||||
|
||||
res.send();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
};
|
||||
|
||||
getUserDetailed = async (
|
||||
req: RequestType,
|
||||
res: Response,
|
||||
@@ -291,23 +271,6 @@ class UserController {
|
||||
next(e);
|
||||
}
|
||||
};
|
||||
|
||||
addName = async (req: RequestType, res: Response, next: NextFunction) => {
|
||||
if (!req.user) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const userID = req.user._id;
|
||||
const name = req.body.name;
|
||||
|
||||
await UserProvider.addName(userID, name);
|
||||
|
||||
res.send();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default UserController;
|
||||
|
||||
@@ -4,7 +4,10 @@ import authNoEmailVerification from "../middleware/authNoEmailVerificication";
|
||||
import UserController from "../controllers/user-controller";
|
||||
import authRefresh from "../middleware/authRefresh";
|
||||
import authLogout from "../middleware/authLogout";
|
||||
import { changePasswordValidationRules } from "../middleware/user/user-middleware";
|
||||
import {
|
||||
changePasswordValidationRules,
|
||||
createAccountValidationRules,
|
||||
} from "../middleware/user/user-middleware";
|
||||
|
||||
const userController = new UserController();
|
||||
|
||||
@@ -28,7 +31,11 @@ router.post("/user-service/logout", authLogout, userController.logout);
|
||||
|
||||
router.post("/user-service/logout-all", authLogout, userController.logoutAll);
|
||||
|
||||
router.post("/user-service/create", userController.createUser);
|
||||
router.post(
|
||||
"/user-service/create",
|
||||
createAccountValidationRules,
|
||||
userController.createUser
|
||||
);
|
||||
|
||||
router.post("/user-service/verify-email", userController.verifyEmail);
|
||||
|
||||
@@ -49,14 +56,6 @@ router.post("/user-service/get-token", authRefresh, userController.getToken);
|
||||
|
||||
// PATCH
|
||||
|
||||
router.patch(
|
||||
"/user-service/refresh-storage-size",
|
||||
auth,
|
||||
userController.refreshStorageSize
|
||||
);
|
||||
|
||||
router.patch("/user-service/add-name", auth, userController.addName);
|
||||
|
||||
router.patch(
|
||||
"/user-service/change-password",
|
||||
auth,
|
||||
|
||||
@@ -21,3 +21,25 @@ export const changePasswordValidationRules = [
|
||||
),
|
||||
middlewareValidationFunction,
|
||||
];
|
||||
|
||||
// POST
|
||||
|
||||
export const createAccountValidationRules = [
|
||||
body("email")
|
||||
.exists()
|
||||
.withMessage("Email is required")
|
||||
.isString()
|
||||
.withMessage("Email must be a string")
|
||||
.isEmail()
|
||||
.withMessage("Email is invalid"),
|
||||
body("password")
|
||||
.exists()
|
||||
.withMessage("Password is required")
|
||||
.isString()
|
||||
.withMessage("Password must be a string")
|
||||
.isLength({ min: 6, max: 256 })
|
||||
.withMessage(
|
||||
"Password must be at least 6 characters and at most 256 characters"
|
||||
),
|
||||
middlewareValidationFunction,
|
||||
];
|
||||
|
||||
@@ -7,9 +7,6 @@ import env from "../enviroment/env";
|
||||
|
||||
const userSchema = new mongoose.Schema(
|
||||
{
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
@@ -79,60 +76,7 @@ const userSchema = new mongoose.Schema(
|
||||
passwordResetToken: {
|
||||
type: String,
|
||||
},
|
||||
googleDriveEnabled: {
|
||||
type: Boolean,
|
||||
},
|
||||
googleDriveData: {
|
||||
id: {
|
||||
type: String,
|
||||
},
|
||||
key: {
|
||||
type: String,
|
||||
},
|
||||
iv: {
|
||||
type: Buffer,
|
||||
},
|
||||
token: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
s3Enabled: {
|
||||
type: Boolean,
|
||||
},
|
||||
s3Data: {
|
||||
id: {
|
||||
type: String,
|
||||
},
|
||||
key: {
|
||||
type: String,
|
||||
},
|
||||
bucket: {
|
||||
type: String,
|
||||
},
|
||||
iv: {
|
||||
type: Buffer,
|
||||
},
|
||||
},
|
||||
personalStorageCanceledDate: Number,
|
||||
storageData: {
|
||||
storageSize: Number,
|
||||
storageLimit: Number,
|
||||
failed: Boolean,
|
||||
},
|
||||
storageDataPersonal: {
|
||||
storageSize: Number,
|
||||
failed: Boolean,
|
||||
},
|
||||
storageDataGoogle: {
|
||||
storageSize: Number,
|
||||
storageLimit: Number,
|
||||
failed: Boolean,
|
||||
},
|
||||
activeSubscription: Boolean,
|
||||
planID: String,
|
||||
passwordLastModified: Number,
|
||||
lastSubscriptionCheckTime: Number,
|
||||
lastSubscriptionStatus: Boolean,
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
@@ -141,7 +85,6 @@ const userSchema = new mongoose.Schema(
|
||||
|
||||
export interface UserInterface extends Document {
|
||||
_id: mongoose.Types.ObjectId;
|
||||
name: string;
|
||||
email: string;
|
||||
password: string;
|
||||
tokens: any[];
|
||||
@@ -152,40 +95,7 @@ export interface UserInterface extends Document {
|
||||
emailVerified?: boolean;
|
||||
emailToken?: string;
|
||||
passwordResetToken?: string;
|
||||
googleDriveEnabled?: boolean;
|
||||
googleDriveData?: {
|
||||
id?: string;
|
||||
key?: string;
|
||||
iv?: Buffer;
|
||||
token?: string;
|
||||
};
|
||||
s3Enabled?: boolean;
|
||||
s3Data?: {
|
||||
id?: string;
|
||||
key?: string;
|
||||
bucket?: string;
|
||||
iv?: Buffer;
|
||||
};
|
||||
storageData?: {
|
||||
storageSize?: number;
|
||||
storageLimit?: number;
|
||||
failed?: boolean;
|
||||
};
|
||||
storageDataPersonal?: {
|
||||
storageSize?: number;
|
||||
failed?: boolean;
|
||||
};
|
||||
storageDataGoogle?: {
|
||||
storageSize?: number;
|
||||
storageLimit?: number;
|
||||
failed?: boolean;
|
||||
};
|
||||
activeSubscription?: boolean;
|
||||
passwordLastModified?: number;
|
||||
personalStorageCanceledDate?: number;
|
||||
planID?: string;
|
||||
lastSubscriptionCheckTime?: number;
|
||||
lastSubscriptionStatus?: boolean;
|
||||
|
||||
getEncryptionKey: () => Buffer | undefined;
|
||||
generateTempAuthToken: () => Promise<any>;
|
||||
@@ -200,12 +110,6 @@ export interface UserInterface extends Document {
|
||||
changeEncryptionKey: (randomKey: Buffer) => Promise<void>;
|
||||
generateEmailVerifyToken: () => Promise<string>;
|
||||
generatePasswordResetToken: () => Promise<string>;
|
||||
encryptDriveIDandKey: (ID: string, key: string) => Promise<void>;
|
||||
decryptDriveIDandKey: () => Promise<{ clientID: string; clientKey: string }>;
|
||||
encryptDriveTokenData: (token: Object) => Promise<void>;
|
||||
decryptDriveTokenData: () => Promise<any>;
|
||||
encryptS3Data: (id: string, key: string, bucket: string) => Promise<void>;
|
||||
decryptS3Data: () => Promise<{ id: string; key: string; bucket: string }>;
|
||||
}
|
||||
|
||||
const maxAgeAccess = 60 * 1000 * 20 + 1000 * 60;
|
||||
@@ -301,8 +205,6 @@ userSchema.methods.generateAuthToken = async function (
|
||||
_id: user._id,
|
||||
emailVerified: user.emailVerified || env.disableEmailVerification,
|
||||
email: user.email,
|
||||
s3Enabled: user.s3Enabled,
|
||||
googleDriveEnabled: user.googleDriveEnabled,
|
||||
};
|
||||
|
||||
let accessToken = jwt.sign({ user: userObj, iv }, env.passwordAccess!, {
|
||||
@@ -318,8 +220,6 @@ userSchema.methods.generateAuthToken = async function (
|
||||
|
||||
const encryptedToken = user.encryptToken(refreshToken, encryptionKey, iv);
|
||||
|
||||
//user.tokens = user.tokens.concat({token: encryptedToken});
|
||||
|
||||
uuid = uuid ? uuid : "unknown";
|
||||
|
||||
await User.updateOne(
|
||||
@@ -327,23 +227,7 @@ userSchema.methods.generateAuthToken = async function (
|
||||
{ $push: { tokens: { token: encryptedToken, uuid, time } } }
|
||||
);
|
||||
|
||||
// console.log("saving user")
|
||||
// console.log("user saved")
|
||||
return { accessToken, refreshToken };
|
||||
|
||||
// const iv = crypto.randomBytes(16);
|
||||
|
||||
// const user = this;
|
||||
// let token = jwt.sign({_id:user._id.toString(), iv}, env.passwordAccess!);
|
||||
|
||||
// const encryptionKey = user.getEncryptionKey();
|
||||
|
||||
// const encryptedToken = user.encryptToken(token, encryptionKey, iv);
|
||||
|
||||
// user.tokens = user.tokens.concat({token: encryptedToken});
|
||||
|
||||
// await user.save();
|
||||
// return token;
|
||||
};
|
||||
|
||||
userSchema.methods.encryptToken = function (
|
||||
@@ -521,147 +405,6 @@ userSchema.methods.generateEmailVerifyToken = async function () {
|
||||
return token;
|
||||
};
|
||||
|
||||
userSchema.methods.encryptDriveIDandKey = async function (
|
||||
ID: string,
|
||||
key: string
|
||||
) {
|
||||
const iv = crypto.randomBytes(16);
|
||||
|
||||
const user = this as UserInterface;
|
||||
|
||||
const encryptedKey = user.getEncryptionKey();
|
||||
|
||||
const encryptedDriveID = user.encryptToken(ID, encryptedKey, iv);
|
||||
const encryptedDriveKey = user.encryptToken(key, encryptedKey, iv);
|
||||
|
||||
if (!user.googleDriveData) user.googleDriveData = {};
|
||||
|
||||
user.googleDriveData!.id = encryptedDriveID;
|
||||
user.googleDriveData!.key = encryptedDriveKey;
|
||||
user.googleDriveData!.iv = iv;
|
||||
|
||||
await user.save();
|
||||
};
|
||||
|
||||
userSchema.methods.decryptDriveIDandKey = async function () {
|
||||
const user = this as UserInterface;
|
||||
|
||||
const iv = user.googleDriveData!.iv;
|
||||
|
||||
const encryptedKey = user.getEncryptionKey();
|
||||
|
||||
const encryptedDriveID = user.googleDriveData?.id;
|
||||
const encryptedDriveKey = user.googleDriveData?.key;
|
||||
|
||||
const decryptedDriveID = user.decryptToken(
|
||||
encryptedDriveID,
|
||||
encryptedKey,
|
||||
iv
|
||||
);
|
||||
const decryptedDriveKey = user.decryptToken(
|
||||
encryptedDriveKey,
|
||||
encryptedKey,
|
||||
iv
|
||||
);
|
||||
|
||||
return {
|
||||
clientID: decryptedDriveID,
|
||||
clientKey: decryptedDriveKey,
|
||||
};
|
||||
};
|
||||
|
||||
userSchema.methods.encryptDriveTokenData = async function (token: Object) {
|
||||
const user = this as UserInterface;
|
||||
const iv = user.googleDriveData?.iv;
|
||||
|
||||
const tokenToString = JSON.stringify(token);
|
||||
|
||||
const encryptedKey = user.getEncryptionKey();
|
||||
|
||||
const encryptedDriveToken = user.encryptToken(
|
||||
tokenToString,
|
||||
encryptedKey,
|
||||
iv
|
||||
);
|
||||
|
||||
if (!user.googleDriveData) user.googleDriveData = {};
|
||||
|
||||
user.googleDriveData.token = encryptedDriveToken;
|
||||
user.googleDriveEnabled = true;
|
||||
|
||||
await user.save();
|
||||
};
|
||||
|
||||
userSchema.methods.decryptDriveTokenData = async function () {
|
||||
const user = this as UserInterface;
|
||||
|
||||
const iv = user.googleDriveData!.iv;
|
||||
|
||||
const encryptedKey = user.getEncryptionKey();
|
||||
|
||||
const encryptedToken = user.googleDriveData?.token;
|
||||
|
||||
const decryptedToken = user.decryptToken(encryptedToken, encryptedKey, iv);
|
||||
|
||||
const tokenToObj = JSON.parse(decryptedToken);
|
||||
|
||||
return tokenToObj;
|
||||
};
|
||||
|
||||
userSchema.methods.encryptS3Data = async function (
|
||||
ID: string,
|
||||
key: string,
|
||||
bucket: string
|
||||
) {
|
||||
const iv = crypto.randomBytes(16);
|
||||
|
||||
const user = this as UserInterface;
|
||||
|
||||
const encryptedKey = user.getEncryptionKey();
|
||||
|
||||
const encryptedS3ID = user.encryptToken(ID, encryptedKey, iv);
|
||||
const encryptedS3Key = user.encryptToken(key, encryptedKey, iv);
|
||||
const encryptedS3Bucket = user.encryptToken(bucket, encryptedKey, iv);
|
||||
|
||||
if (!user.s3Data) user.s3Data = {};
|
||||
|
||||
user.s3Data!.id = encryptedS3ID;
|
||||
user.s3Data!.key = encryptedS3Key;
|
||||
user.s3Data!.bucket = encryptedS3Bucket;
|
||||
user.s3Data!.iv = iv;
|
||||
user.s3Enabled = true;
|
||||
|
||||
await user.save();
|
||||
};
|
||||
|
||||
userSchema.methods.decryptS3Data = async function () {
|
||||
const user = this as UserInterface;
|
||||
|
||||
const iv = user.s3Data?.iv;
|
||||
|
||||
const encryptedKey = user.getEncryptionKey();
|
||||
|
||||
const encrytpedS3ID = user.s3Data?.id;
|
||||
const encryptedS3Key = user.s3Data?.key;
|
||||
const encryptedS3Bucket = user.s3Data?.bucket;
|
||||
|
||||
const decrytpedS3ID = user.decryptToken(encrytpedS3ID, encryptedKey, iv);
|
||||
const decryptedS3Key = user.decryptToken(encryptedS3Key, encryptedKey, iv);
|
||||
const decryptedS3Bucket = user.decryptToken(
|
||||
encryptedS3Bucket,
|
||||
encryptedKey,
|
||||
iv
|
||||
);
|
||||
|
||||
//console.log("decrypted keys", decrytpedS3ID, decryptedS3Key, decryptedS3Bucket);
|
||||
|
||||
return {
|
||||
id: decrytpedS3ID,
|
||||
key: decryptedS3Key,
|
||||
bucket: decryptedS3Bucket,
|
||||
};
|
||||
};
|
||||
|
||||
userSchema.methods.generatePasswordResetToken = async function () {
|
||||
const iv = crypto.randomBytes(16);
|
||||
|
||||
|
||||
@@ -139,51 +139,11 @@ class UserService {
|
||||
return { accessToken, refreshToken };
|
||||
};
|
||||
|
||||
refreshStorageSize = async (userID: string) => {
|
||||
const user = await User.findById(userID);
|
||||
|
||||
if (!user) throw new NotFoundError("Cannot find user");
|
||||
|
||||
const fileList = await File.find({
|
||||
"metadata.owner": user._id,
|
||||
"metadata.personalFile": null,
|
||||
});
|
||||
|
||||
let size = 0;
|
||||
|
||||
for (let currentFile of fileList) {
|
||||
size += currentFile.length;
|
||||
}
|
||||
|
||||
user.storageData = { storageSize: size, storageLimit: 0 };
|
||||
|
||||
await user.save();
|
||||
};
|
||||
|
||||
getUserDetailed = async (userID: string) => {
|
||||
const user = await User.findById(userID);
|
||||
|
||||
if (!user) throw new NotFoundError("Cannot find user");
|
||||
|
||||
if (
|
||||
!user.storageData ||
|
||||
(!user.storageData.storageSize && !user.storageData.storageLimit)
|
||||
)
|
||||
user.storageData = { storageLimit: 0, storageSize: 0 };
|
||||
if (
|
||||
!user.storageDataPersonal ||
|
||||
(!user.storageDataPersonal.storageSize &&
|
||||
!user.storageDataPersonal.failed)
|
||||
)
|
||||
user.storageDataPersonal = { storageSize: 0 };
|
||||
if (
|
||||
!user.storageDataGoogle ||
|
||||
(!user.storageDataGoogle.storageLimit &&
|
||||
!user.storageDataGoogle.storageSize &&
|
||||
!user.storageDataGoogle.failed)
|
||||
)
|
||||
user.storageDataGoogle = { storageLimit: 0, storageSize: 0 };
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
@@ -254,17 +214,6 @@ class UserService {
|
||||
throw new ForbiddenError("Reset Password Token Do Not Match");
|
||||
}
|
||||
};
|
||||
|
||||
addName = async (userID: string, name: string) => {
|
||||
if (!name || name.length === 0) throw new ForbiddenError("No name");
|
||||
|
||||
const user = await User.findById(userID);
|
||||
|
||||
if (!user) throw new NotFoundError("Cannot find user");
|
||||
|
||||
user.name = name;
|
||||
await user.save();
|
||||
};
|
||||
}
|
||||
|
||||
export default UserService;
|
||||
|
||||
@@ -14,7 +14,7 @@ const LoginPage = () => {
|
||||
const [password, setPassword] = useState("");
|
||||
const [verifyPassword, setVerifyPassword] = useState("");
|
||||
const [mode, setMode] = useState<"login" | "create" | "reset">("login");
|
||||
const [attemptingLogin, setAttemptingLogin] = useState(false);
|
||||
const [attemptingLogin, setAttemptingLogin] = useState(true);
|
||||
const [loadingLogin, setLoadingLogin] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const dispatch = useAppDispatch();
|
||||
@@ -30,7 +30,6 @@ const LoginPage = () => {
|
||||
const redirectPath = location.state?.from?.pathname || "/home";
|
||||
dispatch(setUser(userResponse));
|
||||
navigate(redirectPath);
|
||||
setAttemptingLogin(false);
|
||||
window.localStorage.setItem("hasPreviouslyLoggedIn", "true");
|
||||
} catch (e) {
|
||||
setAttemptingLogin(false);
|
||||
@@ -122,7 +121,12 @@ const LoginPage = () => {
|
||||
}, [mode, email, password, verifyPassword]);
|
||||
|
||||
useEffect(() => {
|
||||
attemptLoginWithToken();
|
||||
const loggedIn = window.localStorage.getItem("hasPreviouslyLoggedIn");
|
||||
if (loggedIn === "true") {
|
||||
attemptLoginWithToken();
|
||||
} else {
|
||||
setAttemptingLogin(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
if (attemptingLogin) {
|
||||
|
||||
@@ -57,14 +57,6 @@ const SettingsPage = () => {
|
||||
setShowSidebarMobile(false);
|
||||
};
|
||||
|
||||
if (!user) {
|
||||
return (
|
||||
<div className="w-screen h-screen flex justify-center items-center">
|
||||
<Spinner />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="hidden sm:block">
|
||||
@@ -115,16 +107,23 @@ const SettingsPage = () => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-6 px-2 sm:px-64 w-full">
|
||||
<div className="block sm:hidden mb-2">
|
||||
<MenuIcon
|
||||
className="w-8 h-8"
|
||||
onClick={() => setShowSidebarMobile(!showSidebarMobile)}
|
||||
/>
|
||||
{user && (
|
||||
<div className="mt-6 px-2 sm:px-64 w-full">
|
||||
<div className="block sm:hidden mb-2">
|
||||
<MenuIcon
|
||||
className="w-8 h-8"
|
||||
onClick={() => setShowSidebarMobile(!showSidebarMobile)}
|
||||
/>
|
||||
</div>
|
||||
{tab === "account" && <SettingsAccountSection user={user} />}
|
||||
{tab === "general" && <SettingsGeneralSection />}
|
||||
</div>
|
||||
{tab === "account" && <SettingsAccountSection user={user} />}
|
||||
{tab === "general" && <SettingsGeneralSection />}
|
||||
</div>
|
||||
)}
|
||||
{!user && (
|
||||
<div className="w-full h-screen flex justify-center items-center">
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<ToastContainer position="bottom-left" />
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user