关于 “记住我” 这个功能的疑问

为什么当我登录账号时点击“记住我”,登录进去后再退出,“记住我”这功能没有效果

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6

「记住我」会在浏览器保存 cookie,用于关闭浏览器或页面后再次打开网站时「记得」我是谁,点击页面上的「退出」后 cookie 会被清除,所以不「记得」你是谁了。

4年前 评论
SZL_ (楼主) 4年前

报错了@Zhibin
file

4年前 评论
Zhibin 4年前
SZL_ (作者) (楼主) 4年前
SZL_ (作者) (楼主) 4年前
Zhibin 4年前
SZL_ (作者) (楼主) 4年前
SZL_ (作者) (楼主) 4年前

是空的@Zhibin
file

4年前 评论
Zhibin 4年前
SZL_ (作者) (楼主) 4年前

还有一个问题,通过您给我发的修改期限的教程,我做完后发现,无论有没有选中 “记住我”,都会生成 cookie,并且在退出后并没有清除 cookie,中途我用$request->cookie()发现rememberweb...的值也是空的

但我把代码退回到修改前,使用5.8自带的功能,再用$request->cookie()发现rememberweb...里有值了,并且一切操作都是正常的

所以我想问,怎么在原基础上去重新设置cookie的时限,不然默认五年太长了 @Zhibin

4年前 评论

@SZL_
使用中间件吧,给你个示范,刚写的测试过可以用,具体可以自己再优化:

namespace App\Http\Middleware;

use Closure;
use Auth;
use Str;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;

class AfterLogin
{
    protected $cookies;
    /**
     * Create a new CookieQueue instance.
     *
     * @param  \Illuminate\Cookie\CookieJar  $cookies
     * @return void
     */
    public function __construct(CookieJar $cookies)
    {
        $this->cookies = $cookies;
    }
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // dd($request);
        $response = $next($request);

        if ($request->getMethod() != 'POST') {
            return $response;
        }

        if (!Str::endsWith($request->getRequestUri(), 'login')) {
            return $response;
        }

        $rememberTokenName = Auth::getRecallerName();
        $cookie = $this->cookies->queued($rememberTokenName);

        if (is_null($cookie)) {
            return $response;
        }

        $cookieValue = $cookie->getValue();

        $this->cookies->queue($rememberTokenName, $cookieValue, 1440);

        return $response;
    }
4年前 评论
Zhibin (作者) 4年前
SZL_ (楼主) 4年前

话说你知道如何实现单用户登录吗@Zhibin

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!