Search code examples
phptwitter-oauthproviderlaravel-socialitelaravel-8

Twitter Provider, doesn't redirect me into the Callback


Facebook, LinkedIn, Google, GitHub, GitLab, Bitbucket and many others support OAuth2 for user-based authentication. but Twitter still using Oauth1 only .

if ($provider === "twitter") {
   return Socialite::driver($provider)
    ->userFromTokenAndSecret(
     env("TWITTER_ACCESS_TOKEN"),
     env("TWITTER_ACCESS_TOKEN_SECRET")
    )
    ->redirect();

Running into this issue :

Symfony\Component\Debug\Exception\FatalThrowableError
Call to undefined method Laravel\Socialite\One\User::redirect()

I want to be redirected into the callback :

https://example.com/api/login/twitter/callback

Any Help !!


Solution

  • You can use Socialite::with($provider)->redirect(); to redirect. But you have to first store the callback URL in your config/services.php file with your ids.

    'twitter' => [
            'client_id' => env('TWITTER_CLIENT_ID'),
            'client_secret' => env('TWITTER_CLIENT_SECRET'),Client Secret
            'redirect' => env('TWITTER_CALLBACK_URL'),
        ],
    

    Then inside your callback function Socialite::driver($provider)->user(); to get the user.