无法获取经过身份验证的数据

时间:2020-11-12 09:21:09

标签: php laravel laravel-sanctum

我有这个全局中间件,可以更新用户的上一次活动

<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Closure;

class HandleLastActivity
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (auth()->user()) {
            $user = auth()->user();
            $user->lastActivity = now();
            $user->save();
        }

        return $next($request);
    }
}

,但是auth()-> user()始终为null。我正在使用Sanctum,它可以在auth中间件下的路由中工作。

env

SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN=.mydomain.com

SANCTUM_STATEFUL_DOMAINS=mydomain.com,www.mydomain.com,api.mydomain.com

config / auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'sanctum',
            'provider' => 'users',
        ],
    ],

0 个答案:

没有答案