Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/reducers/authReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ const authReducer = (state = initState, action) => {
};

case LOGIN_SUCCESS:

const roles = action.payload?.data?.roles || [];
const getRoleWithMaxPriority = (roles) => {
if (!Array.isArray(roles) || roles.length === 0) {
return null;
}
let maxPriorityRole = roles[0];
for (let i = 1; i < roles.length; i++) {
if (roles[i].priority > maxPriorityRole.priority) {
maxPriorityRole = roles[i];
}
}
return maxPriorityRole;
};
const role = getRoleWithMaxPriority(roles);

return {
...state,
accessToken : action.payload.data.accessToken,
Expand All @@ -139,7 +155,8 @@ const authReducer = (state = initState, action) => {
checkLoginOrRegisterSuccess: true,
redirect : true,
refreshToken : action.payload.data.refreshToken,
role : action.payload?.data?.role,
roles : roles,
role : role,
user : {
email : action.payload?.data?.email,
emailVerified: action.payload?.data?.emailVerified,
Expand Down
1 change: 1 addition & 0 deletions service/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const urlLoginController = async(req: Request, res: Response<ResponseBody
phoneVerified: false,
name:"" ,
photoUrl: "",
roles: [],
role: {}
} });
} catch (error:any) {
Expand Down
61 changes: 46 additions & 15 deletions service/functions/auth.helper.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function genarateAccessToken(
email: string,
phone: string,
personID: number,
roleID: number,
roleID: number[],
refresh = true
): { accessToken: string; refreshToken: string | null } {
try {
Expand Down Expand Up @@ -294,33 +294,62 @@ async function createSessionAndLogin(userData:any, originalUrl:string, deviceId:
middleName:personMetaData?. middleName,
});

const role = await databaseActions.findOne(
// const role = await databaseActions.findOne(
// "application",
// "UserRoles",
// {
// attributes: ["roleID"],
// where: { userID: userData.id }
// }
// );
// if (!role && role?.roleID <= 0) {
// WrappidLogger.error("Role not found");
// throw new Error("Role not found");
// }
// const roleOB = await databaseActions.findByPk(
// "application",
// "Roles",
// role?.id
// );
const roles = await databaseActions.findAll(
"application",
"UserRoles",
{
attributes: ["roleID"],
where: { userID: userData.id }
where: { userID: userData.id , _status: "active"}
}
);
if (!role && role?.id <= 0) {
WrappidLogger.error("Role not found");
throw new Error("Role not found");

if (!roles || roles.length === 0) {
WrappidLogger.error("Roles not found");
throw new Error("Roles not found");
}
const roleOB = await databaseActions.findByPk(
"application",
"Roles",
role?.id

// Fetch role details for each roleID
const roleDetails = await Promise.all(
roles.map(async (role:any) => {
return await databaseActions.findByPk(
"application",
"Roles",
role.roleID
);
})
);


if (!roleDetails || roleDetails.length === 0) {
WrappidLogger.error("Role details not found");
throw new Error("Role details not found");
}

const personID = personData.id;
const roleID = role.roleID;
const rolesID = roleDetails.map((role:any) => role?.id).filter(Boolean);

const { refreshToken, accessToken } = genarateAccessToken(
userData.id,
userData.email,
userData.phone,
personID,
roleID
rolesID
);

if (!refreshToken) {
Expand Down Expand Up @@ -378,7 +407,8 @@ async function createSessionAndLogin(userData:any, originalUrl:string, deviceId:
phoneVerified: primaryPhone[0]?.verified,
name: fullName,
photoUrl: personMetaData.photoUrl,
role: {role: roleOB?.role}
roles: roleDetails,
role: roleDetails[0],
}
};
} else {
Expand Down Expand Up @@ -417,7 +447,8 @@ async function createSessionAndLogin(userData:any, originalUrl:string, deviceId:
phoneVerified: primaryPhone[0]?.verified,
name: fullName,
photoUrl: personMetaData.photoUrl,
role: {role: roleOB?.role}
roles: roleDetails,
role: roleDetails[0],
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion service/types/auth.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ interface IUserAuthData {
phoneVerified:boolean;
name: string
photoUrl: string;
role: GenericObject;
roles?: Array<{ [key: string]: any }>;
role:GenericObject;
}

interface NameData {
Expand Down
Loading