登陆页面验证怎么处理??登陆用户选择记住我,下次进登陆页面不应该是直接跳到欢迎我的页面吗?

登陆页面验证怎么处理??登陆用户选择记住我,下次进登陆页面不应该是直接跳到欢迎我的页面吗?

吃饭、悟禅、打豆豆、在路上
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

按照文档所说

file

但是你直接使用viaRemember()这个函数是没用的

查看SessionGuard文件,可以发现viaRemeber这个属性是在SessionGuard::user()中被设置的

file

所以想在登录界面自动登录,只需要使用Auth::user()就行了

file

如果想只在用户选择记住我才自动登录

file

在选择了记住我时,laravel会生成一个相关的cookie,如remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d,名字由下面的函数生成

file

这样,如果当前的session已经失效,SessionGuard::user()会使用remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d的值去调用retriveveByToken来查找对应的用户,然后设置用户,这样就实现了自动登录

5年前 评论
softer 4年前
讨论数量: 10

按照文档所说

file

但是你直接使用viaRemember()这个函数是没用的

查看SessionGuard文件,可以发现viaRemeber这个属性是在SessionGuard::user()中被设置的

file

所以想在登录界面自动登录,只需要使用Auth::user()就行了

file

如果想只在用户选择记住我才自动登录

file

在选择了记住我时,laravel会生成一个相关的cookie,如remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d,名字由下面的函数生成

file

这样,如果当前的session已经失效,SessionGuard::user()会使用remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d的值去调用retriveveByToken来查找对应的用户,然后设置用户,这样就实现了自动登录

5年前 评论
softer 4年前

这是手册上的说明:

如果你想要提供「记住我」的功能,你需要传入一个布尔值到 attempt 方法的第二个参数,在用户注销前 session 值都会被一直保存。users 数据表一定要包含一个 remember_token 字段,这是用来保存「记住我」令牌的。

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // 这个用户被记住了...
}

在 LoginController 里面 use AuthenticatesUsers trait, 在里面可以找到 attemptLogin 方法,默认帮你处理了

protected function attemptLogin(Request $request)
    {
        $credentials = $this->credentials($request);
        return $this->guard()->attempt(
            $credentials, $request->has('remember')
        );
    }

如果要在深入一点的话可以查看 Illuminate\Auth\SessionGuardlogin 方法

public function login(AuthenticatableContract $user, $remember = false)
    {
        $this->updateSession($user->getAuthIdentifier());

        // If the user should be permanently "remembered" by the application we will
        // queue a permanent cookie that contains the encrypted copy of the user
        // identifier. We will then decrypt this later to retrieve the users.
        if ($remember) {
            $this->ensureRememberTokenIsSet($user);

            $this->queueRecallerCookie($user);
        }

        // If we have an event dispatcher instance set we will fire an event so that
        // any listeners will hook into the authentication events and run actions
        // based on the login and logout events fired from the guard instances.
        $this->fireLoginEvent($user, $remember);

        $this->setUser($user);
    }
5年前 评论

在你的登录哦控制器中加上 public function __construct(){ $this->middleware('geust',['only' => ['xxx','xxx']]) }

5年前 评论

可在 app/Http/Middleware/RedirectIfAuthenticated.php 中修改用户已登录的处理:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

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()) {
            session()->flash('info', '您已登录,无需再次操作');
            return redirect('/');
        }

        return $next($request);
    }
}

这个教程后续有讲到。

5年前 评论

按照文档所说

file

但是你直接使用viaRemember()这个函数是没用的

查看SessionGuard文件,可以发现viaRemeber这个属性是在SessionGuard::user()中被设置的

file

所以想在登录界面自动登录,只需要使用Auth::user()就行了

file

如果想只在用户选择记住我才自动登录

file

在选择了记住我时,laravel会生成一个相关的cookie,如remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d,名字由下面的函数生成

file

这样,如果当前的session已经失效,SessionGuard::user()会使用remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d的值去调用retriveveByToken来查找对应的用户,然后设置用户,这样就实现了自动登录

5年前 评论
softer 4年前

@GerBawn 写得不错,不过我感觉这里还是有点小问题,如果当用户的session还没有失效,那么Auth::viaRemember()还是返回false,这样反而无法自动登录。实际上,Auth::user()已经考虑的比较全面了,只有在session失效后才会去调用Auth::viaRemember(),这个Auth::viaRemember()已经包含在Auth::user()中了。。所以第一个写法应该就足够了:

file

不过这样并没有什么好处,纯属给自己添麻烦

5年前 评论

@hustnzj 请问 viaremember怎么在user()方法里面的 没找到

5年前 评论

我通过_ide_helper.php文件找到 Auth 类,在phpstorm中使用 ctrl + b,顺着查看 user() 静态方法如何实现的
file
file
file
file
确实如 hustnzj 所说。

4年前 评论

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