Chivaa
Chivaa

Reputation: 78

Laravel 4 user authentication

I am doing a simple login and i am facing small problem, just can't figure out what's the problem.

Here is my auth.php :

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Authentication Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the authentication driver that will be utilized.
    | This driver manages the retrieval and authentication of the users
    | attempting to get access to protected areas of your application.
    |
    | Supported: "database", "eloquent"
    |
    */

    'driver' => 'eloquent',

    /*
    |--------------------------------------------------------------------------
    | Authentication Model
    |--------------------------------------------------------------------------
    |
    | When using the "Eloquent" authentication driver, we need to know which
    | Eloquent model should be used to retrieve your users. Of course, it
    | is often just the "User" model but you may use whatever you like.
    |
    */

    'model' => 'Login',

    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'tbl_user',

    /*
    |--------------------------------------------------------------------------
    | Password Reminder Settings
    |--------------------------------------------------------------------------
    |
    | Here you may set the settings for password reminders, including a view
    | that should be used as your password reminder e-mail. You will also
    | be able to set the name of the table that holds the reset tokens.
    |
    | The "expire" time is the number of minutes that the reminder should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

As you can see above, i am using my own model Login and my own table tbl_user

Below is my model (Login.php)

<?php
class Login extends Eloquent {
    protected $table = "tbl_user";

    public static function checkUser($array)
    {
        $data = DB::table('tbl_user')->where('user_email', $array['user_email'])->where('user_password', $array['user_password'])->get();

        return $data;
    }
}

Now i think there is something wrong with the model, i just don't know what it is. I am moving from CodeIgniter to Laravel 4 and this auth thingy is new for me.

Here is the route :

Route::post('login', function(){    
    $userdata = array(
        'user_email' => Input::get('email'),
        'user_password' => Hash::make(input::get('password'))
    );

    if(Auth::attempt($userdata)){
        echo "Login Success!";
    }else{
        echo "Login Failed!";
    }
});

I am getting Login Failed! password is hashed! Hurmm! Any suggestion?

Upvotes: 0

Views: 2153

Answers (2)

Anam
Anam

Reputation: 12199

As Petkostas mentioned, you have to implements UserInterface, RemindableInterface.

In addition, Fix this line as well:

$userdata = array(
        'user_email' => Input::get('email'),
        'user_password' => Hash::make(input::get('password')) // no need to hash password
    );

No need to hash password before Auth::attempt() as Laravel will do it for you.

I have removed Hash::make() from code:

$userdata = array(
        'user_email' => Input::get('email'),
        'user_password' => input::get('password')
    );

    if(Auth::attempt($userdata)){
        echo "Login Success!";
    }else{
        echo "Login Failed!";
    }

Upvotes: 0

petkostas
petkostas

Reputation: 7460

Auth uses a user model which implements the Userinterface, if you want to create your own custom User model you should implement the UserInterface, you can check the deault provided user model in your app:

<?php

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;
    }

}

Upvotes: 1

Related Questions