auth_service = $auth_service; $this->user_service = $user_service; $this->middleware(function ($request, $next) use($login_strategy_factory){ // we do it here just to ensure that user session is loaded Log::debug(sprintf("SocialLoginController::middleware")); $this->login_strategy = $login_strategy_factory->build(); return $next($request); }); } /** * @param $provider * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Symfony\Component\HttpFoundation\RedirectResponse */ public function redirect($provider) { try { Log::debug(sprintf("SocialLoginController::redirect provider %s", $provider)); if(!SocialLoginProviders::isSupportedProvider($provider)) throw new ValidationException(sprintf("Provider %s is not supported.", $provider)); return Socialite::driver($provider)->redirect(); } catch (\Exception $ex){ Log::error($ex); } return view("auth.register_error"); } /** * @param $provider * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|mixed */ public function callback($provider) { try { Log::debug(sprintf("SocialLoginController::callback provider %s", $provider)); // validate provider if(!SocialLoginProviders::isSupportedProvider($provider)) throw new ValidationException(sprintf("Provider %s is not supported.", $provider)); $social_user = Socialite::driver($provider)->user(); // try to get user by primary email from our db Log::debug(sprintf("SocialLoginController::callback provider %s trying to get user using email %s", $provider, $social_user->getEmail())); $user = $this->auth_service->getUserByUsername($social_user->getEmail()); if (is_null($user)) { Log::debug(sprintf("SocialLoginController::callback provider %s user does not exists for email %s, creating ...", $provider, $social_user->getEmail())); // if does not exists , registered it with email verified and active $user = $this->user_service->registerUser([ 'email' => $social_user->getEmail(), 'full_name' => $social_user->getName(), 'external_pic' => $social_user->getAvatar(), 'external_id' => $social_user->getId(), 'email_verified' => true, 'active' => true, 'external_provider' => $provider ]); } // do login Auth::login($user, true); // and continue the usual flow return $this->login_strategy->postLogin([ 'provider'=> $provider ]); } catch (\Exception $ex){ Log::error($ex); } return view("auth.register_error"); } }