【问题标题】:laravel socialite not working with JWT authlaravel 社交名流不使用 JWT 身份验证
【发布时间】:2020-06-10 07:39:02
【问题描述】:

我正在与社交名流和 jwt auth 合作,这是我的代码

class SocialAuthFacebookController extends Controller
{

  /**
   * Create a redirect method to facebook api.
   *
   * @return void
   */
    public function redirect()
    {  
        $provider = 'facebook';
        try {
            $socialite = Socialite::driver($provider)->redirect();
        } catch (InvalidStateException $e) {
            $socialite = Socialite::driver($provider)->stateless()->redirect();
        }

        return $socialite;
    }

    /**
     * Return a callback method from facebook api.
     *
     * @return callback URL from facebook
     */
    public function callback(SocialFacebookAccountService $service, Request $request)
    {
        if($request->has('code')) {
            $user = $service->createOrGetUser(Socialite::driver('facebook')->user());

            $token = JWTAuth::fromUser($user);

            return redirect("/#/dashboard")->with('token', $token);

        } else {
           return Socialite::driver($provider)->redirect();
        }
    }
}

我可以从我的 fb 登录回调中获取我的 $token 和 $user,但是当我将其重定向回仪表板时,我仍然无法登录。有什么想法吗?

【问题讨论】:

    标签: laravel laravel-socialite jwt-auth


    【解决方案1】:

    因为 JWT 是无状态的,而社交名流是有状态的。我猜你正在使用像 react 或类似的前端框架,你需要让你的前端框架使用社交名流。

    【讨论】:

      【解决方案2】:

      现在我正在前端应用它,并在https://github.com/websanova/vue-auth/blob/master/docs/Methods.md 中看到此代码 ($auth.oauth2),当我调用函数 social('facebook') 时它运行良好,发送 client_id 并将我重定向到回调和从 Facebook 获得“代码”响应。问题是当我被重定向回我的mounted()上的回调url时,当它检测到URL中有代码我再次发送请求并使用参数this.code然后我得到错误:

      客户端错误:POST graph.facebook.com/v3.0/oauth/access_token 导致 400 Bad Request 响应

      无法加载 URL:此 URL 的域不包含在应用程序的域中。为了能够加载这个

      我设置了“有效的 OAuth 重定向 URI”,但仍然遇到同样的错误

      <script>
          export default {
              data() {
                  return {
                      context: 'oauth2 context',
                      code: this.$route.params.code,
                      type: this.$route.params.type
                  };
              },
              mounted() {
                  var app = this
      
                  console.log(this.code)
                  console.log(this.type)
                  if (this.code) {
                      this.$auth.oauth2({
                          code: true,
                          provider: this.type,
                          params: {
                              client_id: xxxxxx,
                              client_secret: 'xxxxxxxx',
                              code: this.code,
                              redirect_uri: 'mysite.com/#/dashboard'
                          },
                          success: function(res) {
                              console.log('success ' + this.context);
                          },
                          error: function (res) {
      
                              console.log('error ' + this.context);
      
                              console.log(res);
                          },
      
                      });
                  }
              },
              methods: {
                  social(type) {
                      this.$auth.oauth2({
                          provider: type || this.type,
                          rememberMe: true,
                          params: {
                              // code: this.code,
                              client_id: xxxxxxxx,
                              client_secret: 'xxxxxxxx',
                              redirect_uri: 'mysite.com/api/redirect/social'
      
                          },
                          success: function(res) {
                              console.log('success ' + this.context);
                          },
                          error: function (res) {
                              console.log('error ' + this.context);
                          }
      
                      });
                  }
              }
          }
      </script>
      
      ```
      
      
      

      【讨论】:

        猜你喜欢
        • 2017-04-04
        • 2016-12-15
        • 2019-11-15
        • 1970-01-01
        • 2015-10-11
        • 2016-10-20
        • 2020-03-18
        • 2020-01-05
        • 2015-05-10
        相关资源
        最近更新 更多