# authorization-for-laravel-inertia-vue

> Authorization package for roles and permissions with super roles to use in Vue.js with Inertia.js

Latest version **1.1.1** (published 2023-03-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install authorization-for-laravel-inertia-vue
pnpm add authorization-for-laravel-inertia-vue
yarn add authorization-for-laravel-inertia-vue
bun add authorization-for-laravel-inertia-vue
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2023-03-29 |
| First published | 2023-03-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Mahmoud Faisal |
| Maintainers | mahmoud_faisal |
| Keywords | vue, vue3, authorization-for-laravel-inertia-vue, authorization-checks, authorization, permissions, roles, inertia, laravel, package |

## Links

- npm: https://www.npmjs.com/package/authorization-for-laravel-inertia-vue
- Repository: https://github.com/mahmoud-faisal-hub/authorization-for-laravel-inertia-vue
- Homepage: https://github.com/mahmoud-faisal-hub/authorization-for-laravel-inertia-vue#readme
- Issues: https://github.com/mahmoud-faisal-hub/authorization-for-laravel-inertia-vue/issues
- npm.io page: https://npm.io/package/authorization-for-laravel-inertia-vue

## Dependencies (1)

- [@inertiajs/vue3](https://npm.io/package/@inertiajs/vue3.md) ^1.0.2

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2023-03-29
- 1.0.0 — 2023-03-25
- 0.0.2 — 2023-03-24
- 0.0.1 — 2023-03-24

## README

# Authorization For Laravel Inertia Vue

Authorization For Laravel Inertia Vue is a js package to check for the logged in user roles and permissions.

## After installed you can use it like this in [Vue.js](https://vuejs.org/):

```html
<!-- Check if the user has a permission -->
<div v-if="can('edit post')">
  <!-- Do something -->
</div>

<!-- Check if the user has a role -->
<div v-if="is('writer')">
  <!-- Do something -->
</div>
<!-- OR -->
<div v-if="hasRole('writer')">
  <!-- Do something -->
</div>

<!-- Check if the user has any permission in given a array -->
<div v-if="canAny(['create post', 'edit post', 'delete post'])">
  <!-- Do something -->
</div>

<!-- Check if the user has any permission using a separator -->
<div v-if="canAny('create post | edit post | delete post')">
  <!-- Do something -->
</div>

<!-- Check if the user has all the permission in a given array -->
<div v-if="canAll(['create post', 'publish post'])">
  <!-- Do something -->
</div>

<!-- Check if the user has any role in given a array -->
<div v-if="isAny(['writer', 'editor'])">
  <!-- Do something -->
</div>
<!-- OR -->
<div v-if="hasAnyRole('create post | edit post | delete post')">
  <!-- Do something -->
</div>

<!-- Check if the user has all the roles in a given array -->
<div v-if="isAll(['writer', 'editor', 'super admin'])">
  <!-- Do something -->
</div>
```

## Installation

```bash
npm install authorization-for-laravel-inertia-vue
```

## Configuration

First, return the `roles` and `permissions` of the logged in user and the defined `superRules` in the `HandleInertiaRequests.php` file in `app\Http\Middleware\HandleInertiaRequests.php` in the [`share`](https://inertiajs.com/shared-data) method of [inertia](https://inertiajs.com/):
```php
public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'auth' => [
            'roles' => $request->user() ? $request->user()->roles()->pluck('name') : [],
            'permissions' => $request->user() ? $request->user()->getAllPermissions()->pluck('name') : [],
        ],
        'superRoles' => ['Super Admin', 'Super Moderator'],
    ]);
}
```

Second, add and use the `authorization-for-laravel-inertia-vue` plugin in `app.js` file:

```js
import AuthorizationForLaravelInertiaVue from 'authorization-for-laravel-inertia-vue';

// The use it with the createApp
return createApp({ render: () => h(App, props) })
            .use(AuthorizationForLaravelInertiaVue)
            .mount(el);
```

And now you are good to go 🚀.

## Section Links
Permissions:

[can](#check-for-a-single-permission) - 
[canNot](#check-if-user-does-not-has-permission) - 
[canAny](#check-if-user-has-any-of-the-provided-permissions) -
[canNotAny](#check-if-user-does-NOT-has-any-of-the-provided-permissions) - 
[canAll](#check-if-user-has-all-of-the-provided-permissions) -
[canNotAll](#check-if-user-does-not-has-all-of-the-provided-permissions) -
[guest](#check-if-user-is-a-guest-and-do-not-has-any-permission)

Roles

[is & hasRole](#check-for-a-single-role) - 
[isNot & unlessRole](#check-if-user-does-not-has-role) - 
[isAny & hasAnyRole](#check-if-user-has-any-of-the-provided-roles) - 
[isNotAny & unlessAnyRole](#check-if-user-does-not-has-any-of-the-provided-roles) - 
[isAll & hasAllRoles](#check-if-user-has-all-of-the-provided-role) - 
[isNotAll & unlessAllRoles](#check-if-user-does-not-has-all-of-the-provided-role) - 
[isExact & hasExactRoles](#check-if-user-has-exactly-all-of-a-given-list-of-roles) - 
[hasNoRoles](#check-if-user-has-no-roles) -
[isSuper & hasSuperRole](#check-if-user-has-super-role) -
[isNotSuper & unlessSuperRole](#check-if-user-has-no-super-role)

## Documentation

All the methods provided by this package can be used in the `template` directly or imported from the package and used in the `script` as following:

### Check for a single permission:

- You can use it inside the template directly like this:
```html
<!-- Check if the user has a permission -->
<div v-if="can('edit post')">
  <!-- Do something -->
</div>
```

- Or you can use it in the script like this:
```js
import { can } from 'authorization-for-laravel-inertia-vue';

if (can('edit post')) {
    // Do something
}
```

### Check if user does **NOT** has permission:

```html
<!-- Check if the user doesn't has permission -->
<div v-if="canNot('delete post')">
  <!-- Do something -->
</div>
```

### Check if user has **ANY** of the provided permissions:

```html
<!-- Check if the user has Any permission -->
<div v-if="canAny(['edit post', 'delete post'])">
  <!-- Do something -->
</div>

<!-- OR By Using A Separator -->
<div v-if="canAny('create post | edit post | delete post')">
  <!-- Do something -->
</div>
```

### Check if user does **NOT** has **ANY** of the provided permissions:

```html
<!-- Check if the user doesn't has Any of those permissions -->
<div v-if="canNotAny(['edit post', 'delete post'])">
  <!-- Do something -->
</div>
```

### Check if user has **ALL** of the provided permissions:

```html
<!-- Check if the user has All of those permissions -->
<div v-if="canAll(['create post', 'edit post'])">
  <!-- Do something -->
</div>
```

### Check if user does **NOT** has **ALL** of the provided permissions:

```html
<!-- Check if the user does NOT has All of those permissions -->
<div v-if="canNotAll(['create post', 'edit post'])">
  <!-- Do something -->
</div>
```

### Check if user is a **GUEST** and do **NOT** has **ANY** permission:

```html
<!-- Check if the user is a guest permissions -->
<div v-if="guest()">
  <!-- Do something -->
</div>
```
---

### Check for a single role:

```html
<!-- Check if the user a role -->
<div v-if="is('writer')">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasRole('writer')">
  <!-- Do something -->
</div>
```

### Check if user does **NOT** has role:

```html
<!-- Check if the user doesn't has role -->
<div v-if="isNot('writer')">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessRole('writer')">
  <!-- Do something -->
</div>
```

### Check if user has **ANY** of the provided roles:

```html
<!-- Check if the user has Any role -->
<div v-if="isAny(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasAnyRole(['writer', 'editor'])">
  <!-- Do something -->
</div>
```

### Check if user does **NOT** has **ANY** of the provided roles:

```html
<!-- Check if the user doesn't has Any of those roles -->
<div v-if="isNotAny(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessAnyRole(['writer', 'editor'])">
  <!-- Do something -->
</div>
```

### Check if user has **ALL** of the provided role:

```html
<!-- Check if the user has All of those role -->
<div v-if="isAll(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasAllRoles(['writer', 'editor'])">
  <!-- Do something -->
</div>
```

### Check if user does **NOT** has ALL of the provided role:

```html
<!-- Check if the user does NOT has All of those roles -->
<div v-if="isNotAll(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessAllRoles(['writer', 'editor'])">
  <!-- Do something -->
</div>
```

### Check if user has **Exactly ALL** of a given list of roles:

```html
<!-- Check if the user has exactly all of a given list of roles no more no less -->
<div v-if="isExact(['writer', 'editor'])">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasExactRoles(['writer', 'editor'])">
  <!-- Do something -->
</div>
```

### Check if user has **NO** roles:

```html
<!-- Check if the user has no roles -->
<div v-if="hasNoRoles()">
  <!-- Do something -->
</div>
```

### Check if user has **SUPER ROLE**:

```html
<!-- Check if the user has any super role -->
<div v-if="isSuper()">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="hasSuperRole()">
  <!-- Do something -->
</div>
```

### Check if user has **NO** **SUPER ROLE**:

```html
<!-- Check if the user has not any super role -->
<div v-if="isNotSuper()">
  <!-- Do something -->
</div>

<!-- Or -->
<div v-if="unlessSuperRole()">
  <!-- Do something -->
</div>
```

## License
[MIT](./LICENSE.md)

---
_Source: https://npm.io/package/authorization-for-laravel-inertia-vue · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
