/**
  * Update Profile
  *
  * @param Request $request
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function updateProfile(Request $request)
 {
     $user = Auth::user();
     $profile = Profile::where('user_id', $user->id)->first();
     $userUpdateCheck = 0;
     $profileUpdateCheck = 0;
     if (!empty($request['username']) && is_null(User::where('username', $request['username'])->first())) {
         $user->username = $request['username'];
         $userUpdateCheck++;
     }
     if (!empty($request['email']) && is_null(User::where('email', $request['email'])->first())) {
         $user->email = $request['email'];
         $userUpdateCheck++;
     }
     if (!empty($request['first_name'])) {
         $profile->first_name = $request['first_name'];
         $profileUpdateCheck++;
     }
     if (!empty($request['last_name'])) {
         $profile->last_name = $request['last_name'];
         $profileUpdateCheck++;
     }
     if ($userUpdateCheck !== 0) {
         $user->save();
     }
     if ($profileUpdateCheck !== 0) {
         $profile->save();
     }
     return redirect('dashboard');
 }
 /**
  * Create a new user authenticated through a social provider
  *
  * @param $field
  * @param $fieldValue
  * @return mixed
  */
 protected function createUser($userData, $provider)
 {
     $nameFromProvider = isset($userData->user['first_name']) ? $userData->user['first_name'] : $userData->nickname;
     $providerField = $this->selectProviderField($provider);
     $user = new User();
     $user->{$providerField} = $userData->id;
     $user->username = is_null(User::where('username', $nameFromProvider)->first()) ? $nameFromProvider : NULL;
     $user->email = !is_null($userData->email) && is_null(User::where('email', $userData->email)->first()) ? $userData->email : NULL;
     //$userData->email;
     $user->save();
     return User::where($providerField, $userData->id)->first();
 }
 /**
  * Handle a login request to the application.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function postLogin(Request $request)
 {
     $field = filter_var($request['email'], FILTER_VALIDATE_EMAIL) ? "email" : "username";
     $user = User::where($field, $request['email'])->first();
     if (!is_null($user)) {
         if ($user->password == md5($request['password'])) {
             $request->has('remember') ? Auth::login($user, true) : Auth::login($user);
             return redirect('dashboard');
         }
     }
     return redirect('login');
 }
 /**
  * Find or create a new instance of a social user.
  *
  * @param array $data
  * @return User
  */
 protected function findOrCreateSocialUser(array $data)
 {
     $user = User::where('social_id', $data['social_id'])->first();
     if (is_null($user)) {
         return User::create($data);
     }
     return $user;
 }