为了账号安全,请及时绑定邮箱和手机立即绑定

laravel auth()->attempt() 是否写任何会话

laravel auth()->attempt() 是否写任何会话

PHP
扬帆大鱼 2022-12-23 14:37:45
我几个月前才学习 Laravel。我想知道一个小问题。我使用 auth()->attempt() 成功进行了身份验证。Laravel 是否自动添加任何 session() 变量?如果没有,是需要自己添加session变量,还是需要在某处设置。, 非常感谢你!这是我的控制器的一部分    if (!$check = auth()->attempt([      'member_username' => request()->$input('username'),      'password' => request()->$input('pwd'),      'login_enable' => 1    ])){      //processing login successfully    }这是 web.php 的一部分Route::group(['middleware' => 'auth'], function(){// Login successfully route});这是 auth.php 的一部分    'guards' => [        'web' => [            'driver' => 'session',            'provider' => 'account',        ],        'api' => [            'driver' => 'token',            'provider' => 'users',        ],    ],    'providers' => [        'users' => [            'driver' => 'eloquent',            'model' => App\User::class,        ],        'account' => [          'driver' => 'eloquent',          'model' => App\Entity\AccountMain ::class,                ],    ],
查看完整描述

1 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

当您的守卫身份验证为session时,它使用Illuminate\Auth\SessionGuard该类。你可以在这里看到它的attempt方法:


/**

     * Attempt to authenticate a user using the given credentials.

     *

     * @param  array  $credentials

     * @param  bool  $remember

     * @return bool

     */

    public function attempt(array $credentials = [], $remember = false)

    {

        $this->fireAttemptEvent($credentials, $remember);


        $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);


        // If an implementation of UserInterface was returned, we'll ask the provider

        // to validate the user against the given credentials, and if they are in

        // fact valid we'll log the users into the application and return true.

        if ($this->hasValidCredentials($user, $credentials)) {

            $this->login($user, $remember);


            return true;

        }


        // If the authentication attempt fails we will fire an event so that the user

        // may be notified of any suspicious attempts to access their account from

        // an unrecognized user. A developer may listen to this event as needed.

        $this->fireFailedEvent($user, $credentials);


        return false;

    }

你可以看到它调用了login这个守卫的方法


/**

     * Log a user into the application.

     *

     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user

     * @param  bool  $remember

     * @return void

     */

    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);

    }

电话就是您的updateSession答复。


查看完整回答
反对 回复 2022-12-23
  • 1 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信