简体   繁体   中英

Laravel Auth::attempt

I am stuck with Auth::attempt for an api.

UserController.php

$credentials = ['user_email' => $data['email'],'user_password' => md5($data['password'])];
if (Auth::attempt($credentials)) {
    return Redirect::route("user/profile");
}

User.php

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface{

    public $timestamps = false;
    protected $table = 'ofalt_users';
        protected $hidden = array('password');
    protected $primaryKey = 'user_id';

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }
    public function getAuthPassword()
    {
        return $this->user_password;
    }
    public function getReminderEmail()
    {
        return $this->user_email;
    }

}

auth.php

return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => '',
'reminder' => array(
    'email' => 'emails.auth.reminder',
    'table' => 'password_reminders',
    'expire' => 60,
    ),
);

error message

Its returning ErrorException since I am using RESTClient to test the api I can't debug easily the errors. Can somebody please help me.

Laravel provided

Hash::make()

to make your life easier,

eg

function postCreateAccount()
{
    // validate and messages and whatever you want, if all passes:
    $data = Input::all();
    User::create(array(
        'username' => $data['username'],
        'password' => Hash::make($data['password'])
    );
}

function postLogin()
{
    // validate.... if passes
    $data = Input::all();
    if(Auth::attempt(array(
        'username'=>$data['username'],
        'password' => $data['password'])
    ))
    {
        return Redirect::to('profile');
    }
    else
    {
        return Redirect::to('login');
    }
}

you don't need to use MD5, Laravel uses bcrypt, which as far as i know a better hash algorithm when it comes to passwords

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM