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:
25
src/shared/utils/Hasing.ts
Normal file
25
src/shared/utils/Hasing.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
const saltRounds = 10;
|
||||
export const hashGenerate = async (Password: string) => {
|
||||
try {
|
||||
const salt = await bcrypt.genSalt(saltRounds);
|
||||
const hash = await bcrypt.hash(Password, salt);
|
||||
|
||||
return hash;
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
};
|
||||
export const hashValidator = async (
|
||||
password: string,
|
||||
hashedPassword: string
|
||||
) => {
|
||||
try {
|
||||
const result = await bcrypt.compare(password, hashedPassword);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
34
src/shared/utils/mongosecurity.ts
Normal file
34
src/shared/utils/mongosecurity.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { MongoClient } from 'mongodb'
|
||||
export default async function mongoAdminCreation() {
|
||||
const uri = process.env.MONGO_URI!; // Replace with your MongoDB URI
|
||||
const client = new MongoClient(uri);
|
||||
const user = {
|
||||
user:"admin",
|
||||
pwd: process.env.MONGO_PASSWORD!,
|
||||
roles: [{ role: "root", db: process.env.MONGO_AUTH_DB || "admin" }],
|
||||
};
|
||||
try {
|
||||
await client.connect();
|
||||
const db = client.db('admin'); // Specify the actual database where the user should be created
|
||||
|
||||
// Check if the user already exists
|
||||
const userExists = await db.collection('system.users').findOne({ user: user.user});
|
||||
|
||||
if (userExists) {
|
||||
console.log(`User ${user} already exists`);
|
||||
return; // Exit if the user already exists
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Create the user
|
||||
await db.command({ createUser: user.user, pwd: user.pwd, roles: user.roles });
|
||||
console.log("User created successfully!")
|
||||
} catch (error) {
|
||||
console.error("Error creating user:",error);
|
||||
} finally {
|
||||
await client.close();
|
||||
}
|
||||
}
|
||||
|
||||
// mongoAdminCreation
|
||||
143
src/shared/utils/token.ts
Normal file
143
src/shared/utils/token.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import Jwt from "jsonwebtoken";
|
||||
import dotenv from "dotenv";
|
||||
import { extractOrg } from "../services/auth/authServices.ts";
|
||||
import AuthModel from "../V1Models/Auth/userAuthModel.ts";
|
||||
dotenv.config();
|
||||
|
||||
export interface AuthenticatedRequest extends Request {
|
||||
user?: {
|
||||
Email: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
};
|
||||
}
|
||||
const jwt_secret = process.env.JWT_SECRET as string;
|
||||
const refresh_jwt_secret = process.env.REFRESH_JWT_SECRET as string;
|
||||
const tokenGenerator = (
|
||||
Email: string,
|
||||
role: string,
|
||||
userId: string,
|
||||
organization: string
|
||||
) => {
|
||||
const token = Jwt.sign(
|
||||
{ Email: Email, role: role, userId, organization: organization },
|
||||
jwt_secret,
|
||||
{
|
||||
expiresIn: "3h",
|
||||
}
|
||||
);
|
||||
return token;
|
||||
};
|
||||
const tokenRefreshGenerator = (
|
||||
Email: string,
|
||||
role: string,
|
||||
userId: string,
|
||||
organization: string
|
||||
) => {
|
||||
const token = Jwt.sign(
|
||||
{ Email: Email, role: role, userId, organization: organization },
|
||||
refresh_jwt_secret,
|
||||
{
|
||||
expiresIn: "30d",
|
||||
}
|
||||
);
|
||||
return token;
|
||||
};
|
||||
const tokenValidator = async (
|
||||
req: AuthenticatedRequest,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<void> => {
|
||||
const token: string | undefined = req.headers.token as string | undefined;
|
||||
const refresh_token = req.headers["refresh_token"] as string | undefined;
|
||||
if (!token) {
|
||||
res.status(403).json({
|
||||
msg: "No token present",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = Jwt.verify(token, jwt_secret) as {
|
||||
Email: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
};
|
||||
if (!decoded) {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
status: 403,
|
||||
message: "Invalid Token",
|
||||
});
|
||||
return;
|
||||
}
|
||||
req.user = decoded;
|
||||
next();
|
||||
} catch (err) {
|
||||
// res.status(401).json({
|
||||
// msg: "Invalid Token",
|
||||
// });
|
||||
if (!refresh_token) {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
status: 403,
|
||||
message: "No refresh token present",
|
||||
});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const decodedRefresh = Jwt.verify(refresh_token, refresh_jwt_secret) as {
|
||||
Email: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
};
|
||||
if (!decodedRefresh) {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
status: 403,
|
||||
message: "Invalid Token",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const newAccessToken = tokenGenerator(
|
||||
decodedRefresh.Email,
|
||||
decodedRefresh.role,
|
||||
decodedRefresh.userId,
|
||||
decodedRefresh.organization
|
||||
);
|
||||
res.setHeader("x-access-token", newAccessToken);
|
||||
req.user = decodedRefresh;
|
||||
return next();
|
||||
} catch (err) {
|
||||
const decodedAny = Jwt.decode(token || refresh_token) as {
|
||||
Email?: string;
|
||||
role: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
};
|
||||
if (decodedAny?.Email) {
|
||||
const organization = extractOrg(decodedAny?.Email);
|
||||
const user = await AuthModel(organization).findOne({
|
||||
Email: decodedAny.Email,
|
||||
isArchieve: false,
|
||||
});
|
||||
if (user) {
|
||||
user.visitorBrowserID = "";
|
||||
await user.save();
|
||||
}
|
||||
}
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
status: 403,
|
||||
message: "Invalid Token",
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export { tokenValidator, tokenGenerator, tokenRefreshGenerator };
|
||||
Reference in New Issue
Block a user