App\Repositories\Frontend\Access\User\UserRepository::findOrCreateSocial PHP Method

findOrCreateSocial() public method

public findOrCreateSocial ( $data, $provider ) : UserRepository | boolean
$data
$provider
return UserRepository | boolean
    public function findOrCreateSocial($data, $provider)
    {
        /**
         * User email may not provided.
         */
        $user_email = $data->email ?: "{$data->id}@{$provider}.com";
        /**
         * Check to see if there is a user with this email first
         */
        $user = $this->findByEmail($user_email);
        /**
         * If the user does not exist create them
         * The true flag indicate that it is a social account
         * Which triggers the script to use some default values in the create method
         */
        if (!$user) {
            $user = $this->create(['name' => $data->name, 'email' => $user_email], true);
        }
        /**
         * See if the user has logged in with this social account before
         */
        if (!$user->hasProvider($provider)) {
            /**
             * Gather the provider data for saving and associate it with the user
             */
            $user->providers()->save(new SocialLogin(['provider' => $provider, 'provider_id' => $data->id, 'token' => $data->token, 'avatar' => $data->avatar]));
        } else {
            /**
             * Update the users information, token and avatar can be updated.
             */
            $user->providers()->update(['token' => $data->token, 'avatar' => $data->avatar]);
        }
        /**
         * Return the user object
         */
        return $user;
    }

Usage Example

 /**
  * @param Request $request
  * @param $provider
  * @return \Illuminate\Http\RedirectResponse|mixed
  * @throws GeneralException
  */
 public function login(Request $request, $provider)
 {
     //If the provider is not an acceptable third party than kick back
     if (!in_array($provider, $this->helper->getAcceptedProviders())) {
         return redirect()->route('frontend.index')->withFlashDanger(trans('auth.socialite.unacceptable', ['provider' => $provider]));
     }
     /**
      * The first time this is hit, request is empty
      * It's redirected to the provider and then back here, where request is populated
      * So it then continues creating the user
      */
     if (!$request->all()) {
         return $this->getAuthorizationFirst($provider);
     }
     /**
      * Create the user if this is a new social account or find the one that is already there
      */
     $user = $this->user->findOrCreateSocial($this->getSocialUser($provider), $provider);
     /**
      * User has been successfully created or already exists
      * Log the user in
      */
     auth()->login($user, true);
     /**
      * User authenticated, check to see if they are active.
      */
     if (!access()->user()->isActive()) {
         access()->logout();
         throw new GeneralException(trans('exceptions.frontend.auth.deactivated'));
     }
     /**
      * Throw an event in case you want to do anything when the user logs in
      */
     event(new UserLoggedIn($user));
     /**
      * Set session variable so we know which provider user is logged in as, if ever needed
      */
     session([config('access.socialite_session_name') => $provider]);
     /**
      * Return to the intended url or default to the class property
      */
     return redirect()->intended(route('frontend.index'));
 }