Google OAuth Authentication for Website

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Google OAuth Authentication Implementation for Websites

Google OAuth 2.0 is one of the most common authorization methods. Integrates via standard OAuth2 flow or Google Identity Services (GIS)—the new JS SDK for "Sign in with Google" button.

Registering OAuth Client

  1. Google Cloud Console → APIs & ServicesCredentials
  2. Create OAuth 2.0 Client ID of type Web application
  3. Add Authorized redirect URIs: https://example.com/auth/google/callback
  4. Save Client ID and Client Secret

Laravel Socialite

composer require laravel/socialite
// config/services.php
'google' => [
    'client_id'     => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect'      => env('GOOGLE_REDIRECT_URI'),
],
// routes/web.php
Route::get('/auth/google',          [GoogleAuthController::class, 'redirect']);
Route::get('/auth/google/callback', [GoogleAuthController::class, 'callback']);
class GoogleAuthController extends Controller
{
    public function redirect(): RedirectResponse
    {
        return Socialite::driver('google')
            ->scopes(['openid', 'profile', 'email'])
            ->redirect();
    }

    public function callback(): RedirectResponse
    {
        try {
            $googleUser = Socialite::driver('google')->user();
        } catch (\Exception $e) {
            return redirect('/login')->withErrors(['google' => 'Google authorization error']);
        }

        $user = User::updateOrCreate(
            ['google_id' => $googleUser->getId()],
            [
                'name'              => $googleUser->getName(),
                'email'             => $googleUser->getEmail(),
                'email_verified_at' => now(),
                'avatar'            => $googleUser->getAvatar(),
            ]
        );

        Auth::login($user, remember: true);

        return redirect()->intended('/dashboard');
    }
}

Link with Existing Account

If user is already registered with same email via password—decide how to merge accounts:

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

    // Search by google_id
    $user = User::where('google_id', $googleUser->getId())->first();

    // If not—search by email (link existing account)
    if (!$user) {
        $user = User::where('email', $googleUser->getEmail())->first();

        if ($user) {
            $user->update(['google_id' => $googleUser->getId()]);
        } else {
            $user = User::create([
                'google_id'         => $googleUser->getId(),
                'name'              => $googleUser->getName(),
                'email'             => $googleUser->getEmail(),
                'email_verified_at' => now(),
            ]);
        }
    }

    Auth::login($user, remember: true);
    return redirect()->intended('/dashboard');
}

One Tap / Google Identity Services

New Google Identity Services SDK shows One Tap popup without redirect:

<script src="https://accounts.google.com/gsi/client" async defer></script>
<div id="g_id_onload"
     data-client_id="{{ config('services.google.client_id') }}"
     data-callback="handleGoogleResponse"
     data-auto_prompt="false">
</div>
<div class="g_id_signin" data-type="standard"></div>
function handleGoogleResponse(response) {
    // response.credential — is id_token (JWT)
    fetch('/auth/google/token', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrfToken },
        body: JSON.stringify({ credential: response.credential }),
    }).then(r => r.json()).then(data => {
        if (data.redirect) window.location.href = data.redirect;
    });
}
// Verify id_token on server
use Google\Client as GoogleClient;

public function handleToken(Request $request): JsonResponse
{
    $client = new GoogleClient(['client_id' => config('services.google.client_id')]);
    $payload = $client->verifyIdToken($request->credential);

    if (!$payload) {
        return response()->json(['error' => 'Invalid token'], 401);
    }

    $user = User::updateOrCreate(
        ['google_id' => $payload['sub']],
        ['name' => $payload['name'], 'email' => $payload['email']]
    );

    Auth::login($user);
    return response()->json(['redirect' => '/dashboard']);
}

Timeline

Standard OAuth2 flow via Socialite—1–2 days. With One Tap and account linking—2–3 days.