cmgmyr / laravel-messenger

Simple user messaging package for Laravel
MIT License
2.46k stars 517 forks source link

Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given #82

Closed shep1990 closed 8 years ago

shep1990 commented 9 years ago

Hi there,

I am getting the following issue whenever I run the messenger application and since I am new to Laravel I am not 100% sure what is causing this problem. I have tried Googling the error but I am not getting a clear answer on this.

Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

Could you help?

Thanks,

Rich

cmgmyr commented 9 years ago

Do you have Authenticatable on your User model? If you have your code up on GitHub I can take a closer look, but there could be a few things going on. Not sure if you can share or not.

shep1990 commented 9 years ago

Hi there,

Thank you very much for getting back to me! I do have Authenticatable in my User model, I have put my code on github, https://github.com/shep1990/messenger. This is just a clean instillation of Laravel with Messenger installed, I wanted to see if it happened on a clean install.

Thanks again.

Richard.

brianveltman commented 8 years ago

Having the same issue.

brianveltman commented 8 years ago

Of course.. the example code of MessagesController.php is using a user with the ID of 1. If the user does not exist an error will be thrown.

I have updated my code to this:

MessagesController.php

public function __construct()
    {
       $user = Auth::user();
    }
cmgmyr commented 8 years ago

The constructor in the example controller should be removed in the real application, and a better authentication process should be put into place. The initial code is just for testing purposes. Once this is removed, you should be all set...

/**
* Just for testing - the user should be logged in. In a real
* app, please use standard authentication practices
*/
public function __construct()
{
    $user = User::find(1);
    Auth::login($user);
}
nfebe commented 8 years ago

I am having a similar problem, something like :: FatalErrorException in User.php line 10: Interface 'Illuminate\Contracts\Auth\Authenticable' not found

khanakia commented 8 years ago

Hi Add code like below in User.php Model File

<?php
namespace App;
use Eloquent;

// use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;

class User extends Eloquent implements Authenticatable
{
     use AuthenticableTrait;

    // ... Other Code
}
desertebs commented 7 years ago

still not working help plz..

antonkomarev commented 7 years ago

In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class MessengerController extends Controller
{
    protected $user;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->user = Auth::user();

            return $next($request);
        });
    }
}

Or just don't try to get authenticated user in constructor and get in in other controller public methods by typehinting instance of \Illuminate\Http\Request class or use Auth facade.

public function index(Request $request)
{
    $user = $request->user();
    $user = Auth::user();
}
digu087 commented 7 years ago

Do not change anything inside User.php (Model file), keep it as default, as it is at the time of installation of new laravel file. User.php file will be as it is as below, <?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable;

class User extends Authenticatable { use Notifiable;

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

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

}

supershazwi commented 7 years ago

I got this error when trying to use socialite. I checked both my 'social_providers' table and 'users' table and realise that the row in 'social_providers' table was referencing the wrong user_id. So I just changed the ids to match and I stopped getting this error.

Not sure whether this will help anyone.

prkspkrl commented 7 years ago

i have same problem what can i do ??

ashish135 commented 7 years ago

you need to return $user after the create() method.

CrhistianDLM commented 5 years ago

Probaste cambiando Auth::login($user->id) a Auth::login($user)

nickforbizz commented 5 years ago

my issue ...

Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, null given, called in C:\xampp\htdocs\writersBay\app\Http\Controllers\Admin\Auth\RegisterController.php

nkmswot commented 5 years ago

@nickforbizz i too having the same issue.. I'm on the middle of 5.1 to 5.2 now... what can i do now?

nickforbizz commented 5 years ago

I think in the user model, you should extend authenticatable instead of model and also import it...

On Tue, Jun 25, 2019, 10:30 AM Karthik SWOT notifications@github.com wrote:

@nickforbizz https://github.com/nickforbizz i too having the same issue.. I'm on the middle of 5.1 to 5.2 now... what can i do now?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cmgmyr/laravel-messenger/issues/82?email_source=notifications&email_token=AJQ2JASCCGBRXE44IOXZW5TP4HCQ7A5CNFSM4BRDDU72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYPJ67Q#issuecomment-505323390, or mute the thread https://github.com/notifications/unsubscribe-auth/AJQ2JAWVEXR7JMM6ONLJPEDP4HCQ7ANCNFSM4BRDDU7Q .

DengSihan commented 4 years ago

Laravel 6.2 I got the same issue. Different from the above. My User model is

namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
abstract class User extends Authenticatable
{
    use Notifiable,SoftDeletes;
    ...
}

And I am building a SPA by vue and Laravel, I use api as my guards instead of web in config/auth.php

'guards' => [
    // 'web' => [
    //     'driver' => 'session',
    //     'provider' => 'users',
    // ],
    'api' => [
        'driver' => 'token',
         // 'provider' => 'users',
        'provider' => getSubdomain(),
        'hash' => false,
    ],
],
...
'providers' => [
     // 'users' => [
     //     'driver' => 'eloquent',
     //     'model' => App\Models\User::class,
     // ],
    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
    'worker' => [
        'driver' => 'eloquent',
        'model' => App\Models\Worker::class
    ],
],

then I add

    'index' => [
        'driver' => 'eloquent',
        'model' => null
    ],

to providers in config/auth.php I fixed it. so, you need to provide a provide for each part even if it is empty.

hope it can helps you, sorry for poor in English

maseranw commented 4 years ago

I got this error when trying to use socialite. I checked both my 'social_providers' table and 'users' table and realise that the row in 'social_providers' table was referencing the wrong user_id. So I just changed the ids to match and I stopped getting this error.

Not sure whether this will help anyone.

Thanks,

NkazimuloMvundla commented 4 years ago

gfffh