rojtjo / sentinel-guard

MIT License
0 stars 2 forks source link

Can't use Auth::id() or $user->can() #1

Open ivansammartino opened 6 years ago

ivansammartino commented 6 years ago

Detailed description

Hello, I'm a Laravel novice. I'm not sure if I'm misunderstanding the use of sentinel-guard, but I am encountering a couple of issues, hope you can help...

I setup simple routes like this, just to test (the first route authenticates through Sentinel, the second retrieves user via Auth):

Route::get('sentinel/authenticate', function(){
    $user = Sentinel::authenticate(array(
        'email'    => 'mymail@mydomain.example',
        'password' => 'mypassword',
    ));
    return $user;
});

Route::get('sentinel/check', function(){
    $user = Auth::user();
    dd($user);
});

So far so good! I can authenticate with powerful Sentinel and then "switch" to use native Laravel Auth's method (is this the intended purpose of the class, right?)

But when I try to retrieve the user ID with $user = Auth::id(); I get an error Call to undefined method Illuminate\Database\Query\Builder::getAuthIdentifier()

Also when I try to see if user can do some task with:

$user = Auth::user();
dd($user->can('update'));

I get this error: Call to undefined method Illuminate\Database\Query\Builder::can()

Did I understand the purpose of the class? Or am I totally wrong?

My environment: Windows 10 (Laragon), Laravel, 5.5.*, PHP 7.1.2

rojtjo commented 6 years ago

Hi there,

I'm sorry for my late response here, looks like I didn't receive a notification for this.

The main reason I wrote this was to be able to use the actingAs() method when writing tests. Therefore I did not include the authorization part of it.

When you use Auth::user() to retrieve the authenticated user it will return an instance of Rojtjo\SentinelGuard\User which extends the default Cartalyst\Sentinel\Users\EloquentUser.

Neither of these classes use Illuminate\Foundation\Auth\Access\Authorizable which provided the can() method. You should however be able to extend the Rojtjo\SentinelGuard\User class and then use the Illuminate\Foundation\Auth\Access\Authorizable trait on it.