mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
🔗 fix: Shared Link with Markdown Code Error (#6016)
* refactor: Export AuthContext from AuthContextProvider * refactor: Update useHasAccess to utilize useContext for AuthContext * refactor: Enhance type definitions in useHasAccess for better type safety
This commit is contained in:
parent
50e8769340
commit
f362f18870
2 changed files with 24 additions and 9 deletions
|
|
@ -8,7 +8,7 @@ import {
|
|||
createContext,
|
||||
} from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||
import { useRecoilState } from 'recoil';
|
||||
import { setTokenHeader, SystemRoles } from 'librechat-data-provider';
|
||||
import type * as t from 'librechat-data-provider';
|
||||
import {
|
||||
|
|
@ -200,7 +200,7 @@ const AuthContextProvider = ({
|
|||
},
|
||||
isAuthenticated,
|
||||
}),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
|
||||
[user, error, isAuthenticated, token, userRole, adminRole],
|
||||
);
|
||||
|
||||
|
|
@ -217,4 +217,4 @@ const useAuthContext = () => {
|
|||
return context;
|
||||
};
|
||||
|
||||
export { AuthContextProvider, useAuthContext };
|
||||
export { AuthContextProvider, useAuthContext, AuthContext };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useMemo, useCallback } from 'react';
|
||||
import { PermissionTypes, Permissions } from 'librechat-data-provider';
|
||||
import { useAuthContext } from '~/hooks/AuthContext';
|
||||
import { useMemo, useCallback, useContext } from 'react';
|
||||
import type { TUser, PermissionTypes, Permissions } from 'librechat-data-provider';
|
||||
import { AuthContext } from '~/hooks/AuthContext';
|
||||
|
||||
const useHasAccess = ({
|
||||
permissionType,
|
||||
|
|
@ -9,16 +9,31 @@ const useHasAccess = ({
|
|||
permissionType: PermissionTypes;
|
||||
permission: Permissions;
|
||||
}) => {
|
||||
const { isAuthenticated, user, roles } = useAuthContext();
|
||||
const authContext = useContext(AuthContext);
|
||||
const user = authContext?.user;
|
||||
const roles = authContext?.roles;
|
||||
const isAuthenticated = authContext?.isAuthenticated || false;
|
||||
|
||||
const checkAccess = useCallback(
|
||||
({ user, permissionType, permission }) => {
|
||||
({
|
||||
user,
|
||||
permissionType,
|
||||
permission,
|
||||
}: {
|
||||
user?: TUser | null;
|
||||
permissionType: PermissionTypes;
|
||||
permission: Permissions;
|
||||
}) => {
|
||||
if (!authContext) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isAuthenticated && user?.role != null && roles && roles[user.role]) {
|
||||
return roles[user.role]?.[permissionType]?.[permission] === true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
[isAuthenticated, roles],
|
||||
[authContext, isAuthenticated, roles],
|
||||
);
|
||||
|
||||
const hasAccess = useMemo(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue