@datalayer/core
Version:
**Datalayer Core**
23 lines (22 loc) • 731 B
JavaScript
/*
* Copyright (c) 2023-2025 Datalayer, Inc.
* Distributed under the terms of the Modified BSD License.
*/
import { useIAMStore } from '../state';
const LOGIN_HREF = "/login";
export const useUser = (role) => {
const { user } = useIAMStore();
if (role) {
if (!user?.roles.includes(role.handle)) {
console.log(`User should have role ${role.handle} - Forcing navigation to login page...`);
window.location.href = LOGIN_HREF;
throw new Error(`User should have role ${role.handle}`);
}
}
if (!user) {
console.log('No user found... Forcing navigation to /login');
window.location.href = LOGIN_HREF;
}
return user;
};
export default useUser;