简体   繁体   中英

Laravel 8 Socialite Facebook redirect didn't work/return null

I have an app using Laravel 8 as backend. I already have auth process using a custom table named "accounts" and I want to use some social login process, starting with facebook. The thing is I facing some problems coding it. My controller isn't redirecting correctly to facebook and returning {"data": null, "error": null} . I searched on this issue and already change some parts of the code to solve this, but the problem persist. Here are my code:

my route:

use App\Http\Controllers\LoginSocialController;

Route::get('account/{provider}/login', [LoginSocialController::class, 'redirectToProvider'])->name('social_login');
Route::get('account/{provider}/login/callback', [LoginSocialController::class, 'handleProviderCallback'])->name('social_login_callback');

my controller:

namespace App\Http\Controllers;

use Laravel\Socialite\Facades\Socialite;
use App\Services\AccountService;
use App\Services\AuthService;

use Exception;

class LoginSocialController extends Controller
{
    /**
     * 
     */
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->stateless()->redirect();
    }

    public function handleProviderCallback($provider)
    {
        $providerAccount = Socialite::driver($provider)->stateless()->user();
        dd($providerAccount);
    }
}

my services.php:

return [   
    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],

    'postmark' => [
        'token' => env('POSTMARK_TOKEN'),
    ],

    'ses' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
    ],

    'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect' => env('FACEBOOK_CALLBACK')
    ],

    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => env('GOOGLE_CALLBACK')
    ],

    'apple' => [
        'client_id' => env('APPLE_CLIENT_ID'),
        'client_secret' => env('APPLE_CLIENT_SECRET'),
        'redirect' => env('APPLE_CALLBACK')
    ],

    'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => env('GITHUB_CALLBACK')
    ],

];

I don't know what I'm doing wrong, but it seems to be something with the project not being web and using the laravel only as backend. Has anyone with this same issue?

TIA

Your callback route

Route::get('account/{provider}/login/callback', [LoginSocialController::class, 'handleProviderCallback'])->name('social_login_callback');

must be the same in your FB App that you created before in Facebook developer

在此处输入图像描述

and also check the App secret and App id in your .env file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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