Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samirapadidar's avatar

how to create login and register with googl account?

I want to add a feature to my project called login and register with Google account For this I have installed league/oauth2-client package, now I want to test it For this I used the following code

        $provider = new Google([
            'clientId'     => 'your-client-id',
            'clientSecret' => 'your-client-secret',
            'redirectUri'  => 'your-redirect-uri',
        ]);

        var_dump($provider);
        exit();

But I have a problem with the value of clientId and so on. I know that I have to create a new project in Google Developers Console for that But in the API Library part, I dont know what I have to enable Please help me create a project in Google Developers Console Thanks

0 likes
3 replies
alden8's avatar

@samirapadidar solution with league/oauth2-client package:

-- create a Google project (https://console.developers.google.com/)

-- enable APIs and OAuth consent screen in this Google project with redirect URI http://localhost:8000/auth/google/callback

-- get Client ID and Secret of this Google project

-- install and configure league/oauth2-client Package:

composer require league/oauth2-client

-- create a configuration file config/google_oauth_config.php and fill it with the following details, replacing placeholders with your actual values:

return [
    'google' => [
        'client_id' => 'YOUR_CLIENT_ID',
        'client_secret' => 'YOUR_CLIENT_SECRET',
        'redirect_uri' => 'YOUR_REDIRECT_URI',
    ],
];

-- implement login and registration flow:

require 'vendor/autoload.php';
require 'config/google_oauth_config.php '; // Include your configuration file

// Create a Google provider object
$provider = new League\OAuth2\Client\Provider\Google([
    'clientId'      => $config['google']['client_id'],
    'clientSecret'  => $config['google']['client_secret'],
    'redirectUri'   => $config['google']['redirect_uri'],
]);

// Handle login request
if (!isset($_GET['code'])) {
    // Redirect to Google login page
    $authorizationUrl = $provider->getAuthorizationUrl([
        'access_type' => 'offline', // Request refresh token for persistent logins
        'prompt' => 'select_account' // Allow user to choose Google account
    ]);
    header('Location: ' . $authorizationUrl);
    exit;
}

// Handle login callback
$token = $provider->getAccessToken('authorization_code', [
    'code' => $_GET['code']
]);

// Use the access token to fetch user information
$user = $provider->getResourceOwner($token);

// Process user information for login or registration
// ... (You'll need to implement your own logic here)

echo 'Welcome, ' . $user->getEmail();
``
martinbean's avatar

@samirapadidar As @snapey says, Laravel has a first-party package, Socialite, for exactly this.

public function redirectToProvider()
{
    return Socialite::driver('google')->redirect();
}

public function handleProviderCallback()
{
    $googleUser = Socialite::driver('google')->user();

    // Retrieve app user based on $googleUser->getId() value...
}

Please or to participate in this conversation.