【问题标题】:Laravel 5.8 Custom Email and password ColumnsLaravel 5.8 自定义电子邮件和密码列
【发布时间】:2019-05-23 04:24:13
【问题描述】:

我有一个运行 WordPress 的应用程序,我想使用一个使用 Laravel 5.8 的单独界面来访问它。(不用担心散列)

因此,我不想来回克隆密码,而是使用 Laravel 用户模型中的 user_email 和 user_pass 列。

我已经尝试过官方文档所说的:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        $credentials = $request->only('user_email', 'user_pass');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}

然后我编辑了刀片文件,但无济于事。任何指针?

【问题讨论】:

  • 用户表中的列是否命名为user_emailuser_pass?如果是这样,您需要指定它们,因为它们不是默认值。 Auth::attempt(['user_email' => $request->user_email, 'user_pass' => $request->user_pass])。您还需要对 User 模型进行更改。 stackoverflow.com/questions/39374472/…

标签: wordpress laravel


【解决方案1】:

Laravel 提供了一种通过覆盖某些函数来更改身份验证(电子邮件、密码)的默认列的方法。

在你的用户模型中添加这个覆盖密码默认列的函数:

App/User.php

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->user_pass;
}

并且,在您的 LoginController 中从 email 更改为 user_email

App/Http/Controllers/Auth/LoginController.php

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'user_email';
}

现在你已经覆盖了 Laravel 的 Auth 逻辑使用的默认列。但你还没有完成。

LoginController 具有验证用户输入的功能,并且密码列被硬编码为password,因此为了更改它,您还需要在 LoginController 中添加这些功能:

App/Http/Controllers/Auth/LoginController.php

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 *
 * @throws \Illuminate\Validation\ValidationException
 */
protected function validateLogin(Request $request)
{
    $request->validate([
        $this->username() => 'required|string',
        'user_pass' => 'required|string',
    ]);
}


/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request)
{
    return $request->only($this->username(), 'user_pass');
}

下一步是创建一个自定义 Provider,我们将其命名为 CustomUserProvider,而不是默认的 EloquentUserProvider,您将在其中覆盖密码字段。

App/Providers/CustomUserProvider.php

<?php
namespace App\Providers;

class CustomUserProvider extends EloquentUserProvider
{
    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array  $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        if (empty($credentials) ||
           (count($credentials) === 1 &&
            array_key_exists('user_pass', $credentials))) {
            return;
        }

        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // Eloquent User "model" that will be utilized by the Guard instances.
        $query = $this->createModel()->newQuery();

        foreach ($credentials as $key => $value) {
            if (Str::contains($key, 'user_pass')) {
                continue;
            }

            if (is_array($value) || $value instanceof Arrayable) {
                $query->whereIn($key, $value);
            } else {
                $query->where($key, $value);
            }
        }

        return $query->first();
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  array  $credentials
     * @return bool
     */
    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['user_pass'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }
}

现在你扩展了默认提供者,你需要告诉 Laravel 使用这个而不是 EloquentUserProvider。这就是你可以做到的。

App/Providers/AuthServiceProvider.php


/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    $this->app->auth->provider('custom', function ($app, $config) {
        return new CustomUserProvider($app['hash'], $config['model']);
    });
}

最后更新配置信息config/auth.php,把驱动从eloquent改成custom(我上面就是这么命名的,你可以随便改)。所以config/auth.php文件应该有这个位:

'providers' => [
    'users' => [
        'driver' => 'custom',
        'model' => App\User::class,
    ],
],

希望对你有帮助!

问候

【讨论】:

    【解决方案2】:

    如果你可以在这里使用会话而不是像使用核心 PHP 一样使用 Auth::attempt,它就会正常工作。

    【讨论】:

      猜你喜欢
      • 2021-02-07
      • 2020-02-15
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      相关资源
      最近更新 更多