This codebase demonstrates the ability to "login with Google", when building a website in PHP.
This makes use of the Slim4 framework for routing, and the google/apiclient PHP package to assist
with the Google OAuth2 flow.
The following article proved immensely helpful when putting this codebase together: https://www.coderglass.com/php/login-with-google-in-php.php and would be appropriate to others who want an example that is more "raw" or stripped down.
- Clone the repository.
- Register an application with Google to get a google client ID and secret for your website to use for logging in with Google (details further down).
- Create a
.envfile from the.env.exampleand give it your Google app credentials. - Navigate to the site folder and run
composer installto install the packages. - Navigate to the site/public folder and run
sudo php -S localhost:80to run the website locally. - Navigate to http://localhost in your browser to see the demo site.
For the above to work, you may need to install PHP 8.4 and composer.
In order to be able to use Google to authenticate user logins, you need to register your application with google to generate a client ID and secret. You can do this by going to the Google API Console.
- Then create a new project, give it a name, and click create.
- Then select Credentials from the panel on the left in the APIs & Services section.
- Then click Configure consent screen.
- Then Get started.
- Then create Oauth2 client.
- Set the type to Web application.
- Give the application name.
- Set the authorised redirect URIs.
- Copy down the client ID, and secret.