jeudi 15 février 2018

How login with Facebook and Google without authentication in Laravel 5.1 using socialite version 2.1?

'facebook' => ['client_id' => 'id',
'client_secret' => 'fgdfgsdfgrtt45453',
'redirect' => 'http://example.com/callback', ],

In route.php

Route::get('/redirect', 'SocialAuthFacebookController@redirect'); Route::get('/callback', 'SocialAuthFacebookController@callback');

And I have add Services/SocialFacebookAccountService.php directory in App. SocialFacebookAccountService.php

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

class SocialFacebookAccountService
{
    public function createOrGetUser(ProviderUser $providerUser)
    {
        $account = SocialFacebookAccount::whereProvider('facebook')
            ->whereProviderUserId($providerUser->getId())
            ->first();
        echo 'Account info : ';

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

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

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

            if (!$user) {

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

            $account->user()->associate($user);
            $account->save();

            return $user;
        }
    }
}

Please help , How to get user info in callback.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire