laravel社交名流不与JWT auth合作

laravel社交名流不与JWT auth合作,laravel,laravel-socialite,jwt-auth,Laravel,Laravel Socialite,Jwt Auth,我与社交名媛和jwt auth合作这是我的代码 class SocialAuthFacebookController extends Controller { /** * Create a redirect method to facebook api. * * @return void */ public function redirect() { $provider = 'facebook'; try {

我与社交名媛和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,但当我将其重定向回仪表板时,我仍然无法登录。有什么想法吗?

因为JWT是无状态的,社交名流是有状态的。我猜你正在使用一个前台框架,比如react或类似的,你需要让socialite被你的前台框架使用。

现在我正在努力在前台应用它,在我调用函数social('facebook')时看到了这段代码($auth.oauth2),它工作得很好,发送客户端id并将我重定向到回调,然后从Facebook获得“代码”响应。问题是,当我在my mounted()上被重定向回我的回调url时,当它检测到url中有代码时,我再次发送请求,并使用参数this.code,然后我得到错误:

客户端错误:
POST-graph.facebook.com/v3.0/oauth/access\u-token
导致
400错误请求
响应

无法加载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>

```



导出默认值{
数据(){
返回{
上下文:“oauth2上下文”,
代码:此.$route.params.code,
类型:此.$route.params.type
};
},
安装的(){
var app=这个
console.log(this.code)
console.log(this.type)
如果(此代码){
这是$auth.oauth2({
代码:对,
提供程序:此.type,
参数:{
客户识别号:xxxxxx,
客户机密:“xxxxxxxx”,
代码:this.code,
重定向uri:'mysite.com/#/dashboard'
},
成功:功能(res){
console.log('success'+this.context);
},
错误:函数(res){
console.log('error'+this.context);
控制台日志(res);
},
});
}
},
方法:{
社会(类型){
这是$auth.oauth2({
提供者:type | | this.type,
记住:没错,
参数:{
//代码:this.code,
客户识别号:xxxxxxxx,
客户机密:“xxxxxxxx”,
重定向uri:'mysite.com/api/redirect/social'
},
成功:功能(res){
console.log('success'+this.context);
},
错误:函数(res){
console.log('error'+this.context);
}
});
}
}
}
```