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

Lordbedwetter's avatar

LinkedIn Socialite Works on localhost, but not in product

In my local setup, I have no issues logging in with LinkedIn using Socialite, however, when I pushed it to production, I get a 500 server error after the LinkedIn login screen appears and I attempt to authenticate.

I have changed the domain in my session. php file:

'domain' => env('SESSION_DOMAIN', ".rockymountainweb.design"),

This is my Controller:

<?php

namespace App\Http\Controllers;

use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class LoginWithLinkedInController extends Controller
{
     /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';
 
    /**
     * Redirect the user to the Linkedin authentication page.
     *
     * @return Response
     */
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->scopes(['r_liteprofile', 'r_emailaddress'])->redirect();
    }
   
    /**
     * Obtain the user information from Linkedin.
     *
     * @return Response
     */
    public function handleProviderCallback($provider)
    {
        $user = Socialite::driver($provider)->user();
        $authUser = $this->findOrCreateUser($user, $provider);
        Auth::login($authUser, true);
        return redirect($this->redirectTo);
    }
   
    /**
     * If a user has registered before using social auth, return the user
     * else, create a new user object.
     * @param  $user Socialite user object
     * @param $provider Social auth provider
     * @return  User
     */
    public function findOrCreateUser($user, $provider)
    {
        $authUser = User::where('provider_id', $user->id)->first();
        if ($authUser) {
            return $authUser;
        }
        return User::create([
            'name'     => $user->name,
            'email'    => $user->email,
            'provider' => $provider,
            'provider_id' => $user->id
        ]);
    }
}

Routes:

Route::get('login/{provider}', [LoginWithLinkedInController::class, 'redirectToProvider']);
Route::get('{provider}/callback', [LoginWithLinkedInController::class, 'handleProviderCallback']);

My services.php file:


    'linkedin' => [
            'client_id' => env('LINKEDIN_CLIENT_ID'),
            'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
            'redirect' => env('LINKEDIN_CALLBACK_URL'),
        ],

From the view:

    <div class="inline-flex justify-center">
                    <p class="w-1/2 my-auto text-lg font-medium text-gray-700">
                        Sign in with:
                      </p>
                  <a href="{{ url('/login/linkedin') }}" class="inline-flex justify-center w-full px-4 py-2 text-sm font-medium text-gray-500 bg-white border border-gray-300 rounded-md shadow-sm hover:bg-gray-50">
                    <span class="sr-only">Sign in with LinkIn</span>
                    <svg xmlns="http://www.w3.org/2000/svg" class="w-10 h-10" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 32 32" xml:space="preserve"><g id="OUTLINE_copy"><path fill="#414752" d="M23.247 21.927v-5.36c0-2.872-1.533-4.208-3.577-4.208-1.649 0-2.388.907-2.8 1.544v-1.324h-3.107c.041.877 0 9.348 0 9.348h3.107v-5.221c0-.279.02-.558.103-.757.224-.558.735-1.136 1.593-1.136 1.125 0 1.574.857 1.574 2.113v5.001h3.107zM10.49 11.303c1.083 0 1.758-.718 1.758-1.616-.02-.917-.675-1.614-1.738-1.614s-1.757.697-1.757 1.614c0 .897.675 1.616 1.717 1.616h.02zm1.553 10.624v-9.348H8.937v9.348h3.106z"/><path fill="#414752" d="M16 1c8.271 0 15 6.729 15 15s-6.729 15-15 15S1 24.271 1 16 7.729 1 16 1m0-1C7.163 0 0 7.163 0 16s7.164 16 16 16 16-7.164 16-16S24.836 0 16 0z" id="Shopping_10_116_"/></g><metadata><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:dc="http://purl.org/dc/elements/1.1/"><rdf:Description about="https://iconscout.com/legal#licenses" dc:title="linkedin,social,media,online" dc:description="linkedin,social,media,online" dc:publisher="Iconscout" dc:date="2017-09-15" dc:format="image/svg+xml" dc:language="en"><dc:creator><rdf:Bag><rdf:li>Epicflaticon by Alfredo</rdf:li></rdf:Bag></dc:creator></rdf:Description></rdf:RDF></metadata></svg>
                  </a>
                </div>

Directly from LinkedIn: Authorized redirect URLs for your app https://rockymountainweb.design/linkedin/callback

Hopefully, someone can give me an idea of where I may be going wrong. As I said, it works perfectly in localhost and I have been unable to figure this out for several days and I'm out of ideas.

Regards,

Edward

0 likes
6 replies
Wakanda's avatar

@lordbedwetter check-in your laravel log to see the error associated with 500. It may be because you changed your domain and you may need to change it from LinkedIn as well

Lordbedwetter's avatar

I have the same domain on LinkedIn. I made sure to make them the same before testing.

martinbean's avatar

@lordbedwetter Can’t really offer any advice without seeing an error message, so if you’re getting a 500 error you’ll need to check your Laravel error log on the server to see what the exception was.

Lordbedwetter's avatar

According to my error log on Forge, the error occurs:

[2021-01-26 04:09:18] production.ERROR:  {"exception":"[object] (Laravel\Socialite\Two\InvalidStateException(code: 0):  at /home/forge/rockymountainweb.design/vendor/laravel/socialite/src/Two/AbstractProvider.php:220)
yoo's avatar

Have you found the solution to this? I'm having the same issue

Lordbedwetter's avatar

@yoo I have updated my website and have not tried to implement Socialite into my application. Can you provide the error message in your logs and/or your repo so we can see your code?

Please or to participate in this conversation.