0
votes

First of all, I have seen this topics (and many others):

Laravel Auth::attempt() always false?

Laravel 5 Auth:attempt() always returns false

But I have been unable to login in my app. I'm using Laravel 5.6 and my table is called oc_users.

DB::table('oc_users')->insert(
[
    'email'     => '[email protected]',
    'pwd'       => bcrypt('123456'),
    'active'    => true
]);

I went to the file Laravel > App > User.php and changed to:

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table = 'oc_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['email', 'pwd'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['pwd', 'remember_token'];
}

And the code to perform the login:

public function form_login(Request $request)
{   
    if($request->filled('email') == FALSE || $request->filled('pwd') == FALSE)
        return Redirect::back()->withErrors('Fill the credentials');

    if (Auth::attempt(['email' => $request->email, 'pwd' => $request->pwd]))
        return redirect()->intended('app');

    return Redirect::back()->withErrors('Email or password wrong');       
}

I also went to the file App > Config > Auth.php trying to figure out if there's a table I need to change but it doesn't seem to exists in Laravel 5.6

2
Could you post the schema creation of your oc_users table?Julian van den Berkmortel

2 Answers

3
votes

From what I experienced before, auth::attempt() function has a default checking of the password column.

To make your code work, you could try this.

In your user model add:

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

This function will override the default getAuthPassword() function in the Authenticatable.php

0
votes

Laravel now uses bcrypt:

class User extends Model {

    public function setPasswordAttribute($value) {
         $this->attributes['password'] = bcrypt($value);
    }

}