nuxt-users
Version:
A comprehensive user management module for Nuxt 3 and Nuxt 4 applications with authentication, authorization, database support, and CLI tools
115 lines (114 loc) • 3.59 kB
JavaScript
import { useRuntimeConfig } from "#app";
import { NO_AUTH_PATHS, NO_AUTH_API_PATHS } from "../constants.js";
import { hasPermission, isWhitelisted } from "../utils/permissions.js";
import { useAuthentication } from "./useAuthentication.js";
export const usePublicPaths = () => {
const { public: { nuxtUsers } } = useRuntimeConfig();
const config = nuxtUsers;
const apiBasePath = config.apiBasePath || "/api/nuxt-users";
const { user } = useAuthentication();
const getPublicPaths = () => {
const noAuthPaths = [...NO_AUTH_PATHS];
const noAuthApiPaths = NO_AUTH_API_PATHS.map((path) => `${apiBasePath}${path}`);
const whitelistedPaths = config.auth?.whitelist || [];
const customPasswordResetPath = config.passwordResetUrl && config.passwordResetUrl !== "/reset-password" ? config.passwordResetUrl : null;
const allPublicPaths = [
...noAuthPaths,
...noAuthApiPaths,
...whitelistedPaths
];
if (customPasswordResetPath) {
allPublicPaths.push(customPasswordResetPath);
}
return {
// All truly public paths (no auth required)
all: allPublicPaths,
// Categorized public paths
builtIn: {
pages: noAuthPaths,
api: noAuthApiPaths
},
whitelist: whitelistedPaths,
customPasswordResetPath,
apiBasePath
};
};
const getAccessiblePaths = () => {
const publicPaths = getPublicPaths();
if (!user.value) {
return {
all: publicPaths.all,
public: publicPaths.all,
roleBasedPaths: [],
userRole: null
};
}
const userRole = user.value.role;
const permissions = config.auth?.permissions || {};
const rolePermissions = permissions[userRole] || [];
const roleBasedPaths = [];
rolePermissions.forEach((permission) => {
if (typeof permission === "string") {
roleBasedPaths.push(permission);
} else if (typeof permission === "object" && permission.path) {
roleBasedPaths.push(permission.path);
}
});
return {
all: [...publicPaths.all, ...roleBasedPaths],
public: publicPaths.all,
roleBasedPaths,
userRole
};
};
const isAccessiblePath = (path, method = "GET") => {
if (path.includes(".")) {
return true;
}
if (path.startsWith("/_")) {
return true;
}
if (NO_AUTH_PATHS.includes(path)) {
return true;
}
const noAuthApiPaths = NO_AUTH_API_PATHS.map((p) => `${apiBasePath}${p}`);
if (noAuthApiPaths.includes(path)) {
return true;
}
if (config.passwordResetUrl && config.passwordResetUrl !== "/reset-password" && path === config.passwordResetUrl) {
return true;
}
if (isWhitelisted(path, config.auth?.whitelist || [])) {
return true;
}
if (user.value) {
return hasPermission(user.value.role, path, method, config.auth?.permissions || {});
}
return false;
};
const isPublicPath = (path) => {
if (path.includes(".")) {
return true;
}
if (path.startsWith("/_")) {
return true;
}
if (NO_AUTH_PATHS.includes(path)) {
return true;
}
const noAuthApiPaths = NO_AUTH_API_PATHS.map((p) => `${apiBasePath}${p}`);
if (noAuthApiPaths.includes(path)) {
return true;
}
if (config.passwordResetUrl && config.passwordResetUrl !== "/reset-password" && path === config.passwordResetUrl) {
return true;
}
return isWhitelisted(path, config.auth?.whitelist || []);
};
return {
getPublicPaths,
getAccessiblePaths,
isPublicPath,
isAccessiblePath
};
};