In this article, I'll show you a complete implementation for Google OAuth integration (SSO – Single Sign On) in your Symfony project. I'm using LTS version 6.4. This allows users to sign up and sign in to you with their Google Account.
Overview
We will use the Google OAuth integration with the KnpUOAuth2ClientBundle that offers good integration with Symfony Security. The solution will include the following components:
- Installation and configuration of the required packages
- Creating a Google OAuth Client
- User Model Customization
- Implementation of the Authentication Handlers
- Frontend integration (login button))
What you should already have:
- Basic Symfony/PHP experience
- A Symfony project best from version 6.4
- A ready-made login and registration process
- A Google account
Preparatory tasks at Google
Google OAuth Setup Guide
Create a Google Cloud Console project
- Visit the [Google Cloud Console]]
- Create a new project or select an existing project
- Navigate to the "OAuth consent screen" in the "APIs & Services menu"“
- Select the user type (external or internal) and enter the required information:
– App Name
– User Support Email
– Developer Contact Information - Save the settings
Create OAuth credentials
- Navigate to "Credentials" in the "APIs & Services" menu“
- Click Create Credentials and select OAuth client ID)
- Select "Web application" as the application type)
- Enter a name for the client
- Add authorised redirect URIs:
– For the development environment: 'http://localhost:8000/connect/google/check`
– For the production environment: 'https://deine-domain.de/connect/google/check` - Click Create)
- Make a note of the "Client ID" and the "Client Secret"“
Configuring Environment Variables
Add the Client ID and Client Secret to your '.env.local' file:
|
1 2 |
|
Enable Google OAuth API
- Navigate to "Library" in the "APIs & Services" menu“
- Search by „Google People API“
- Enable this API for your project
Google OAuth integration in Symfony
Installing the required packages
|
1 2 3 4 5 6 7 8 |
|
Next, we need to set up the configuration for the OAuth2 bundle
|
1 2 3 4 5 6 7 8 9 |
|
The user model
We're now configuring our user model so that it can ingest and manage the user data provided by Google, such as name, email address, and profile picture. This means that we have to adapt our user entity. To do this, open the src/Entity/User.php and make the following changes:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
Migrating the new fields in the User Entity
|
1 2 3 4 5 |
|
Google Authenticator for Google OAuth
We create the following PHP file under src/Security/GoogleAuthenticator.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
Adjust the corresponding routes if they are different from yours.
Controller for Google OAuth integration
As another important step, we need a controller:
|
1 |
|
We add the following code to the GoogleController.php File:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Adjust the appropriate route here as well.
Customizing the Security Configuration
It's time to integrate our GoogleAuthenticator. To do this, we open our security.yaml under config/packages/security.yaml and add the following lines:
|
1 2 3 4 5 6 7 8 |
|
Google Login Button
In the login and registration template, we then add the button for Google authentication.
|
1 2 3 4 5 6 7 8 |
|
Summary of Google OAuth integration for Symfony
We have now created a complete implementation for the Google OAuth integration in your Symfony project. Here's an overview of the implementation:
Installed Packages
- KnpUOAuth2ClientBundle for OAuth2 integration
- League/OAuth2-Google as Google OAuth2 Provider
Main Components
- User Entity with Google Support:
- Stores important Google information such as googleId, profile picture, name
- Supports both traditional password authentication and Google OAuth
- GoogleAuthenticator:
- Processes the Google OAuth flow
- Creates new users or connects existing accounts
- Handles authentication for the security component
- Controller for login and Google connection:
- GoogleController with routes for OAuth initiation and callback
- SecurityController for classic login
- RegistrationController for manual registration
- Templates with Google Button:
- Login and registration pages with Google login button
Functionalities
- Single Sign-On (SSO) with Google:
- Users can log in with one click using their Google account
- Profile data is taken from the Google account
- Automatic Account Linking:
- If a user with the same email address already exists, their account will be linked to Google
- Hybrid authentication:
- Supports both traditional email/password authentication and Google OAuth
- Saves the registration source (Google or Form)
Tips for production environments
- Make sure HTTPS connections are secure
- Make sure redirect URLs are configured correctly in the Google Cloud Console
- For high user numbers (>100), full verification of the Google project is required
This implementation is future-proof and can be easily extended to include other OAuth providers. User information is stored consistently, regardless of whether a user logs in through Google or registers traditionally.
If you have any questions or get stuck at one point, feel free to write your experiences in the comments.



