Laravel社交名媛谷歌登录只有一个域名

Laravel社交名媛谷歌登录只有一个域名,laravel,google-authentication,laravel-socialite,Laravel,Google Authentication,Laravel Socialite,我有一个谷歌+登录我的应用程序与拉威尔社交名媛。登录完成后,我有一个回调来连接用户(如果需要,我会在数据库中创建她) 但我只想限制与该公司的连接(如电子邮件“example@company.com,因此只有带有“company.com”的电子邮件) 我能和拉威尔社交名流一起做吗?我可以在我的回调中手动进行验证,但如果社交名流可以这样做,那就更好了 多谢各位 我的回拨: public function handleProviderCallback($provider){ $user = Soc

我有一个谷歌+登录我的应用程序与拉威尔社交名媛。登录完成后,我有一个回调来连接用户(如果需要,我会在数据库中创建她)

但我只想限制与该公司的连接(如电子邮件“example@company.com,因此只有带有“company.com”的电子邮件)

我能和拉威尔社交名流一起做吗?我可以在我的回调中手动进行验证,但如果社交名流可以这样做,那就更好了

多谢各位

我的回拨:

public function handleProviderCallback($provider){
  $user = Socialite::driver($provider)->user();

  if ($user) {
    $local_user = User::whereEmail($user->getEmail())->first();
    // If we don't have a user create a new user
    if (!$local_user) {
      $fragment = explode(' ', $user->getName());
      $local_user = User::create([
        'first_name' => isset($fragment[0]) ? $fragment[0] : '',
        'last_name' => isset($fragment[1]) ? $fragment[1] : '',
        'email' => $user->getEmail(),
        'last_seen' => Carbon::now(),
        'password' => ''
      ]);
      $local_user->roles()->attach(Role::whereName('User')->first());
    }
    auth()->login($local_user);
  }
  return redirect($this->redirectTo);
}

您有一个关于域限制的分步指南:[

在控制器中,您需要指定以下操作:

public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('google')->user();
    } catch (\Exception $e) {
        return redirect('/login');
    }
    // only allow people with @company.com to login
    if(explode("@", $user->email)[1] !== 'company.com'){
        return redirect()->to('/');
    }
    // check if they're an existing user
    $existingUser = User::where('email', $user->email)->first();
    if($existingUser){
        // log them in
        auth()->login($existingUser, true);
    } else {
        // create a new user
        $newUser                  = new User;
        $newUser->name            = $user->name;
        $newUser->email           = $user->email;
        $newUser->google_id       = $user->id;
        $newUser->avatar          = $user->avatar;
        $newUser->avatar_original = $user->avatar_original;
        $newUser->save();
        auth()->login($newUser, true);
    }
    return redirect()->to('/home');
}
如果这对你有帮助,请告诉我


尊敬!

上面的链接已断开,以下是正确的链接: