Closed jelledijkstra97 closed 5 years ago
Try to import
use Cartalyst\Sentinel\Sentinel;
and then replace
public function __construct(Guard $auth)
with
public function __construct(Sentinel $auth)
and make use of it instead of the facade.
if ($this->auth->guest()) {
...
Thanks for the reply!
Unfortunately this did not solve my problem. The docs say to use Cartalyst\Sentinel\Laravel\Facade\Sentinel right?
Here's my kernel.php, maybe that gives a clue.
<?php
namespace Schedules\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Schedules\Http\Middleware\TrustProxies::class,
\Schedules\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\Schedules\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\Illuminate\Session\Middleware\StartSession::class,
\Schedules\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Schedules\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => 'Schedules\Http\Middleware\Authenticate',
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \Schedules\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
/**
* The priority-sorted list of middleware.
*
* This forces non-global middleware to always be in the given order.
*
* @var array
*/
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Schedules\Http\Middleware\Authenticate::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
}
Any other suggestions?
What do you have in the RedirectIfAuthenticated
class?
<?php
namespace Schedules\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Sentinel;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Sentinel::check())
{
return redirect('/');
}
return $next($request);
}
}
Can't reproduce this issue:
.env
file to point to my databasephp artisan migrate
Sentinel::guest()
returns false
Try to reproduce the problem in a fresh Laravel installation, if it occurs, push the exact code to a public repository and link it here please.
Going to close it for now as it seems a problem on your application.
Your Environment
Expected behaviour
I expect the auth middleware to use my signed in user.
I have a Laravel 6.0 app thats uses Sentinel for authentication. I can succesfully log in however after redirecting to my home page a auth middleware redirects me back to the login page. After log in a new row is inserted in the persistences table and inside my session a variable called 'cartalyst_sentinel' is stored with the same value as the persistence row.
When I call Sentinel::findByPersistenceCode with the session code inside the middleware, the current user is returned, however one line later Sentinel::guest() returns true.
Auth controller:
Auth middleware:
In both parts the correct user is printed to my log. But after that Sentinel::guest() returns true.
How can this be?