Laravel manual authentication: Auth :: try always false

I use the Russian laravel authentication system. Form submission redirects to this route shown below. And in the authenticate () function, the username and password never match the one I saved earlier. those. Auth::attempt always false.

  Route::post('/logintest', ' mycontroller@authenticate '); Route::get('/home', ['middleware' => 'auth', function() { echo "home page";}); }]); 

authenticate function:

 public function authenticate(Request $request) { $input=$request->all(); $password=$input['password']; $name=$input['name']; if (Auth::attempt(['Name' => $name, 'Password' => $password]) ){ return redirect()->intended('/home'); } else { return redirect('/login')->with('message','Error logging in!'); } } 

I registered the user this way. the password is hashed using bcrypt (). function. but in the authenticate () function, I compare with a simple password. I read somewhere Auth automatically processes it. OR Should I change something in config / auth.php because I used the name for authentication instead of the username?

 public function register(Request $request) { $input=$request->all(); $password=bcrypt($input['password']); $name=$input['name']; $insert= User::insert(['Name'=>$name,'Password'=>$password]); return redirect('/login') ->with('message','successfully Registered.'); } 
+6
source share
9 answers

Check out the code below

 public function authenticate(Request $request) { $password = $request->input('password'); $name = $request->input('name'); if (Auth::attempt(['name' => $name, 'password' => $password]) ) { return redirect()->intended('/home'); } else { return view('login')->withErrors('Error logging in!'); } } 
+3
source

The problem with the names. Auth@attempt accepts all these credentials except the password (case-sensitive), which you pass in this array and launches the where query (this way you can add additional restrictions for the attempt, since they are only conditions). If he finds a model, he will perform a hash check on the password credentials (case sensitive) that you passed, and the model hashes the password that he receives from $model->getAuthPassword() .

This field in the credentials is special, because this is what Auth requires so that it knows which field in the credentials means the password. It does not correlate directly with the field that you used in the users table, and should have the name password in the credential array. Other fields in the credentials that you submit, in addition to the "password", directly correlate with the fields in the users table, since they are conditions for querying the database in this table.

You must declare a user in your model if you use a password field other than the password in the table. In your case, you use the "Password". (it all depends on the case)

 class User .... { ... public function getAuthPassword() { return $this->Password; // case sensitive } ... } 

When transferring credentials, you pass the plaintext password, as hash_check will happen, not a direct comparison.

You can name the fields you ever want on your actual table, you just need to do Eloquent about it.

+3
source

How do you use the name instead of email (by default) as the username for authentication. You must add the $username property to your AuthController .

 .... class AuthController extends Controller { use AuthenticatesAndRegistersUsers, ThrottlesLogins; /** * Override the input name 'email' * Change it as the name on blade * * @var string $username */ protected $username = 'Name'; .... } 

Alternatively, you can override the loginUsername() method from Illuminate\Foundation\Auth\AuthenticatesUsers .

 .... class AuthController extends Controller { use AuthenticatesAndRegistersUsers, ThrottlesLogins; /** * Get the login username to be used by the controller. * * @return string */ public function loginUsername() { return 'Name'; } .... } 

Like others, business matters. Then you need to override the getAuthPassword() method from the Illuminate\Auth\Authenticatable tag on your User model

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

Everything looks right.

What are the column names in the user table?

Names are case sensitive. So make sure they are really Name and Password , not Name and Password .

0
source

Why don't you use the php artisan make:auth ? He will do everything you need.

0
source
 Route::group(['middleware' => ['web']], function () { Route::post('/logintest', ' mycontroller@authenticate '); }); 
  • check out the above change in your Routes.php if you are using version 5 or 5.2

  • Make sure the field names in your usernames "Name" and "Password" update it again.

  • Check the length of the Password field field (in your database, users ). It should contain a long, hashed password, something like this $2y$10$eM.kmjTwEIykhNUqMsNzMud0E6eO6RUYAzTqirrbozY1zdhVwQmsC atleast, (varchar (60))

It would be better if you could show us the schema of user tables

  1. Finally, make sure that you enter the correct password (since I do not see many errors in your code)
0
source

If you want to use Name as a unique username and password fiendname as Password

You can do it.

In your AuthController add this method

 public function loginUsername() { return 'Name'; } 

And in your user model add this method

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

Hope this works.

0
source

Try switching to subscripts on this line:

 //... if (Auth::attempt(['Name' => $name, 'Password' => $password]) ) { //... 

For

 //... if (Auth::attempt(['Name' => $name, 'Password' => $password]) ) { //... 
0
source

You must write the password symbol p in a lowercase letter.

Replace

 Auth::attempt(['Name' => $name, 'Password' => $password]) 

to

 Auth::attempt(['Name' => $name, 'password' => $password]) // 'p' is in small letter here. 

also check this link.

0
source

All Articles