重定向后,为什么我的Sentinel用户不可用?

时间:2019-09-24 16:17:59

标签: php laravel cartalyst-sentinel

我有一个Laravel 6.0应用,该应用使用Sentinel进行身份验证。我可以成功登录,但是在重定向到我的主页后,身份验证中间件将我重定向到登录页面。登录后,将新行插入到持久性表中,并在我的会话中存储一个名为“ cartalyst_sentinel”的变量,其值与持久性行相同。

当我使用中间件内部的会话代码调用Sentinel :: findByPersistenceCode时,将返回当前用户,但是稍后,Sentinel :: guest()返回true。

身份验证控制器:

<?php namespace Schedules\Http\Controllers;

use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Http\Request;
use Lang;
use Laracasts\Flash\Flash;
use Log;
use Illuminate\Support\Facades\Session;
use Activation;

/**
 * Class AuthController
 * @package Schedules\Http\Controllers
 */
class AuthController extends Controller
{
    /**
     * AuthController constructor.
     */
    function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getLogin()
    {
        return view('auth.login');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postLogin(Request $request)
    {
        $credentials = $request->except(['_token']);

        if (empty($credentials['email']) || empty($credentials['password'])) {
            Flash::error(Lang::get('auth.messages.empty_fields'));
            return redirect('login');
        }

        if (Sentinel::authenticate($credentials, true)) {
            log::debug("Succesfully logged in");
            Log::debug(Sentinel::getUser()); // This prints the correct user
            return redirect('/');
        } else {
            Flash::error(Lang::get('auth.messages.invalid_login'));
        }

        return redirect('login');
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function getLogout()
    {
        Sentinel::logout(null, true);

        return redirect('login');
    }

}

验证中间件:

<?php namespace Schedules\Http\Middleware;

use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Log;
use Session;

class Authenticate {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard $auth
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Sentinel::findByPersistenceCode(Session::get('cartalyst_sentinel'));
        Log::debug($user); // This prints the correct user

        if (Sentinel::guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('login');
            }
        }

        return $next($request);
    }

}

在这两部分中,正确的用户都被打印到我的日志中。但是之后,Sentinel :: guest()返回true。

怎么可能?

0 个答案:

没有答案