public static function socialLogin($socialType, $socialData) { try { $socialUser = UserSocial::where(['type' => $socialType, 'uid' => $socialData->id])->firstOrFail(); $socialUser->meta = $socialData->user; $socialUser->save(); $user = $socialUser->user; $user->fill(['name' => $socialData->name, 'email' => $socialData->email, 'last_login' => \Carbon\Carbon::now()]); $user->save(); } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $exc) { try { $user = self::where('email', $socialData->email)->firstOrFail(); } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $exc) { $user = new static(['name' => $socialData->name, 'email' => $socialData->email, 'password' => \Illuminate\Support\Facades\Hash::make(str_random()), 'last_login' => \Carbon\Carbon::now()]); $user->save(); } $user->socialAccounts()->create(['type' => $socialType, 'uid' => $socialData->id, 'meta' => $socialData->user]); } \Illuminate\Support\Facades\Auth::login($user, true); return $user; }
public function login(Request $request, $provider = null) { $this->layout = 'user.login'; $this->metas['title'] = "Хэрэглэгч нэвтрэх хэсэг"; $this->view = $this->BuildLayout(); $user = $socialUser = ''; //https://laracasts.com/series/whats-new-in-laravel-5/episodes/9 switch ($provider) { case 'facebook': case 'twitter': case 'google': if ($request->has('code') || $request->has('oauth_token') || $request->has('state')) { $socialUser = Socialite::with($provider)->user(); } else { $socialite = Socialite::with($provider); if ($provider == 'facebook') { $socialite->scopes(['user_friends', 'public_profile', 'email']); } return $socialite->redirect(); } break; default: # code... break; } if ($socialUser) { $email = $socialUser->email; $name = $socialUser->name; $nameArray = explode(' ', $name); $firstname = reset($nameArray); $lastname = str_replace($firstname, '', trim($name)); $fb_id = $tw_id = $gp_id = $photo_url = $local_photo_url = ''; $avatarSavePath = public_path('images/avatar'); switch ($provider) { case 'facebook': $fb_id = $socialUser->id; $gender = $socialUser->user['gender']; $photo_url = $socialUser->avatar_original; break; case 'twitter': $tw_id = $socialUser->id; $photo_url = $socialUser->avatar_original; break; case 'google': $gp_id = $socialUser->id; $gender = $socialUser->user['gender']; $photo_url = $socialUser->avatar; break; default: break; } $userSocial = UserSocial::where('social', $provider)->where('socialname', $socialUser->id); if (!empty($photo_url)) { $photo_urlArray = parse_url($photo_url); unset($photo_urlArray['query']); $photo_url = $photo_urlArray['scheme'] . '://' . $photo_urlArray['host'] . $photo_urlArray['path']; } if ($userSocial->exists()) { $userId = $userSocial->get()->first()->user_id; $user = User::find($userId); } else { //if not existing then just register $user = User::create(['email' => $email, 'firstname' => $firstname, 'lastname' => $lastname, 'registered_with' => $provider, 'register_ip' => $request->getClientIp(), 'public' => 0, 'status' => 1, 'role' => 2]); if (!empty($photo_url)) { $thumbnail = Image::make($photo_url); $thumbnail->resize(80, 80); $thumbnail->save($avatarSavePath . '/thumbnail/' . $user->id . '.jpg'); $medium = Image::make($photo_url); $medium->resize(160, 160); $medium->save($avatarSavePath . '/medium/' . $user->id . '.jpg'); $large = Image::make($photo_url); $large->resize(360, 360); $large->save($avatarSavePath . '/large/' . $user->id . '.jpg'); //$ext = pathinfo($photo_url,PATHINFO_EXTENSION); $ext = 'jpg'; //file_put_contents($avatarSavePath.'/'.$user->id.'.'.$ext, fopen($photo_url, 'r')); $local_photo_url = $user->id . '.' . $ext; $user->avatar = $local_photo_url; $user->save(); } $usersocial = new UserSocial(); $usersocial->user_id = $user->id; $usersocial->social = $provider; $usersocial->socialname = $socialUser->id; $user->usersocial()->save($usersocial); $this->sendThankYouEmail($user); } Auth::login($user, true); } //if already logged in if (Auth::check() || Auth::viaRemember()) { return redirect('/user/profile'); } return $this->view; }