Support the ongoing development of Laravel.io →
posted 10 years ago
Authentication
Last updated 2 years ago.
0

check the output for Input::only('email', 'password') also change Form::open to this {{ Form::open( [ 'url' => '/login', 'method' => 'post' ] ) }}

Last updated 2 years ago.
0

in your app/config/auth.php: Did you specify "eloquent" or "database" as the desired auth driver? If you select "eloquent": does the "model" parameter point to your correct Model-class? If you select "database": does the "table" parameter point to your correct table?

if you use eloquent, did you specify the following function in your model-class?

public function getAuthIdentifier() {
    return $this->getKey();
  }

  public function getAuthPassword() {
    return $this->password;    ////////////// password = the columnname of your databasetable for the model
  }
Last updated 2 years ago.
0

ShiFoo - All is in order. I've spefied Eloquent and it points to my model class. And the model reflects exactly what you wrote.

arcollector - I changed the Form::open to what you suggested, although it threw an error with the [ ], so I changed it to array(). It took it but came back with an invalid login message.

For the Input::only, I subbed it for:

array('email'=>Input::get('email'), 'password'=>Input::get('password'))

And I'm still getting the invalid login message.

Last updated 2 years ago.
0

If you cannot use [], then you have an old PHP version, consider update it.

Last updated 2 years ago.
0

Did you check if your Input is working correctly e.g. by dumping Input::get('email') and check whether it has the expected value?

Last updated 2 years ago.
0

Ok. PHP version aside, any ideas why my Auth isn't working?

Last updated 2 years ago.
0

a silly question, is there any records in your users table??

Last updated 2 years ago.
0

ShiFoo said:

Did you check if your Input is working correctly e.g. by dumping Input::get('email') and check whether it has the expected value?

I am asking this because i once had a phenomenon where my Input facede hadn't had the proper keys but instead they were numbered.

Reference post from my in the archive: http://forumsarchive.laravel.io/viewtopic.php?id=17122

Last updated 2 years ago.
0

arcollector - There's one record in my database as a test. It has both a username and an email. I've tried both.

ShiFoo - I'm new to Laravel. Literally using this project as my first outing to learn it. How do I dump Input::get('email')? Are you talking about a dump from the database like so:

$users = DB::table('users')->get();

foreach ($users as $user)
{
    var_dump($user->email);
}

If so, it returned the correct data.

Last updated 2 years ago.
0

var_dump(Input::all()); var_dump(Input::get('email'));

Does these show proper values?

Last updated 2 years ago.
0

I'm sorry. I'm still picking this up. Where do I put this code?

Last updated 2 years ago.
0

a little tip: laravel comes with a function named dd (dump and die), so var_dump(Input::all()); var_dump(Input::get('email')); can be translated to dd( Input::all(), Input::get('email') )

do this:

Route::post('login', function()
{
	dd( Input::all());
	// rest of your code
} );
Last updated 2 years ago.
0

Thanks. I appreciate the patience.

So, I added this to my route:

dd(Input::all(), Input::get('email'));

And it returned:

array(3) { ["_token"]=> string(40) "sMg2Y3iwfOCcyWCoxtBdwo34EwfJY5SeKsDFXgPY" ["email"]=> string(16) "[email protected]" ["password"]=> string(7) "1234" } string(16) "[email protected]"

Looks right to me.

Last updated 2 years ago.
0

Is your primary key of the user table a varchar (in this case your email) or an unsigned integer? Did you create your user table with a database migration of laravel/artisan or by hand?

AFAIK, the primary key has to be a unsigned integer. If yours is an integer try:

Route::post('login', function()
{
Auth::loginUsingId(1); // ... where 1 is the primary key of the desired record in your user table
}
Last updated 2 years ago.
0

what version of laravel are you using? maybe 3.0? in that case there is an option to setup in the file located at application/config/auth.php called username that refers to a column name in the users table to make possible auth works properly, you need to change it to email

Last updated 2 years ago.
0

arcollector - I'm using Laravel 4.

ShiFoo - There is a primary key in my users table that's an integer and unsigned. When I used your example (which I replaced with everything I previously had in the route), it brought me to a blank page.

Last updated 2 years ago.
0
Route::post('login', function()
{
Auth::loginUsingId(1); // ... where 1 is the primary key of the desired record in your user table
dd( Auth::check() );
} );

check() must return true

Last updated 2 years ago.
0

It returned true.

Last updated 2 years ago.
0

Maybe this will work.

// routes.php
Route::post('login', array('as' => 'auth-login', 'uses' => 'AuthController@postLogin'));

// Controller
public function postLogin() {
    $credentials = array(
        'username'	=> Input::get('username'),
        'password'	=> Input::get('password')
    );

    if ( ! Auth::attempt($credentials) ) {
        return 'Invalid username or password.';
    }

  dd( Auth::user() );
}

// View
{{ Form::open(array('route' => 'auth-login')) }}
Last updated 2 years ago.
0

go to laravel\vendor\laravel\framework\src\Illuminate\Auth, open the file: EloquentUserProvider.php, put this code in the function named

public function retrieveByCredentials(array $credentials)
{
        // above code...
	dd( $query->toSql(), $query );  /// <- put this code
	return $query->first();
}

you must see this output:

string 'select * from `users` where `email` = ?' (length=39)

and following a lot of code, find one named:

protected 'bindings' => 
        array (size=1)

it's must contain the email address that you submitted with the form

it's everything is good, modify the next function to more debugging, by previously deleting the above code modification, in order to continue with the normal program flow

public function validateCredentials(UserInterface $user, array $credentials)
{
	$plain = $credentials['password'];
	dd( $this->hasher->check($plain, $user->getAuthPassword() ) ); /// <--- put this code
	return $this->hasher->check($plain, $user->getAuthPassword());
}

you must see:

boolean true
Last updated 2 years ago.
0

@arcollector So what i get false from the dd() in validateCredentials method?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.