0

I am currently adding Spotify Authentification to just getting the Spotify Token to have acces to different features (User Features) and for example adding to a user connected some tracks to there playlist.

Also I made the Authentification system and when I receive the User Info in my callback endpoint, Laravel (Socialite) store him in a Session file, also I can't get it from different controller like to return all genres from spotify or other things ..

My api routes (Works perfectly)

Route::prefix('/spotify')->group(function () {
    Route::get('auth/redirect', function () {
        return Socialite::driver('spotify')
            ->setScopes(SpotifyController::READ_ACCES)
            ->redirect();
    });

    Route::get('auth/callback', function () {
        return response()->json(['message' => "Utilisateur connecté à spotify"]);
    });
});

When I try to print user() directly into the callback, it works perfectly.

Therefore when I want to get the user token from session (registered by Laravel Socialite) in another api endpoints (For example /api/genres)

Route::prefix('genres')->group(function() {
    Route::get('', [GenreController::class, 'index'])->name('genres.index');
});
class GenreController extends Controller
{
    protected function index(Request $request)
    {
        dd(Socialite::driver('spotify')->user());
    }
}

It return Invalid Exception state .. No session found ..

invalid exception state

Any idea on how to correct this ? Does I need to store the Spotify User access token into database for each of my Connected User directly ?

My session was correctly configured

        'web' => [
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
            EnsureFrontendRequestsAreStateful::class,
            'throttle:60,1',
            \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

I want to have acces to the Spotify User access token from different API endpoints, obviously I cannot get him from Session file

Thanks for your help

3
  • Yes, you need to store the access token somewhere - session, database, etc. - for reuse. You can't call Socialite::driver('spotify')->user() again. Commented Jul 7, 2023 at 15:42
  • @ceejayoz I can store directly using session ? I only use laravel as an api, I will communicate with it from a Flutter mobile App ? Commented Jul 7, 2023 at 19:27
  • That depends on your use case. Sessions go away. You might store the token in your Flutter app, or in the API's database. Commented Jul 7, 2023 at 21:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.