Hi All,
I have a few different sites/domains and working to share some user information when a user clicks a link between the sites. I created a middleware class to check if the URL variable exists and to merge the user info -- technically a JWT token (tymons jwt plugin) but wouldn't think that would matter. After decoding and ensuring the token is correct, then the logic is performed and the user session is created.
I did have to make some tweaks to my User model so that the $user was in the correct Authenticatable format but it isn't throwing any errors -- BUT no session is truly created. When I click on a link/route that is using an "auth" middleware, then it throws me back out to login again.... purpose of the URL token is to log in the user.
Here is my middleware class
<?php
namespace App\Http\Middleware;
use Closure;
use Event, JWTAuth;
use App\User;
use App\Tpperson;
use Illuminate\Support\Facades\Auth;
class checkJWT
{
public function handle($request, Closure $next, $validateAll = false)
{
if ($request->has('jwt') && !Auth::check()) {
$token = $request->jwt;
$jwtObj = JWTAuth::setToken($token)->getPayload();
$tpPID = 0;
if (is_numeric($jwtObj->get('sub')) && $jwtObj->get('sub') > 0) {
$tpPID = $jwtObj->get('sub');
}
if ($tpPID > 0) {
$user = User::find($tpUserExists);
Auth::guard('web')->setUser($user);
dump(auth()->check());
}
dump(Auth::check());
// fire off event to hit up Auth::setUser to be sure
Event::fire('tymon.jwt.valid', array($user));
}
return $next($request);
}
}
My route or web.php (laravel 5.3 route file) has this:
Route::group(['middleware' => ['web','auth.jwt']], function () {
Route::get('/features', array(
'as' => 'features',
function() {
return view('features')->with('teaserPadding', 60);
}
));
});
FYI, I have tried flipping around the order of the 'web' and 'auth.jwt' and only using 'auth.jwt' but that doesn't make a difference in the result --> session not saved.
I have tried the Auth::setUser several different ways too - with guard and without but neither truly logs the user in.
Auth::guard('web')->setUser($user);
Auth::setUser($user);
CRAZY thing is my Auth::check() all return true during that "page request" which seems to mimic Auth::once but I do NOT want a "stateless api".
Ways to ensure Auth::setUser() creates the user's session? Code missing - class or remember me token? Yes, I have tried on Laravel 5.2 and 5.3.
Thanks!
Jeremy