laravel 4 custom named password column

前端 未结 4 2038
情书的邮戳
情书的邮戳 2020-11-29 10:49

So I found a few problems already which says that you have to override getAuthPassword() to give custom name of password column from database. Tried putting this method with

相关标签:
4条回答
  • 2020-11-29 11:08

    Here is my way of changing the default email field of the Laravel login to 'email_address' without changing the vendor code.

    I made a trait class that extends the vendor AuthenticatesUsers trait. And only extended the username method.

    App\Http\Controllers\Auth\AuthenticatesLogins.php:

    namespace App\Http\Controllers\Auth;
    
    use Illuminate\Foundation\Auth\AuthenticatesUsers;
    
    trait AuthenticatesLogins
    {
        use AuthenticatesUsers {
            AuthenticatesUsers::username as parentUsername;
        }
    
        /**
         * Get the login username to be used by the controller.
         *
         * @return string
         */
        public function username()
        {
            $this->parentUsername();
            return 'email_address';
        }
    }
    

    Now in App\Http\Controllers\Controller\LoginController.php:

    class LoginController extends Controller
    {
        use AuthenticatesLogins; // <-- for custom login fields
        // etc.
    

    And... done! Hope this helps.

    0 讨论(0)
  • 2020-11-29 11:12

    There's a static solution for situations when implementing the given interface is not enough (e.g. you have two password columns, a user password and a support password).

    $qry = DB::table('users')
            ->where('email', $email)
            ->whereNotNull('support_password')
            ->first();
    
    if(!empty($qry))
    {
        $check = Hash::check($password, $qry->support_password);
        if ($check)
        {
            Auth::loginUsingId($qry->id, true);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 11:23

    tldr; You can name your password field anything you like, as long as your User model implements the interface correctly.

    However you can't pass different array key to the Auth::attempt method - only password index can be there

    First off you're doing it wrong - you need to pass an array of credentials as 1st param:

    if (Auth::attempt(Input::only('user_displayName', 'user_password')))
    

    Next, unfortunately Eloquent provider has hard-coded password array index in the code, so you can't pass user_password to the attempt method.

    So this is what you need:

    $credentials = Input::only('user_displayName');
    $credentials['password'] = Input::get('user_password');
    
    if (Auth::attempt($credentials))
    
    // or simply rename the input in your form to password and:
    if (Auth::attempt(Input::only('user_displayName', 'password')))
    
    0 讨论(0)
  • 2020-11-29 11:32

    I have not tested this but I believe you just have to override a function in UserTrait.php although don't hack the source. In your models/User.php file add the following function.

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }
    
    0 讨论(0)
提交回复
热议问题