如何验证某人是否是Facebook用户

时间:2018-06-09 01:20:47

标签: laravel facebook

我正在构建一个使用管理员用户注册审批系统的laravel应用程序。管理员批准注册后,我希望用户在其个人资料页面上验证他们是facebook和linkedin的成员。我看到一堆关于用户创建和登录的内容,但不是关于验证。有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

如果您熟悉Laravel Socialite,可以实现这一目标......

用户登录并获得管理员批准后,您可以添加Facebook按钮以使用Socialite登录功能,但需要进行一些自定义工作。

在SocialAuthController.php中

找到handleFacebookCallback方法

在我的例子中,我有一个名为handleProvider的方法,但它的工作方式相同

public function handleProviderCallback($provider)
{

    // First we are going to get the user data from facebook
    $social_user = Socialite::driver($provider)->fields([
        'name', 'email'
    ])->user();

  //Now we make the logic
    if (Auth::check()) {  //if the user is logged in
        $userID = Auth::user()->id; //then we get the user ID

        $user = User::where('id', $userID)->first(); //Prepare the model for update

    //And finally we just start to update each field in DB (you need to create this fields)
        $user->fb_email = $social_user->email;
        $user->avatar = $social_user->avatar;
        $user->fb_id = $social_user->id;
        $user->save();

        return $this->authAndRedirect($user); // This is to redirect the user 

    }
}

有了这个,你可以让用户在你的应用程序中用Facebook链接他们的帐户......

但请记住,如果您要允许用户在Facebook注册,您还需要处理else,其余代码用于创建新帐户。否则,您只需在用户的个人资料页面中添加Facebook按钮即可与其各自的fb帐户相关联。

按钮可以是这样的:

<a href="{{ route('social.auth', 'facebook') }}" class="btn btn-social btn-just-icon btn-link"><i class="fa fa-facebook-square"></i></a>

你也需要首先将社交名称配置为熟悉这一点。