Home » vue » How to use Laravel auth() in a Vue component

How to use Laravel auth() in a Vue component

Posted by: admin November 26, 2021 Leave a comment

Questions:

I normally was using Auth in Laravel blade view to remove buttons that some users have no right too. Now i have just started lately trying to learn Vue and im wondering how im supposed to use this in a Vue Component.

This is what i were using in blade and would like to use in Vue component.

    @if(!Auth::guest() && ($post->author_id == Auth::user()->id))
        <a href="{{ url('home/edit/'.$post->slug)}}" class="btn-sm btn-info">Edit Post</a>
        <a href="{{  url('home/delete/'.$post->id.'?_token='.csrf_token()) }}" class="btn-sm btn-warning">Delete</a>
    @endif
Answers:

You can pass the User instance into the component with a prop. For example:

<example-component :user="{{ Auth::user() }}"></example-component>

Then you can access the user’s information like so:

<template>

 <p>{{ user.name }}</p>

</template>
export default {
   props : ['user']
}

###

There is no way for using default Laravel Auth. You should use special packages for API authentication such as Laravel Sanctum https://laravel.com/docs/8.x/sanctum