You can use Wix member module, I am sure there is something called getRoles(). After getting an array of rows, use find() to check if user have a role called “Admin” (or “ADMIN”, you can check using your own account).
Tips: Make sure you added page permission to the admin page so user will not be able to access even if they found the url.
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
// Check if the user is logged in
if (wixUsers.currentUser.loggedIn) {
// Get the user's badges
const badges = wixUsers.currentUser.badges;
// Check if the user has the "Admin" badge
if (badges.includes("Admin")) {
// Redirect the user to the URL for admins
wixLocation.to("/admin-page");
} else {
// Redirect the user to the URL for non-admins
wixLocation.to("/non-admin-page");
}
} else {
// Redirect the user to the login page
wixLocation.to("/login");
}
Hey guy, this very useful but I dont know where to put this code. Do it put it in the add custom code area? Then in the body? Im not a coder per say, but run a company website and I need to bring up a page based on badge.
You can achieve this by using Wix Code and the wix-users and wix-location APIs as you mentioned. Here’s a code snippet to help you redirect users based on their badges:
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
$w.onReady(function () {
wixUsers.currentUser
.get()
.then((user) => {
// Check if the user is an Admin (replace 'Admin' with your badge name)
if (user && user.loggedIn && user.badges.includes('Admin')) {
// Redirect to the specific URL for Admins
wixLocation.to('/admin-page-url'); // Replace with the URL you want to redirect Admins to
} else {
// Redirect to a different URL for non-Admins
wixLocation.to('/regular-page-url'); // Replace with the URL you want to redirect non-Admins to
}
})
.catch((error) => {
console.error('Error:', error);
});
});
Make sure to replace 'Admin', '/admin-page-url', and '/regular-page-url' with the actual badge name and the URLs you want to use for redirection.
This code will check if the user is logged in and has the ‘Admin’ badge. If they do, it will redirect them to the Admin URL; otherwise, it will redirect them to the regular URL. Hope this helps.