2
votes

I've been searching everywhere, but cannot figure out why my Auth::attempt() keeps failing. Please help me out.

This is my form:

<h2>Admin Login</h2>
<form method="post" action="<?php echo url('login-process') ?>">
    Username: <input type="text" name="username" /> <br/>
    Password: <input type="password" name="password" />
    <button type="submit">Login</button>
</form>

This is my login function:

$username = Input::get('username');
$password = Input::get('password');

if (Auth::attempt(array('username' => $username, 'password' => $password))) {

        echo "success!"; //just for testing purposes
} else {
        echo "fail!";

My User.php is the default one. looks like this:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {


/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

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

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

}

And this is how I save the new user information (using Eloquent):

$user = new User();
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();

But I keep getting fail when user logs in with correct credentials. Been working on this for a few days now, and can't help but feel that I'm missing something trivial. Lend a hand please? Thanks!

1
Is your password field in database at least 60 chars long? - Antonio Carlos Ribeiro
@AntonioCarlosRibeiro yes, it is 64 chars long - user2387902

1 Answers

0
votes

If you have some more error in this would be easier, an error message or something. But the following is how I log my users in. The new methods gave me errors since the latest install of Laravel 4.1.26.

It's not in the following part but it's formatted a bit cleaner I think. It could be the methods for the new remember token. Please give some more error info. Do you get a Laravel error. The echo you have will not be shown because it will not hold that screen. Maybe use return var_dump('Success') this will break the process.

$credentials = [
   "username" => Input::get("username"),
   "password" => Input::get("password")
];
if(Auth::attempt($credentials)) {
   return true;
} else {
   return false;
}

And it's a good thing to implement these methods in your User model which are necessary since 4.1.26:

public function getRememberToken() {
   return $this->remember_token;
}


public function setRememberToken($value) {
   $this->remember_token = $value;
}


public function getRememberTokenName() {
   return 'remember_token';
}