Can't detect callback is originated from provider in Laravel Socialite

Viewed 1443

In Socialite documentation, it says the following line gets the user information:

$user = Socialite::driver('twitter')->user();

This naturally assumes that the callback were originated from Twitter. However, callback route is requested unintentionally in some cases, and the line above gives the error like that:

InvalidArgumentException in TwitterProvider.php line 15:
Invalid request. Missing OAuth verifier.

How can I detect the callback is originated from expected place (it is Twitter here) prior to executing the line above? My log file is full of these error messages. I think these errors come from search engine bots requesting callback route (Auth mechanism seems to work properly).

5 Answers

I think it should catch general errors.

    try {
        $user = Socialite::driver('twitter')->user();
    } catch (\Exception $e) {
        # do something if nothing works
    }
Related