Laravel Socialite 在 Postman 中工作,但在 SPA 中不起作用

问题描述 投票:0回答:2

我处于一种奇怪的情况,当我在邮递员中测试对 /auth/social/facebook 的请求时,我成功重定向到 facebook 页面,但是当从我的水疗中心向同一路线发出请求时,我收到 CORS 相关错误:

跨源请求被阻止:同源策略不允许读取 远程资源位于 https://www.facebook.com/v3.3/dialog/oauth?client_id=240805606930310&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fapi%2Fauth%2Fsocial%2Ffacebook%2Fcallback&scope=email&response_type=code。 (原因:CORS 标头“Access-Control-Allow-Origin”丢失)。地位 代码:400。

在我的代码中我有:

路线:

Route::get('/auth/social/{provider}', [AuthController::class, 'socialRedirect']);

控制器:

public function socialRedirect($provider){
        return Socialite::driver($provider)->stateless()->redirect();
    }

并在 CORS 配置中:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];
laravel http-status-code-400 laravel-socialite
2个回答
6
投票

我不确定您是否使用 InertiaJS,但如果您将其用于 laravel 应用程序上的 SPA 连接器,并对标头信息进行修改以允许来源或进行重定向,这可能不起作用,因为请求是根据您的惯性要求进行弥补。相反,您可以返回 Inertia 响应,通过该响应 Inertia 能够将请求重定向到您想要的位置。

    $redirectUrl = Socialite::driver('driverProvider')->redirect()->getTargetUrl();
    return response('', 409)->header('X-Inertia-Location', $redirectUrl);

有一些关于此的有用信息。 https://inertiajs.com/redirects#external-redirects


0
投票

这对我有用:

Route::get('/auth/login', function () {
    return Inertia::location(Socialite::driver('auth0')->redirect());    
})

在此处查看有关外部重定向的信息:https://inertiajs.com/redirects

© www.soinside.com 2019 - 2024. All rights reserved.