Role nd token Based Routing completed for Project,trash,home which is in Controller. Token, Auth Purpose,Rolebased middlewares created. Auth API,Project token Based API, Home Token Based API, Trash Token Based API In v1 AuthRoutes
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
import { Request, Response } from "express";
|
||||
import {
|
||||
AuthLogin,
|
||||
AuthLogout,
|
||||
AuthSignup,
|
||||
forgetPassword,
|
||||
} from "../../../../shared/services/auth/authServices.ts";
|
||||
|
||||
export const SignupController = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
console.log("req.body: ", req.body);
|
||||
const { userName, Email, Password, profilePicture } = req.body;
|
||||
if (!userName || !Email || !Password) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await AuthSignup(req.body);
|
||||
|
||||
switch (result.status) {
|
||||
case "User already exists":
|
||||
res.status(403).json({
|
||||
message: "User already exists",
|
||||
});
|
||||
break;
|
||||
|
||||
case "Success":
|
||||
res.status(201).json({
|
||||
message: "New User created",
|
||||
});
|
||||
break;
|
||||
default:
|
||||
res.status(500).json({
|
||||
message: "Internal server error",
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: "An unexpected error occurred",
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
export const SignInController = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { Email, Password, fingerprint } = req.body;
|
||||
if (!fingerprint || !Email || !Password) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await AuthLogin(req.body);
|
||||
|
||||
switch (result.status) {
|
||||
case "User Not Found!!! Kindly signup...":
|
||||
res.status(404).json({
|
||||
message: "User Not Found!!! Kindly signup...",
|
||||
});
|
||||
break;
|
||||
case "Email & Password is invalid...Check the credentials":
|
||||
res.status(400).json({
|
||||
message: "Email & Password is invalid...Check the credentials",
|
||||
});
|
||||
break;
|
||||
case "Already LoggedIn on another browser....Please logout!!!":
|
||||
res.status(403).json({
|
||||
message: "Already LoggedIn on another browser....Please logout!!!",
|
||||
});
|
||||
break;
|
||||
|
||||
case "User_Datas not found":
|
||||
res.status(404).json({
|
||||
message: "User_Datas not found",
|
||||
});
|
||||
break;
|
||||
case "User update failed.":
|
||||
res.status(400).json({
|
||||
message: "User update failed.",
|
||||
});
|
||||
break;
|
||||
case "Success":
|
||||
res.status(200).json({
|
||||
message: result.data,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
res.status(500).json({
|
||||
message: "Internal server error",
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: "An unexpected error occurred",
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
export const SignOutController = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { Email } = req.body;
|
||||
if (!Email) {
|
||||
res.status(400).json({
|
||||
message: "Email field is Mandatory",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await AuthLogout(req.body);
|
||||
|
||||
switch (result.status) {
|
||||
case "User not found":
|
||||
res.status(404).json({
|
||||
message: "User not found",
|
||||
});
|
||||
break;
|
||||
case "Token not found":
|
||||
res.status(404).json({
|
||||
message: "Token not found",
|
||||
});
|
||||
break;
|
||||
case "Success":
|
||||
res.status(200).json({
|
||||
message: "Logout Successfull",
|
||||
});
|
||||
break;
|
||||
default:
|
||||
res.status(500).json({
|
||||
message: "Internal server error",
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: "An unexpected error occurred",
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
export const ForgetPasswordController = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { Email } = req.body;
|
||||
if (!Email) {
|
||||
res.status(400).json({
|
||||
message: "Email field is Mandatory",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await forgetPassword(req.body);
|
||||
switch (result.status) {
|
||||
case "You can only reset your password once every 24 hours.":
|
||||
res.status(400).json({
|
||||
message: "You can only reset your password once every 24 hours.",
|
||||
});
|
||||
break;
|
||||
case "Email not found":
|
||||
res.status(404).json({
|
||||
message: "Email not found",
|
||||
});
|
||||
break;
|
||||
case "Success":
|
||||
res.status(200).json({
|
||||
message: "Password reset link sent successfully",
|
||||
});
|
||||
break;
|
||||
default:
|
||||
res.status(500).json({
|
||||
message: "Internal server error",
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: "An unexpected error occurred",
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
export const ResetPasswordController = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { newPassword, resetToken, confirmPassword } = req.body;
|
||||
if (!newPassword || !resetToken || !confirmPassword) {
|
||||
res.status(400).json({
|
||||
message: "All fields are Mandatory",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await forgetPassword(req.body);
|
||||
|
||||
switch (result.status) {
|
||||
case "Invalid token payload.":
|
||||
res.status(400).json({
|
||||
message: "Invalid token payload.",
|
||||
});
|
||||
break;
|
||||
case "Password mismatch":
|
||||
res.status(400).json({
|
||||
message: "Password mismatch",
|
||||
});
|
||||
break;
|
||||
case "User not found":
|
||||
res.status(404).json({
|
||||
message: "User not found",
|
||||
});
|
||||
break;
|
||||
case "Token is invalid or expired.":
|
||||
res.status(404).json({
|
||||
message: "Token is invalid or expired.",
|
||||
});
|
||||
break;
|
||||
case "Success":
|
||||
res.status(200).json({
|
||||
message: "Password reset successfull!!",
|
||||
});
|
||||
break;
|
||||
default:
|
||||
res.status(500).json({
|
||||
message: "Internal server error",
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: "An unexpected error occurred",
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Request, Response } from "express";
|
||||
import versionService from "../../../../shared/services/version/versionService.ts";
|
||||
|
||||
export const versioncontroller = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
console.log("req.body: ", req.body);
|
||||
const { projectId, userId, description, db } = req.body;
|
||||
// if (!userName || !Email || !description) {
|
||||
// res.status(400).json({
|
||||
// message: "All fields are required",
|
||||
// });
|
||||
// return;
|
||||
// }
|
||||
const result = await versionService.saveCurrentStateAsVersion(
|
||||
db,
|
||||
projectId,
|
||||
userId,
|
||||
description
|
||||
);
|
||||
|
||||
console.log(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: "An unexpected error occurred",
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
83
src/api-server/V1/v1Routes/authRoutes.ts
Normal file
83
src/api-server/V1/v1Routes/authRoutes.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import express from "express";
|
||||
import {
|
||||
ForgetPasswordController,
|
||||
ResetPasswordController,
|
||||
SignInController,
|
||||
SignOutController,
|
||||
SignupController,
|
||||
} from "../v1Controllers/authController/authControllers.ts";
|
||||
import { versioncontroller } from "../v1Controllers/versionController/versioncontroller.ts";
|
||||
import {
|
||||
createProjectController,
|
||||
GetProjects,
|
||||
RemoveProject,
|
||||
updateProjectController,
|
||||
ViewData,
|
||||
} from "../../controller/project/projectController.ts";
|
||||
import { tokenValidator } from "../../../shared/utils/token.ts";
|
||||
import authorizedRoles from "../../../shared/middleware/rbacMiddleware.ts";
|
||||
import { recentDataController } from "../../controller/home/homeControllers.ts";
|
||||
import {
|
||||
GetTrashList,
|
||||
RestoreTrash,
|
||||
} from "../../controller/trash/trashcontrollers.ts";
|
||||
|
||||
const Authrouter = express.Router();
|
||||
Authrouter.post("/Auth/signup", SignupController);
|
||||
Authrouter.post("/Auth/login", SignInController);
|
||||
Authrouter.post("/Auth/logout", SignOutController);
|
||||
Authrouter.post("/Auth/forgetPassword", ForgetPasswordController);
|
||||
Authrouter.post("/Auth/reset-password/:resetToken", ResetPasswordController);
|
||||
Authrouter.post("/Auth/versionData", versioncontroller);
|
||||
|
||||
// project
|
||||
Authrouter.post("/Auth/upsertProject", tokenValidator, createProjectController);
|
||||
Authrouter.get(
|
||||
"/Auth/Projects",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
GetProjects
|
||||
);
|
||||
Authrouter.patch(
|
||||
"/Auth/Project/archive/:projectId",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
RemoveProject
|
||||
);
|
||||
|
||||
Authrouter.patch(
|
||||
"/Auth/Project/modify",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
updateProjectController
|
||||
);
|
||||
Authrouter.get(
|
||||
"/Auth/Project/view",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
ViewData
|
||||
);
|
||||
|
||||
//home-Page
|
||||
Authrouter.get(
|
||||
"/Auth/RecentlyViewed",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
recentDataController
|
||||
);
|
||||
|
||||
//trash
|
||||
Authrouter.get(
|
||||
"/Auth/Trash/Lists",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
GetTrashList
|
||||
);
|
||||
|
||||
Authrouter.patch(
|
||||
"/Auth/restore",
|
||||
tokenValidator,
|
||||
authorizedRoles("Admin", "User"),
|
||||
RestoreTrash
|
||||
);
|
||||
export default Authrouter;
|
||||
@@ -20,8 +20,10 @@ import productRouter from "./Routes/productRoutes.ts";
|
||||
import projectRouter from "./Routes/projectRoutes.ts";
|
||||
import trashRouter from "./Routes/trashRoutes.ts";
|
||||
import homePageRouter from "./Routes/homepageRoutes.ts";
|
||||
import redis from "../shared/redis/redis.ts";
|
||||
import Authrouter from "./V1/v1Routes/authRoutes.ts";
|
||||
// import productFlowRoutes from "./Routes/productFlowRouts.ts";
|
||||
|
||||
redis;
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
// const allowedOriginsDev = [
|
||||
@@ -87,4 +89,8 @@ app.use("/api/v2", productRouter);
|
||||
app.use("/api/v1", projectRouter);
|
||||
app.use("/api/v1", trashRouter);
|
||||
app.use("/api/v1", homePageRouter);
|
||||
|
||||
//New versions
|
||||
app.use("/API/V1", Authrouter);
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import { Request, Response } from "express";
|
||||
import { RecentlyAdded, searchProject, searchTrashProject } from "../../../shared/services/home/homeService.ts";
|
||||
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
|
||||
|
||||
export const recentDataController = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { userId, organization } = req.params;
|
||||
if (!userId || !organization) {
|
||||
const { userId, organization,role } = req.user||{};
|
||||
if (!userId || !organization||!role) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await RecentlyAdded({ userId, organization });
|
||||
const result = await RecentlyAdded({ userId, organization,role });
|
||||
|
||||
switch (result.status) {
|
||||
case "User not found":
|
||||
|
||||
@@ -6,20 +6,27 @@ import {
|
||||
updateProject,
|
||||
viewProject,
|
||||
} from "../../../shared/services/project/project-Services.ts";
|
||||
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
|
||||
|
||||
export const createProjectController = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { projectUuid, userId, thumbnail, organization } = req.body;
|
||||
if (!projectUuid || !userId || !thumbnail || !organization) {
|
||||
const { userId, organization } = req.user || {};
|
||||
console.log("req.user: ", req.user);
|
||||
const { projectUuid, thumbnail } = req.body;
|
||||
if (!req.user || !req.user.userId || !req.user.organization) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
}
|
||||
if (!projectUuid || !thumbnail) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await createProject(req.body);
|
||||
const result = await createProject({ ...req.body, userId, organization });
|
||||
|
||||
switch (result.status) {
|
||||
case "project_exists":
|
||||
@@ -54,18 +61,19 @@ export const createProjectController = async (
|
||||
}
|
||||
};
|
||||
export const GetProjects = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { userId, organization } = req.params;
|
||||
if (!userId || !organization) {
|
||||
const { userId, organization, role } = req.user || {};
|
||||
// const { userId, organization } = req.params;
|
||||
if (!userId || !organization || !role) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await GetAllProjects({ userId, organization });
|
||||
const result = await GetAllProjects({ userId, organization, role });
|
||||
switch (result?.status) {
|
||||
case "User not found":
|
||||
res.status(404).json({
|
||||
@@ -92,19 +100,34 @@ export const GetProjects = async (
|
||||
}
|
||||
};
|
||||
export const RemoveProject = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { projectId } = req.params;
|
||||
const { organization, userId } = req.body;
|
||||
if (!projectId || !organization || !userId) {
|
||||
// const { organization, userId } = req.body;
|
||||
const { organization, userId, role } = req.user || {};
|
||||
if (
|
||||
!req.user ||
|
||||
!req.user.userId ||
|
||||
!req.user.organization ||
|
||||
!req.user.role
|
||||
) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
}
|
||||
if (!projectId || !organization || !userId || !role) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await DeleteProject({ projectId, organization, userId });
|
||||
const result = await DeleteProject({
|
||||
projectId,
|
||||
organization,
|
||||
userId,
|
||||
role,
|
||||
});
|
||||
switch (result?.status) {
|
||||
case "Project not found":
|
||||
res.status(404).json({
|
||||
@@ -135,13 +158,13 @@ export const RemoveProject = async (
|
||||
}
|
||||
};
|
||||
export const updateProjectController = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { projectId, organization, projectName, thumbnail, userId } =
|
||||
req.body;
|
||||
if (!userId || !organization || !projectId) {
|
||||
const { userId, organization, role } = req.user || {};
|
||||
const { projectId, projectName, thumbnail } = req.body;
|
||||
if (!userId || !organization || !projectId || !role) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
@@ -153,6 +176,7 @@ export const updateProjectController = async (
|
||||
userId,
|
||||
projectName,
|
||||
thumbnail,
|
||||
role,
|
||||
});
|
||||
switch (result?.status) {
|
||||
case "Project not found":
|
||||
@@ -184,14 +208,25 @@ export const updateProjectController = async (
|
||||
return;
|
||||
}
|
||||
};
|
||||
export const ViewData = async (req: Request, res: Response): Promise<void> => {
|
||||
export const ViewData = async (
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { projectId, organization, userId } = req.query as {
|
||||
organization: string;
|
||||
const { organization, userId, role } = req.user || {};
|
||||
if (
|
||||
!req.user ||
|
||||
!req.user.userId ||
|
||||
!req.user.organization ||
|
||||
!req.user.role
|
||||
) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
}
|
||||
const { projectId } = req.query as {
|
||||
projectId: string;
|
||||
userId: string;
|
||||
};
|
||||
if (!userId || !organization || !projectId) {
|
||||
if (!userId || !organization || !projectId || !role) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
@@ -201,6 +236,7 @@ export const ViewData = async (req: Request, res: Response): Promise<void> => {
|
||||
projectId,
|
||||
organization,
|
||||
userId,
|
||||
role,
|
||||
});
|
||||
switch (result?.status) {
|
||||
case "Project not found":
|
||||
|
||||
@@ -3,20 +3,21 @@ import {
|
||||
TrashDatas,
|
||||
RestoreTrashData,
|
||||
} from "../../../shared/services/trash/trashService.ts";
|
||||
import { AuthenticatedRequest } from "../../../shared/utils/token.ts";
|
||||
|
||||
export const GetTrashList = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { organization } = req.query as { organization: string };
|
||||
if (!organization) {
|
||||
const { organization, role, userId } = req.user || {};
|
||||
if (!organization || !role || !userId) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await TrashDatas({ organization });
|
||||
const result = await TrashDatas({ organization, role, userId });
|
||||
|
||||
switch (result.status) {
|
||||
case "Trash is Empty":
|
||||
@@ -28,7 +29,6 @@ export const GetTrashList = async (
|
||||
|
||||
case "Success":
|
||||
res.status(200).json({
|
||||
// message: "Project created Successfully",
|
||||
TrashDatas: result.ListDatas,
|
||||
});
|
||||
break;
|
||||
@@ -47,22 +47,26 @@ export const GetTrashList = async (
|
||||
};
|
||||
|
||||
export const RestoreTrash = async (
|
||||
req: Request,
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { organization, projectId } = req.query as {
|
||||
organization: string;
|
||||
const { organization, role, userId } = req.user || {};
|
||||
const { projectId } = req.query as {
|
||||
projectId: string;
|
||||
};
|
||||
console.log("organization: ", organization);
|
||||
if (!organization || !projectId) {
|
||||
if (!organization || !projectId || !role || !userId) {
|
||||
res.status(400).json({
|
||||
message: "All fields are required",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const result = await RestoreTrashData({ organization, projectId });
|
||||
const result = await RestoreTrashData({
|
||||
organization,
|
||||
projectId,
|
||||
role,
|
||||
userId,
|
||||
});
|
||||
|
||||
switch (result.status) {
|
||||
case "Project not found":
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response } from "express";
|
||||
import userModel from "../../shared/model/user-Model.ts";
|
||||
import {hashGenerate,hashValidator} from "../../shared/security/Hasing.ts"
|
||||
import {hashGenerate,hashValidator} from "../../shared/utils/Hasing.ts"
|
||||
|
||||
let serverAlive = true;
|
||||
export class User {
|
||||
|
||||
Reference in New Issue
Block a user