mardi 3 avril 2018

Laravel 5 Login with google returning null

i am very confused why i am returning null with my code when i publish it in web.

Here is what i've returned null from this line:

public function callback(SocialGoogleAccountService $service)
    {        
        $user = $service->createOrGetUser(Socialite::driver('google')->stateless()->user());        
        dd($user);
        auth()->login($user);
        return redirect()->to('/home');
    }

i followed this from a tutorial, now here is the full code of social login from google.

<?php
namespace App\Services;
use App\SocialFacebookAccount;
use App\User;
use Laravel\Socialite\Contracts\User as ProviderUser;

class SocialGoogleAccountService
{
    public function createOrGetUser(ProviderUser $providerUser)
    {
        $account = SocialFacebookAccount::whereProvider('google')
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {           
            return $account->user;

        } else {

            $account = new SocialFacebookAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => 'google'
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();
            if (!$user) {

                $user = User::create([
                    'email' => $providerUser->getEmail(),
                    'name' => $providerUser->getName(),
                    'password' => md5(rand(1,10000)),
                    'status' => 1,
                ]);
            }
            $account->user()->associate($user);
            $account->save();
            return $user;
        }
    }
}

the above code is where the null came from, now here is the full code of the SocialAuthGoogleController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;
use App\Services\SocialGoogleAccountService;

class SocialAuthGoogleController extends Controller
{
    /**
   * Create a redirect method to google api.
   *
   * @return void
   */
    public function redirect()
    {
        return Socialite::driver('google')->redirect();
    }

    /**
     * Return a callback method from google api.
     *
     * @return callback URL from google
     */
    public function callback(SocialGoogleAccountService $service)
    {        
        $user = $service->createOrGetUser(Socialite::driver('google')->stateless()->user());        
        auth()->login($user);
        return redirect()->to('/home');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire