app/Http/Controllers/Auth/SocialAccountController.php
/**
     * Obtain the user information
     *
     * @return Response
     */
Obtain the user information
@return Response
Obtain the user information @return Response
public function handleProviderCallback($socialProvider)
    {
        try {
            $socialUser = Socialite::driver($socialProvider)->user();
        } catch (\Exception $e) {
            logger("Social Login Fallito: " . $e);
            return redirect()->route('login');
        }
        $account = LinkedSocialAccount::whereSocialProviderName($socialProvider)->whereSocialProviderId($socialUser->getId())->first(); 
        if ($account) {
            $user = $account->user; 
        } else {
            $user = User::whereEmail($socialUser->getEmail())->first(); 
            $avatar = ($socialUser->avatar_original) ?? $socialUser->avatar;
            if(!$user){
                $user = User::create([
                    'email' => $socialUser->getEmail(),
                    'name'  => $socialUser->getName(),
                    'avatar' => $avatar
                ]);
            }
            if(!$user->custom_avatar && ($user->accounts()->count() == 0)){
                $user->update(['avatar' => $avatar]);
            }
            $user->accounts()->create([ 
                'social_provider_id'   => $socialUser->getId(),
                'social_provider_name' => $socialProvider,
            ]);
        }
        auth()->login($user, true); 
        return redirect()->route('dashboard');
    }
public function handleProviderCallback ( $ socialProvider ) { try { $ socialUser = Socialite :: driver ( $ socialProvider ) -> user ( ) ; } catch ( \ Exception $ e ) { logger ( " Social Login Fallito:  " . $ e ) ; return redirect ( ) -> route ( 'login' ) ; } $ account = LinkedSocialAccount :: whereSocialProviderName ( $ socialProvider ) -> whereSocialProviderId ( $ socialUser -> getId ( ) ) -> first ( ) ; if ( $ account ) { $ user = $ account -> user ; } else { $ user = User :: whereEmail ( $ socialUser -> getEmail ( ) ) -> first ( ) ; $ avatar = ( $ socialUser -> avatar_original ) ?? $ socialUser -> avatar ; if ( ! $ user ) { $ user = User :: create ( [ 'email' => $ socialUser -> getEmail ( ) , 'name' => $ socialUser -> getName ( ) , 'avatar' => $ avatar ] ) ; } if ( ! $ user -> custom_avatar && ( $ user -> accounts ( ) -> count ( ) == 0 ) ) { $ user -> update ( [ 'avatar' => $ avatar ] ) ; } $ user -> accounts ( ) -> create ( [ 'social_provider_id' => $ socialUser -> getId ( ) , 'social_provider_name' => $ socialProvider , ] ) ; } auth ( ) -> login ( $ user , true ) ; return redirect ( ) -> route ( 'dashboard' ) ; }
Obtain the user information
 @return Response
Obtain the user information @return Response
// interrogo il social network e mi faccio tornare le informazioni che immagazzino in $user // cerco se le info tornare appartengono ad un utente // se trovo corrispondenza torna l'utente // altrimenti cerco un utente con l'email tornata dal social network // controllo se è la prima volta che mi associo un account social e se non ho settato un avatar custom // associo il social account all'utente appena creato o esistente // loggo l'utente