Laravel 4 user password with password

So, I found a few problems already stating that you need to override getAuthPassword () to provide a custom password column name from the database. I tried to put this method with the same name as in the column in the database, and did not work. It still removes this error: Undefined index: password.

This is auth:

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

I tried to replace user_password with a password both in the form and in the controller nothing works.

So the question is, do I have a column in the database called "user_password", is there a way to get Auth to work?

PS checked every old solution I found

EDIT

User table structure:

 +======================+ | User | +======================+ | user_id | +----------------------+ | user_displayName | +----------------------+ | user_fname | +----------------------+ | user_lname | +----------------------+ | user_email | +----------------------+ | user_password | +----------------------+ | created_at | +----------------------+ | updated_at | +----------------------+ 
+5
source share
4 answers

TL; DR; You can call your password field in any way convenient for you if your user model uses the interface correctly.

However, you cannot pass another array key to the Auth::attempt method - only password can be there

First of all, you are doing it wrong - you need to pass an array of credentials as the first parameter:

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

Further, unfortunately, the Eloquent provider has a hard-coded index of the password array in the code, so you cannot pass user_password to the attempt method.

So here 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'))) 
+7
source

I have not tested this, but I believe that you just need to override the function in UserTrait.php, although don't crack the source. In your /User.php model files, add the following function.

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

There is a static solution for situations where the implementation of this interface is insufficient (for example, 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
source

Here is my way to change the default email field to log into Laravel to email_address without changing the vendor code.

I created a feature class that extends the AuthenticatesUsers provider trait. And only an advanced 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 the application \ Http \ Controllers \ Controller \ LoginController.php:

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

And ... done! Hope this helps.

0
source

Source: https://habr.com/ru/post/947784/


All Articles