简体   繁体   English

Laravel,Inertia.js 和 vue,检查用户是否登录

[英]Laravel, Inertia.js and vue, check if user is logged In

I'm using laravel and inertia.js to implement my project.我正在使用 laravel 和惯性.js 来实现我的项目。 In my navbar I want to display div element with some user details if the user is logged in. And if the user is not logged in the div should not appear.在我的导航栏中,如果用户已登录,我想显示带有一些用户详细信息的 div 元素。如果用户未登录,则不应出现 div。 I have tried this but its not showing the details neither when I am logged in nor when I'm not.我已经尝试过了,但是当我登录或未登录时,它都没有显示详细信息。 What should I do?我应该怎么办?

<div class="ml-3 relative" v-if="$page.props.auth.user">
    <div>
        <button @click="dropDown=true"
                class="bg-gray-800 flex text-sm rounded-full focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white"
                id="user-menu" aria-haspopup="true">
            <span class="sr-only">Open user menu</span>
            <img class="h-8 w-8 rounded-full object-cover" :src="$page.props.user.profile_photo_url"
                 :alt="$page.props.user.name"/>
        </button>
    </div>
    <div v-if="dropDown"
         class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg py-1 bg-white ring-1 ring-black ring-opacity-5"
         role="menu" aria-orientation="vertical" aria-labelledby="user-menu">
        <a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem">
            <form method="POST" @submit.prevent="logout">
                <jet-responsive-nav-link as="button">
                    Logout
                </jet-responsive-nav-link>
            </form>
        </a>
    </div>
</div>

On AppServiceProvider在 AppServiceProvider 上

public function boot()
{
    //check if user is logged in
    Inertia::share('auth.user', function() {
        return ['loggedIn' => Auth::check()];
    });
}

With Inertia you can create a HandlerInertiaRequest Middlware.使用 Inertia,您可以创建 HandlerInertiaRequest Middlware。 The HandleInertiaRequests middleware provides a "share" method where you can define the data that is automatically shared with each Inertia response. HandleInertiaRequests 中间件提供了一个“share”方法,您可以在其中定义与每个 Inertia 响应自动共享的数据。 In Your case thats the user which is logged in.在您的情况下,这就是登录的用户。

You can find more infos in the docs Shared Data .您可以在文档Shared Data中找到更多信息。 They have an example in there demo app repository .他们在演示应用程序中有一个示例。

In your view you can check if the user is logged in by checking the shared data user prop.在您看来,您可以通过检查共享数据用户属性来检查用户是否已登录。

v-if="$page.props.auth.user"

I guess you can follow the same approach built here in this middleware, https://github.com/inertiajs/pingcrm/blob/master/app/Http/Middleware/HandleInertiaRequests.php and then you separated the logic from the providers我想您可以按照此中间件https://github.com/inertiajs/pingcrm/blob/master/app/Http/Middleware/HandleInertiaRequests.php中构建的相同方法,然后将逻辑与提供程序分开

Laravel 8.4.0 with InertiaJS returns $page.props.user by default if user logged in (see this answer ). Laravel 8.4.0 与 InertiaJS 如果用户登录,则默认返回$page.props.user (请参阅此答案)。 So you can simply do,所以你可以简单地做,

<element v-if="$page.props.user">This will only show when a user logged in.</element>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM