【发布时间】:2022-10-02 14:03:34
【问题描述】:
当我尝试返回 Auth::user() 或返回 $request->user() 时,handle($request, Closure $next, ...$guards) 函数(在 Authenticate.php 中间件中)返回 null。该中间件为每条路由执行。我正在尝试返回 Auth::user() 因为我需要当前登录用户的电子邮件,返回 Auth::user() 是为了检查我是否可以获得有关当前登录用户的信息。
<?php
namespace App\\Http\\Middleware;
use Illuminate\\Auth\\Middleware\\Authenticate as Middleware;
use Closure;
use Illuminate\\Support\\Facades\\Cookie;
use PHPOpenSourceSaver\\JWTAuth\\Exceptions\\TokenExpiredException;
use PHPOpenSourceSaver\\JWTAuth\\Exceptions\\TokenInvalidException;
use PHPOpenSourceSaver\\JWTAuth\\Facades\\JWTAuth;
use Symfony\\Component\\Routing\\Exception\\RouteNotFoundException;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \\Illuminate\\Http\\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (!$request->expectsJson()) {
return route(\'login\');
}
}
public function handle($request, Closure $next, ...$guards)
{
return Auth::user();
return $next($request);
}
}
-
返回
Auth::user()?归还到哪里? -
我正在尝试用邮递员退回它
-
欢迎来到 SO...什么是默认守卫?
-
\'guards\' => [ // \'web\' => [ // \'driver\' => \'session\', // \'provider\' => \'users\', // ], \'api\' => [ \'driver\' => \'jwt\', \'provider\' => \'users\', ], ],
-
默认设置是什么?请将此信息添加到您的答案中,而不是在 cmets 中的代码块中
标签: php laravel null middleware