简体   繁体   English

用户取消应用请求后,Laravel 5 Socialite Facebook 登录错误处理回调

[英]Laravel 5 Socialite Facebook Login Error handling on callback after user cancels app request

I have a Laravel 5 web app with Socialite to login my user by using Facebook account.我有一个带有 Socialite 的 Laravel 5 网络应用程序,可以使用 Facebook 帐户登录我的用户。

This is my callback function:这是我的回调函数:

public function callback(SocialAccountService $service)
{

    $user = $service->createOrGetUser(Socialite::driver('facebook')->user());

    auth()->login($user);

    return redirect()->to('/home');
}

This is my SocialAccountService, basically the function returns the user if existing or creates a new one:这是我的 SocialAccountService,基本上该函数返回用户(如果存在或创建一个新用户):

class SocialAccountService
{
    public function createOrGetUser(ProviderUser $providerUser)
    {
        $account = SocialAccount::whereProvider('facebook')
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {
            return $account->user;
        } else {

            $account = new SocialAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => 'facebook'
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();

            if (!$user) {

                $user = User::create([
                    'email' => $providerUser->getEmail(),
                    'name' => $providerUser->getName(),
                ]);
            }

            $account->user()->associate($user);
            $account->save();

            return $user;

        }

    }
}

Now the problem is, I am able to make my user login successfully via Facebook, but when the user clicks Cancel on the FB dialog for permission, it breaks.现在的问题是,我能够让我的用户通过 Facebook 成功登录,但是当用户在 FB 对话框上单击取消以获得许可时,它会中断。

Facebook 应用程序权限拒绝的回调错误

How can I handle this error?我该如何处理这个错误? I can see the error message in URL box, but don't know to handle them.我可以在 URL 框中看到错误消息,但不知道如何处理它们。

PS I am fairly new to Laravel and Socialite PS 我对 Laravel 和 Socialite 还很陌生

in the callback(...) method you can check for presense of the 'code' input field and if it is not there redirect to back to login page with errors.callback(...)方法中,您可以检查“代码”输入字段是否存在,如果不存在,则重定向到有错误的登录页面。

Example:示例:

function callback(SocialAccountService $service ,Request $request) {
    if (! $request->input('code')) {
        return redirect('login')->withErrors('Login failed: '.$request->input('error').' - '.$request->input('error_reason'));
    }

    // rest of your code
}    

Try catch is the best practice(it catches all exception) Try catch 是最佳实践(它捕获所有异常)

 try
{
    $userSocial =Socialite::driver('facebook')
    ->stateless()->user();
    dd($userSocial);
 }
catch (\Throwable $e) { 

//handle error here for php 7 or 8  

  } catch (\Exception $e) { 
  // for php 5
  handle error here 

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM