Laravel Socialite: How to change redirect_uri at runtime?
Asked Answered
S

2

6

Is is possible to change the value of redirect_uri set in config/services.php at runtime?

I tried doing like this:

return $socialite->driver('facebook')->redirectUrl(newUrl)->redirect();

but it throws an error saying that the redirect_uri should match the one from the OAuth dialog. Upon further checking of the error message, the value of redirect_uri is empty so apparently, the redirectUrl() method does not work.

PS:

This is the socialite version that I'm using defined in composer.json:

"laravel/socialite": "^2.0"

Sophistic answered 8/6, 2017 at 15:37 Comment(0)
O
5

The solution to this is:

public function redirectToProvider($accountType, $provider)
{
    return Socialite::driver($provider)
        ->with(['redirect_uri' => "YOUR_NEW_URL"])
        ->stateless()
        ->redirect();
}

With this method, you could override any of the values in the http request (including the scope).

Oosperm answered 16/2, 2018 at 15:34 Comment(2)
I did it and worked, the google redirected to me to new URL, but I got a new error "redirect_uri_mismatch", I think because this new URL is different from URL in config/env. Any idea? Another thing is the "stateless()" isn't necessary on this case, according to the initial question. "Stateless" is only necessary when we no want to use sessions, like API auth.Keenakeenan
This doesn't appear to work with Twitter provider... any other ideas/options?Millisent
N
1

I am using ^5.9 version of laravel/socialite, using redirectUrl() works as intended for me. I think the key thing is you have to use this method both when redirecting and when fetching the user, so the parameters are equal.

// Just redirecting
Socialite::driver('google')
            ->redirectUrl($redirectUrl)
            ->redirect();
// Fetching user
Socialite::driver('google')
                ->redirectUrl($redirectUrl)
                ->user();

Additionally for Google Oauth, if you just added redirect URL you might have to give it some time to take effect, that's what at least it says in the Google admin panel.

Naive answered 19/12, 2023 at 9:33 Comment(1)
This is the answer, you must use the redirect when redirecting and when fetching.Morphine

© 2022 - 2025 — McMap. All rights reserved.