Here you go https://laracasts.com/discuss/channels/laravel/session-values-lost-after-redirect?page=0
Calling $request->session()->save() before redirecting should fix it.
If using facade use the facade syntax.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hiya everyone, I'm trying to login users with Spotify using Socialite. I successfully redirect to login page and in return get the user object. Problem occurs when I want to pass the user object to my blade files and controllers. When I return handleProviderCallback with redirect()->route(), I lose all session data. I can persist the session if I return view() but when I do that and refresh the page I get "Invalid authorization code". How can I make sure session persists after reloads? What am I missing here? Thanks in advance!
Edit: Using Laravel 6
SESSION_DRIVER=file
SESSION_DOMAIN="http://localhost"
SESSION_ENCRYPT=false
<?php
namespace App\Http\Controllers\Auth;
use Socialite;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller {
use AuthenticatesUsers;
public function redirectToSpotify() {
$scopes = ['playlist-read-private', 'playlist-read-collaborative'];
return Socialite::driver('spotify')
->with(['show_dialog' => 'true'])
->scopes($scopes)
->redirect();
}
public function handleSpotifyCallback() {
$user = Socialite::driver('spotify')
->stateless()
->user();
// dd($user); Returns good data.
Session::put('user', $user);
Session::save();
// dd(Session::all()); Has the $user in it.
return redirect()->route('dashboard');
}
}
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Found out that the problem was
SESSION_DOMAIN="http://localhost"
Set it to null and session was persistent again. Thanks for the help anyway! :)
Please or to participate in this conversation.