Search code examples
laravellaravel-socialite

Client Error 500 in socialite google login laravel 5.8


I have a laravel project that has been integrated with Facebook login, but if I use Google login, the page raises a client error that is 500 server errors

You can check on this website https://indhri.asynchsolution.com and login using google, don't worry, I will delete the database immediately if this problem is resolved

this is my SocialController.php

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role;
use App\SocialAccount;
use App\User;

class SocialiteController extends Controller
{
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    public function handleProviderCallback($provider)
    {
        //$user = Socialite::driver($provider)->user();
         try {
            $user = Socialite::driver($provider)->user();
        } catch (Exception $e) {
            return redirect('/auth/login');
        }
        //dd($user);
        $authUser = $this->findOrCreateUser($user, $provider);

        Auth::login($authUser, true);
        return redirect('/personal');
    }

    public function findOrCreateUser($socialUser, $provider)
    {
        $socialAccount = SocialAccount::where('provider_id', $socialUser->getId())
                        ->where('provider_name', $provider)
                        ->first();

        if($socialAccount) {
            return $socialAccount->user;
        } else {
            $user = User::where('email', $socialUser->getEmail())->first();

            if(! $user) {
                $user = User::create([
                    'username' => $socialUser->getName(),
                    'email' => $socialUser->getEmail()

                ]);
                $user->assignRole('Registered');
            }

            $user->socialAccounts()->create([
                'provider_id' => $socialUser->getId(),
                'provider_name' => $provider
            ]);

            return $user;
        }
    }
}

This is the client error

this is error on Inspect


Solution

  • Add a SESSION_DOMAIN in .env file

    SESSION_DOMAIN=indhri.asynchsolution.com
    

    or change config/session.php from null to indhri.asynchsolution.com

    'domain' => env('SESSION_DOMAIN', null),
    

    Clear the cache

    php artisan config:clear
    php artisan cache:clear
    

    Reload auto-discovered packages

    composer dump-autoload
    

    And try it on a private window because old session cookies can cause an issue

    Hope this helps