Laravel Socialite Twitter OAuth fails with “Session store not set on request” in stateless API

I’m working on integrating Twitter OAuth using Laravel Socialite in a stateless API for a Single Page Application (SPA). I’m encountering an error when trying to generate the authorization URL.

Problem:
When calling the redirect() method on the Twitter Socialite driver, I’m getting the following error:

Session store not set on request.

Here’s the relevant part of my controller:

public function getSocialRedirect(Request $request, string $provider)
{
    try {
        $url = Socialite::driver('twitter')
            ->stateless()
            ->scopes($scopes)
            ->with(['state' => $state])
            ->redirect()
            ->getTargetUrl();

        return response()->json([
            'url' => $url,
        ]);
    } catch (Exception $e) {
        throw $e;
    }
}

I also added this in my config, for use oauth2

'twitter' => [
        'enabled'       => env('TWITTER_ENABLED', false),
        'client_id'     => env('TWITTER_CLIENT_ID'),
        'client_secret' => env('TWITTER_CLIENT_SECRET'),
        'redirect'      => env('TWITTER_REDIRECT_URI'),
        'oauth'         => 2
    ]

I’ve used the stateless() method as suggested in the Socialite documentation for API usage.
The route is in routes/api.php

How can I generate a Twitter OAuth authorization URL using Laravel Socialite in a completely stateless manner, suitable for use in a SPA? Is there a way to bypass the session requirement altogether?
Any insights or alternative approaches would be greatly appreciated.

You’ll need to use the web middleware if you need session state, CSRF protection.

Your routes should go inside the web middleware as shown below:

Route::group(['middleware' => ['web']], function () {
    // your routes here
});

If the above solution doesn’t work then try adding this to $middleware into Kernel.php

protected $middleware = [
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
];

4

The Socialite package’s Twitter provider has a design assumption that doesn’t work well in an API context (like a Vue 3 frontend with a Laravel backend). That’s what i found :

In the AbstractProvider.php class, the redirect() method includes this code:

public function redirect()
{
    $state = null;
    if ($this->usesState()) {
        $this->request->session()->put('state', $state = $this->getState());
    }
    if ($this->usesPKCE()) {
        $this->request->session()->put('code_verifier', $this->getCodeVerifier());
    }
    return new RedirectResponse($this->getAuthUrl($state));
}

This method checks if PKCE (Proof Key for Code Exchange) is used via the usesPKCE() method:

protected function usesPKCE()
{
    return $this->usesPKCE;
}

In the TwitterProvide.php class, which extends AbstractProvider, the $usesPKCE property is always set to true

class TwitterProvider extends AbstractProvider implements ProviderInterface
{
    /**
     * The scopes being requested.
     *
     * @var array
     */
    protected $scopes = ['users.read', 'tweet.read'];

    /**
     * Indicates if PKCE should be used.
     *
     * @var bool
     */
    protected $usesPKCE = true;

    /**
     * The separating character for the requested scopes.
     *
     * @var string
     */
    protected $scopeSeparator = ' ';

    /**
     * The query encoding format.
     *
     * @var int
     */
    protected $encodingType = PHP_QUERY_RFC3986;

This means that even when you set the provider to stateless, it will still attempt to store the code verifier in the session. In an API context, where sessions aren’t typically used, this leads to a “session is not defined” error.

This is why I had to implement my own custom method to handle the Twitter provider in this stateless, API-based scenario.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật