diff --git a/app/reducers/authReducer.js b/app/reducers/authReducer.js index 18ec3f4..214e290 100644 --- a/app/reducers/authReducer.js +++ b/app/reducers/authReducer.js @@ -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, @@ -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, diff --git a/service/controllers/auth.controller.ts b/service/controllers/auth.controller.ts index f846bcc..c96b327 100644 --- a/service/controllers/auth.controller.ts +++ b/service/controllers/auth.controller.ts @@ -138,6 +138,7 @@ export const urlLoginController = async(req: Request, res: Response { + 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) { @@ -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 { @@ -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], } }; } diff --git a/service/types/auth.types.ts b/service/types/auth.types.ts index 2daa9ff..a4b8fc8 100644 --- a/service/types/auth.types.ts +++ b/service/types/auth.types.ts @@ -43,7 +43,8 @@ interface IUserAuthData { phoneVerified:boolean; name: string photoUrl: string; - role: GenericObject; + roles?: Array<{ [key: string]: any }>; + role:GenericObject; } interface NameData {