mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): add auth example
This commit is contained in:
@ -18,6 +18,13 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
|
||||
const userInfo: Api.Auth.UserInfo = reactive(getUserInfo());
|
||||
|
||||
/** is super role in static route */
|
||||
const isStaticSuper = computed(() => {
|
||||
const { VITE_AUTH_ROUTE_MODE, VITE_STATIC_SUPER_ROLE } = import.meta.env;
|
||||
|
||||
return VITE_AUTH_ROUTE_MODE === 'static' && userInfo.roles.includes(VITE_STATIC_SUPER_ROLE);
|
||||
});
|
||||
|
||||
/** Is login */
|
||||
const isLogin = computed(() => Boolean(token.value));
|
||||
|
||||
@ -41,8 +48,9 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
*
|
||||
* @param userName User name
|
||||
* @param password Password
|
||||
* @param [redirect=true] Whether to redirect after login. Default is `true`
|
||||
*/
|
||||
async function login(userName: string, password: string) {
|
||||
async function login(userName: string, password: string, redirect = true) {
|
||||
startLoading();
|
||||
|
||||
const { data: loginToken, error } = await fetchLogin(userName, password);
|
||||
@ -53,7 +61,9 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
if (pass) {
|
||||
await routeStore.initAuthRoute();
|
||||
|
||||
await redirectFromLogin();
|
||||
if (redirect) {
|
||||
await redirectFromLogin();
|
||||
}
|
||||
|
||||
if (routeStore.isInitAuthRoute) {
|
||||
window.$notification?.success({
|
||||
@ -94,6 +104,7 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
||||
return {
|
||||
token,
|
||||
userInfo,
|
||||
isStaticSuper,
|
||||
isLogin,
|
||||
loginLoading,
|
||||
resetStore,
|
||||
|
@ -10,10 +10,16 @@ export function getUserInfo() {
|
||||
const emptyInfo: Api.Auth.UserInfo = {
|
||||
userId: '',
|
||||
userName: '',
|
||||
roles: []
|
||||
roles: [],
|
||||
buttons: []
|
||||
};
|
||||
const userInfo = localStg.get('userInfo') || emptyInfo;
|
||||
|
||||
// fix new property: buttons, this will be removed in the next version `1.1.0`
|
||||
if (!userInfo.buttons) {
|
||||
userInfo.buttons = [];
|
||||
}
|
||||
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
|
@ -194,6 +194,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
|
||||
|
||||
const vueRoutes = getAuthVueRoutes(sortRoutes);
|
||||
|
||||
resetVueRoutes();
|
||||
|
||||
addRoutesToVueRouter(vueRoutes);
|
||||
|
||||
getGlobalMenus(sortRoutes);
|
||||
|
@ -10,6 +10,7 @@ import { useSvgIcon } from '@/hooks/common/icon';
|
||||
* @param roles Roles
|
||||
*/
|
||||
export function filterAuthRoutesByRoles(routes: ElegantConstRoute[], roles: string[]) {
|
||||
// in static mode of auth route, the super admin role is defined in front-end
|
||||
const SUPER_ROLE = 'R_SUPER';
|
||||
|
||||
// if the user is super admin, then it is allowed to access all routes
|
||||
@ -30,9 +31,7 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
|
||||
const routeRoles = (route.meta && route.meta.roles) || [];
|
||||
|
||||
// if the route's "roles" is empty, then it is allowed to access
|
||||
if (!routeRoles.length) {
|
||||
return [route];
|
||||
}
|
||||
const isEmptyRoles = !routeRoles.length;
|
||||
|
||||
// if the user's role is included in the route's "roles", then it is allowed to access
|
||||
const hasPermission = routeRoles.some(role => roles.includes(role));
|
||||
@ -43,7 +42,7 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
|
||||
filterRoute.children = filterRoute.children.flatMap(item => filterAuthRouteByRoles(item, roles));
|
||||
}
|
||||
|
||||
return hasPermission ? [filterRoute] : [];
|
||||
return hasPermission || isEmptyRoles ? [filterRoute] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user