Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

christiangerdes's avatar

502 Bad Gateway on Socialite??

Hi,

I'm trying to implement Socialite but I keep receiving a 502 bad gateway error. Does anyone of you know how to fix this?

Let me know if you need to see some code.

Thanks!

0 likes
3 replies
Dry7's avatar

Make a screenshot of the page with an error and show the code you wrote

christiangerdes's avatar

This is the error. Not very helpfull: http://imgur.com/TD84G0W

This is the controller. The redirect method redirects to the social provider for the user the accept the login. The callback method handels the callback where is user is created etc.

class SocialLoginController extends Controller
{
    protected $social;

    public function __construct(SocialAuthentication $social)
    {
        $this->social = $social;
    }

    public function redirect($service)
    {
        return $this->social->redirectToSocialProvider($service);
    }

    public function callback($service)
    {
        $user = $this->social->registerUsingService($service);

        auth()->login($user, true);

        event(new UserLoggedIn($user, session()->pull('item_tokens')));

        return redirect()->route('users.show');
    }
}

This is the logic:

class SocialAuthentication
{
    protected $parser;

    public function __construct(Parser $parser)
    {
        $this->parser = $parser;
    }

    public function redirectToSocialProvider($service)
    {
        return Socialite::driver($service)->redirect();
    }

    public function registerUsingService($service)
    {
        $serviceUser = Socialite::driver($service)->user();

        $user = $this->getExistingUser($serviceUser, $service);

        if ( ! $user) {
            $user = $this->createUser($serviceUser);
        }

        if ($this->needsToCreateSocial($user, $service)) {
            $this->createSocialLink($user, $service, $serviceUser);
        }

        return $user;
    }

    protected function createUser($serviceUser)
    {
        return User::create([
            'email'         => $serviceUser->getEmail(),
            'first_name'    => $this->parser->parse($serviceUser->getName())->getFirstName(),
            'last_name'     => $this->parser->parse($serviceUser->getName())->getLastName(),
            'confirmed'     => true
        ]);
    }

    protected function createSocialLink($user, $service, $serviceUser)
    {
        $user->social()->create([
            'social_id' => $serviceUser->getId(),
            'service' => $service,
        ]);
    }

    protected function needsToCreateSocial(User $user, $service)
    {
        return ! $user->hasSocialLinked($service);
    }

    protected function getExistingUser($serviceUser, $service)
    {
        return User::where('email', $serviceUser->getEmail())->orWhereHas('social', function ($q) use ($serviceUser, $service) {
            $q->where('social_id', $serviceUser->getId())->where('service', $service);
        })->first();
    }
}

web.php

    Route::get('login/{service}', ['as' => 'social_login', 'uses' => 'SocialLoginController@redirect']);
    Route::get('login/{service}/callback', 'SocialLoginController@callback');
christiangerdes's avatar
Level 16

I found the error. It was caused by the upstream which was to big. I had to change my configuration by adding the following to my nginx site file.

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
5 likes

Please or to participate in this conversation.