修改Laravel 5.2身份验证


Modify Laravel 5.2 Authentication

有人能帮我修改Laravel 5.2身份验证吗。我不知道如何正确调整默认数据库迁移和配置身份验证的某些部分。我已经迁移了数据库,并生成了身份验证的视图来测试身份验证。然后,我尝试回滚更改,因为我需要做的是更改password_resetsusers表的名称,以及更改这两个表字段的名称。我需要为password_resetsusers的所有字段添加前缀。我研究了如何做到这一点的方法并进行了测试。但是,每次提交表格时,我总是会出错,因为我修改了表格结构。我需要了解在调整数据库后,我还应该在哪里以及修改什么。有人能指导我做这个吗?我真的很感激。我是Laravel的新手,我真的想学习这个框架。

以下是我的进展:

所以我重新命名了这些表。我在userspassword_resets中都添加了前缀emr_然后,对于emr_users表的字段,我将前缀添加到具有usr_的字段,并且对于emr_password_resets,我添加了ps_

用户表:2014_10_12_000000_create_users_table

    //some codes
    Schema::create('emr_users', function (Blueprint $table) {
        $table->increments('usr_id');
        $table->string('usr_name');
        $table->string('usr_email')->unique();
        $table->string('usr_password');
        $table->rememberToken();
        $table->timestamps();
    });
    //some codes

password_resets表:2014_10_12_100000_create_password_resets_table

    //some codes
    Schema::create('emr_password_resets', function (Blueprint $table) {
        $table->string('ps_email')->index();
        $table->string('ps_token')->index();
        $table->timestamp('ps_created_at');
    });
    //some codes

在使用php artisan migrate迁移数据库并运行php artisan serve之后,一切看起来都很好,但当我测试登录表单并提交它时,我会收到以下错误:

2/2 QueryException in Connection.php line 673:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'emr.users' doesn't exist (SQL: select * from `users` where `email` = admin@sample.com limit 1)
...

1/2 PDOException in Connection.php line 333:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'emr.users' doesn't exist
...

我通过编辑/config/auth.php文件解决了上述错误。默认情况下,在这个文件的第73到76行注释掉一个代码块。我取消对代码块的注释,并在第75行将'table' => 'users'更改为'table' => 'emr_users',结果为:

<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App'User::class,
        ],
        'users' => [                      /* line 73 */
            'driver' => 'database',
            'table' => 'emr_users',       /* line 75 */
        ],                                /* line 76 */
    ],
    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | Here you may set the options for resetting passwords including the view
    | that is your password reset e-mail. You may also set the name of the
    | table that maintains all of the reset tokens for your application.
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
];

因此,错误消失并识别emr_users表。但当我试图从登录表单再次提交时,会产生另一个错误:

2/2 QueryException in Connection.php line 673:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from `emr_users` where `email` = admin@sample.com limit 1)
...

1/2 PDOException in Connection.php line 333:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause'

email字段更改为usr_email字段,这就是上面显示错误的原因。

这是AuthController.php 的内容

<?php
namespace App'Http'Controllers'Auth;
use App'User;
use Validator;
use App'Http'Controllers'Controller;
use Illuminate'Foundation'Auth'ThrottlesLogins;
use Illuminate'Foundation'Auth'AuthenticatesAndRegistersUsers;
class AuthController extends Controller {
    /*
      |--------------------------------------------------------------------------
      | Registration & Login Controller
      |--------------------------------------------------------------------------
      |
      | This controller handles the registration of new users, as well as the
      | authentication of existing users. By default, this controller uses
      | a simple trait to add these behaviors. Why don't you explore it?
      |
     */
use AuthenticatesAndRegistersUsers,
    ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return 'Illuminate'Contracts'Validation'Validator
     */
    protected function validator(array $data) {
        return Validator::make($data, [
                    'name' => 'required|max:255',
                    'email' => 'required|email|max:255|unique:users',
                    'password' => 'required|min:6|confirmed',
        ]);
    }
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data) {
        return User::create([
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'password' => bcrypt($data['password']),
        ]);
    }
    /**
     * Handle a login request to the application.
     *
     * @param 'Illuminate'Http'Request $request
     * @return 'Illuminate'Http'Response
     * 
     * Overrides AuthenticateUsers.php postLogin function
     */
    public function postLogin(Request $request) {
        $attempt_request = [
            'usr_email' => $request->email,
            'usr_password' => $request->password
        ];
        if (Auth::attempt($attempt_request)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}

我想了解如何正确配置这些表和字段,以及Laravel 5.2的身份验证设计或结构,以确定在哪里以及修改什么。

请帮忙。感谢

好的,首先使用laravel提供的默认postLogin函数。这很好,但如果您需要对身份验证过程有更多的控制,您可以覆盖此函数。

因此,在您的AuthController中,您将创建一个名为postLogin 的函数

<?php
/**
 * Class AuthController
 * @package App'Http'Controllers'Auth
 */
class AuthController extends Controller 
{
  ....
    /**
     * Handle a login request to the application.
     *
     * @param 'Illuminate'Http'Request $request
     * @return 'Illuminate'Http'Response
     */
    public function postLogin(Request $request) 
    {
       if (Auth::attempt(['usr_email' => $request->email, 'password' => $request->password]) {
        // Authentication passed...
        return redirect()->intended('dashboard');
      }
      ....
    }
}

现在给你的应用程序''用户模型添加以下功能

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

现在,对于您的密码重置表,只需在passwords.users.table 的config/auth.php文件中更改表的名称

 /*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| Here you may set the options for resetting passwords including the view
| that is your password reset e-mail. You may also set the name of the
| table that maintains all of the reset tokens for your application.
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
    'users' => [
        'provider' => 'users',
        'email' => 'auth.emails.password',
        'table' => 'emr_password_resets',
        'expire' => 60,
    ],
],

您没有电子邮件的列,因此出现了错误。

检查迁移的用户表,

您可能需要添加

$table->string('email')->unique();

然后运行,php artisan migrate。插入字段