Vue.js with Laravel Permission
I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).
laravel vue.js user-permissions
add a comment |
I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).
laravel vue.js user-permissions
I am also facing the same problem.
– Tejas Khutale
Nov 15 '18 at 5:25
add a comment |
I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).
laravel vue.js user-permissions
I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am not understanding how can I check permission in the Vue JS front End (In Laravel blade I am using @Can to check the permission).
laravel vue.js user-permissions
laravel vue.js user-permissions
edited Feb 10 at 19:00
Pierce O'Neill
329514
329514
asked Nov 15 '18 at 5:17
Kapil YamakanmardiKapil Yamakanmardi
435
435
I am also facing the same problem.
– Tejas Khutale
Nov 15 '18 at 5:25
add a comment |
I am also facing the same problem.
– Tejas Khutale
Nov 15 '18 at 5:25
I am also facing the same problem.
– Tejas Khutale
Nov 15 '18 at 5:25
I am also facing the same problem.
– Tejas Khutale
Nov 15 '18 at 5:25
add a comment |
2 Answers
2
active
oldest
votes
I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.
It might even be as simple as
Vue.directive('can', function (el, binding)
return Laravel.permissions.indexOf(binding) !== -1;
)
I haven't tested this code. Just brainstorming here.
<button v-can="editStuff">You can edit this thing</button>
I can hold permissions this way:
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'userId' => Auth::user()->id,
'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
]); ?>
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
2
I guess you'd just use<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likelylayout.blade.php
).
– Namoshek
Feb 8 at 10:22
2
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
1
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
add a comment |
I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.
Routes:
Route::get('/permission/permissionName', 'PermissionController@check');
Controller:
function check($permissionName)
if (! Auth::user()->hasPermissionTo($permissionName))
abort(403);
return response('', 204);
Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.
Vue.mixin("can", (permissionName) =>
let hasAccess;
axios.get(`/permission/$permissionName`)
.then(()=>
hasAccess = true;
.catch(()=>
hasAccess = false;
;
return hasAccess;
);
And then everytime you wanna check permission, you can just do
<el-input v-if="can('write-stuff')"> </el-input>
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312875%2fvue-js-with-laravel-permission%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.
It might even be as simple as
Vue.directive('can', function (el, binding)
return Laravel.permissions.indexOf(binding) !== -1;
)
I haven't tested this code. Just brainstorming here.
<button v-can="editStuff">You can edit this thing</button>
I can hold permissions this way:
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'userId' => Auth::user()->id,
'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
]); ?>
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
2
I guess you'd just use<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likelylayout.blade.php
).
– Namoshek
Feb 8 at 10:22
2
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
1
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
add a comment |
I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.
It might even be as simple as
Vue.directive('can', function (el, binding)
return Laravel.permissions.indexOf(binding) !== -1;
)
I haven't tested this code. Just brainstorming here.
<button v-can="editStuff">You can edit this thing</button>
I can hold permissions this way:
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'userId' => Auth::user()->id,
'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
]); ?>
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
2
I guess you'd just use<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likelylayout.blade.php
).
– Namoshek
Feb 8 at 10:22
2
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
1
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
add a comment |
I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.
It might even be as simple as
Vue.directive('can', function (el, binding)
return Laravel.permissions.indexOf(binding) !== -1;
)
I haven't tested this code. Just brainstorming here.
<button v-can="editStuff">You can edit this thing</button>
I can hold permissions this way:
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'userId' => Auth::user()->id,
'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
]); ?>
I'm literally working on this exact same thing. I'm thinking of adding a custom Vue directive that would check against the Laravel.permissions array.
It might even be as simple as
Vue.directive('can', function (el, binding)
return Laravel.permissions.indexOf(binding) !== -1;
)
I haven't tested this code. Just brainstorming here.
<button v-can="editStuff">You can edit this thing</button>
I can hold permissions this way:
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
'userId' => Auth::user()->id,
'permissions' => Auth::user()->permissions()->pluck('name')->toArray()
]); ?>
edited Nov 28 '18 at 6:15
answered Nov 15 '18 at 5:28
Ismoil ShifoevIsmoil Shifoev
1,3601914
1,3601914
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
2
I guess you'd just use<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likelylayout.blade.php
).
– Namoshek
Feb 8 at 10:22
2
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
1
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
add a comment |
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
2
I guess you'd just use<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likelylayout.blade.php
).
– Namoshek
Feb 8 at 10:22
2
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
1
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
How you get laravel permission array in vue js?
– Tejas Khutale
Nov 15 '18 at 5:56
2
2
I guess you'd just use
<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likely layout.blade.php
).– Namoshek
Feb 8 at 10:22
I guess you'd just use
<script type="text/javascript"> window.Laravel = permissions: Json.parse('@json(Auth::user()->permissions)') ;</script>
somewhere in a blade template (most likely layout.blade.php
).– Namoshek
Feb 8 at 10:22
2
2
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
I think you're going to want to set up an endpoint that you can hit from the vue front end that will give you the user permissions back, otherwise, in the example above, what prevents a user from editing their permissions and gaining access to everything.
– wheelmaker
Feb 11 at 17:40
1
1
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
@wheelmaker this would not matter as everything that requires a permission would still be behind an endpoint, its just for the frontend layout
– Michael Mano
Feb 11 at 21:36
add a comment |
I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.
Routes:
Route::get('/permission/permissionName', 'PermissionController@check');
Controller:
function check($permissionName)
if (! Auth::user()->hasPermissionTo($permissionName))
abort(403);
return response('', 204);
Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.
Vue.mixin("can", (permissionName) =>
let hasAccess;
axios.get(`/permission/$permissionName`)
.then(()=>
hasAccess = true;
.catch(()=>
hasAccess = false;
;
return hasAccess;
);
And then everytime you wanna check permission, you can just do
<el-input v-if="can('write-stuff')"> </el-input>
add a comment |
I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.
Routes:
Route::get('/permission/permissionName', 'PermissionController@check');
Controller:
function check($permissionName)
if (! Auth::user()->hasPermissionTo($permissionName))
abort(403);
return response('', 204);
Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.
Vue.mixin("can", (permissionName) =>
let hasAccess;
axios.get(`/permission/$permissionName`)
.then(()=>
hasAccess = true;
.catch(()=>
hasAccess = false;
;
return hasAccess;
);
And then everytime you wanna check permission, you can just do
<el-input v-if="can('write-stuff')"> </el-input>
add a comment |
I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.
Routes:
Route::get('/permission/permissionName', 'PermissionController@check');
Controller:
function check($permissionName)
if (! Auth::user()->hasPermissionTo($permissionName))
abort(403);
return response('', 204);
Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.
Vue.mixin("can", (permissionName) =>
let hasAccess;
axios.get(`/permission/$permissionName`)
.then(()=>
hasAccess = true;
.catch(()=>
hasAccess = false;
;
return hasAccess;
);
And then everytime you wanna check permission, you can just do
<el-input v-if="can('write-stuff')"> </el-input>
I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.
Routes:
Route::get('/permission/permissionName', 'PermissionController@check');
Controller:
function check($permissionName)
if (! Auth::user()->hasPermissionTo($permissionName))
abort(403);
return response('', 204);
Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.
Vue.mixin("can", (permissionName) =>
let hasAccess;
axios.get(`/permission/$permissionName`)
.then(()=>
hasAccess = true;
.catch(()=>
hasAccess = false;
;
return hasAccess;
);
And then everytime you wanna check permission, you can just do
<el-input v-if="can('write-stuff')"> </el-input>
answered Feb 12 at 17:15
ShuyiShuyi
464722
464722
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312875%2fvue-js-with-laravel-permission%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am also facing the same problem.
– Tejas Khutale
Nov 15 '18 at 5:25