williamcruzme / vue-gates

🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.

Home Page:https://williamcruzme.github.io/vue-gates/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mounted user roles when logged in

gladtoseeuhappy opened this issue · comments

commented

Hello, @williamcruzme, is there a way to mounted Roles and Permission when logged In??

created() {
	async() => {
		const { data: permissions } = await this.$axios.get("/api/auth/permissions");
		const { data: roles } = await this.$axios.get("/api/auth/roles");

		this.$laravel.setPermissions(permissions);
		this.$laravel.setRoles(roles);
		console.log(permissions)
	}
}

I tried above code, but nothing happend in console.log

Thanks..

Hello @N0SYS73M1SS4F3, you could try with:

async created() {
  const { data: permissions } = await this.$axios.get("/api/auth/permissions");
  const { data: roles } = await this.$axios.get("/api/auth/roles");

  this.$laravel.setPermissions(permissions);
  this.$laravel.setRoles(roles);
  console.log(permissions)
}
commented

Hello @williamcruzme yes, i tried ur code, it work, Roles mounted, Permissions mounted,

but i try bind permission to User to See Sidebar base On Roles by implement like this

in My template

    <div v-for="(item, i) in menus" :key="i" router exact>
	<div v-if="$laravel.hasPermission(item.permission)">
		<template v-if="!item.children">
			<v-list-item :key="i" :to="item.to">
				<v-list-item-content>
					<v-list-item-title>
						<v-icon left>{{ item.icon }}</v-icon>
						{{ item.name }}
					</v-list-item-title>
				</v-list-item-content>
			</v-list-item>
		</template>

in my <script>

menus: [
	{
		name: "Dashboard",
		icon: "mdi-view-dashboard",
		to: "/",
		permission: "view users"
	},

When logged In, Every roles can see sidebar the same as Admin Role, Except Reload Pagee..

Is there a way to archieve it???
Sorry for ma bad English....

Edit: if i do my code like below, it work perfectly

created() {
	if (this.user.role[0] === "administrator") {
		this.$laravel.setPermissions("view users");
	}

	if (this.user.role[0] === "saleman") {
		this.$laravel.setPermissions([
			"view sales",
			"add sales",
			"edit sales",
			"delete sales",
			"import sales",
			"summary report",
			"product report",
			"daily sale report",
			"monthly sale report",
			"daily purchase report",
			"sale report",
			"purchase report",
			"warehouse report",
			"product qty alert",
			"customer report",
			"supplier report",
			"due report",
			"pos"
		]);
	}
}

Hi @N0SYS73M1SS4F3, thank you for your time.

Solution
Set the permissions/roles when you log in (before redirect), and enable persistent feature:

Vue.use(LaravelPermissions, { persistent: true });

Recommend solution
Work with Nuxt.js, you set the permissions/roles on the server side.