【问题标题】:Laravel 5.4 change default username and password fieldsLaravel 5.4 更改默认用户名和密码字段
【发布时间】:2017-08-31 07:50:09
【问题描述】:

我继承了我目前正在移植到 laravel 的应用程序。他们已经设计了数据库,并且他们使用与 laravel 的默认设置不同的表进行身份验证。在数据库中,它们具有大写的字段名称。

电子邮件保存在名为MCP_EMAIL 的字段中,密码保存在名为MCP_PASSWORD 的字段中

我已设法更改注册流程,用户可以正常注册。 问题在于登录。当您尝试登录时,没有任何反应。没有错误消息,没有日志,什么都没有。它只是不断回到登录页面!

这是用户类。

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    use Notifiable;

    protected $table = "user_profiles";

    protected $primaryKey ="MCP_ID";

    protected $rememberTokenName = 'MCP_REMEMBER_TOKEN';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'MCP_DISPLAY_NAME', 'MCP_EMAIL', 'MCP_PASSWORD',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    const CREATED_AT  = "ADDED";
    const UPDATED_AT = "MODIFIED";

    public function getAuthPassword(){
        return $this->MCP_PASSWORD;
    }

    //seems to be a repetition of protected$rememberTokenName 
    public function getRememberTokenName()
    {
        return $this->MCP_REMEMBER_TOKEN;
    }

}

还有登录控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facade\Auth;
use Log;

class LoginController extends Controller
{       

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username(){
        return 'MCP_EMAIL';
    }

}

我错过了什么?为什么我看不到任何错误消息?

更新

我做的一切都是正确的。除了以上所有操作外,您还需要更改login.blade.php中输入的名称

<div class="form-group{{ $errors->has('MCP_EMAIL') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="MCP_EMAIL" type="email" class="form-control" name="MCP_EMAIL" value="{{ old('MCP_EMAIL') }}" required autofocus>

                                @if ($errors->has('MCP_EMAIL'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('MCP_EMAIL') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

见related question here

【问题讨论】:

    标签: laravel laravel-5 laravel-5.4


    【解决方案1】:

    您是否知道 L5.4 带有预构建的身份验证,以提供一种快速的方式来搭建您需要运行 php artisan auth:make 的脚手架,我很确定您需要在 AuthenticatesUsers trait 中附加一些功能,除非数据库列。看看这个AuthenticatesUsers

    如果您不使用身份验证脚手架:

    1) 无需从您的 LoginController 中删除 AuthenticatesUsers

    2)尝试根据本节Manually Authenticating Users实现您的LoginController

    【讨论】:

    • 我不确定您是否正确阅读了我的问题,或者我是否表达不清,但我使用的是内置身份验证。只是我的表没有被称为users,我的电子邮件字段没有被称为email。不过我找到了解决方案!
    猜你喜欢
    • 2022-01-16
    • 2017-07-30
    • 2017-12-07
    • 2018-06-21
    • 1970-01-01
    • 2018-07-10
    • 2013-11-23
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多