laravel 記住我 功能 修改時間

laravel 記住我 30 天 (框架默認(rèn)寫死了,5年)

解決方案

<?php

namespace App\Listeners;

use Cookie;
use Illuminate\Auth\Events\Login;
use Illuminate\Contracts\Cookie\QueueingFactory;

class SetCookieListener
{
    protected $cookieQueue;
    protected $cookie;

    /**
     * Create the event listener.
     *
     * @param QueueingFactory $cookieQueue
     * @param Cookie $cookie
     * @internal param QueueingFactory $cookie
     */
    public function __construct(QueueingFactory $cookieQueue, Cookie $cookie)
    {
        $this->cookieQueue = $cookieQueue;
        $this->cookie = $cookie;
    }

    /**
     * Handle the event.
     *
     * @param  Login $event
     */
    public function handle(Login $event)
    {
        $cookieName = \Auth::guard()->getRecallerName();
        $min = 30 * 24 * 60;

        if ($event->remember) {
            $value = $this->cookie->get($cookieName);
            $first_time = $this->cookie->get('first_time');
            if ($value && ! $first_time) {
                $this->cookieQueue->queue('first_time', 'changed', $min);
                $this->cookieQueue->queue($cookieName, $value, $min);
            }
        }
    }
}





<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'Illuminate\Auth\Events\Login' => [
            'App\Listeners\SetCookieListener',
        ],
    ];

分析

// 可以看到 中間件 RedirectIfAuthenticated 中檢查登陸,調(diào)到 sessionGuard 中的 check
class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }
}

// sessionGuard 中的 check

    public function check()
    {
        return ! is_null($this->user());
    }



// sessionGuard 中的 user
public function user()
    {
        if ($this->loggedOut) {
            return;
        }

        // If we've already retrieved the user for the current request we can just
        // return it back immediately. We do not want to fetch the user data on
        // every call to this method because that would be tremendously slow.
        if (! is_null($this->user)) {
            return $this->user;
        }

        $id = $this->session->get($this->getName());

        // First we will try to load the user using the identifier in the session if
        // one exists. Otherwise we will check for a "remember me" cookie in this
        // request, and if one exists, attempt to retrieve the user using that.
        if (! is_null($id)) {
            if ($this->user = $this->provider->retrieveById($id)) {
                $this->fireAuthenticatedEvent($this->user);
            }
        }

        // If the user is null, but we decrypt a "recaller" cookie we can attempt to
        // pull the user data on that cookie which serves as a remember cookie on
        // the application. Once we have a user we can return it to the caller.
        $recaller = $this->recaller();

  ### 當(dāng)用戶登陸過期并且記住我的 cookie 值不為空的時候,去拿取 cookie 中的 user 信息,并返回 #####
        if (is_null($this->user) && ! is_null($recaller)) {
            $this->user = $this->userFromRecaller($recaller);

            if ($this->user) {
                $this->updateSession($this->user->getAuthIdentifier());

                $this->fireLoginEvent($this->user, true);
        ## 觸發(fā)用戶登錄時間 ## ??通過監(jiān)聽登錄事件來改變 cookie
            }
        }

        return $this->user;
    }

如果你「記住」用戶,可以使用 viaRemember 方法來檢查這個用戶是否使用「記住我」 cookie 進行認(rèn)證

另一種記住我解決方案

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容