This document describes the integration of Google OAuth 2.1 for user authentication within the application. This feature allows users to sign in using their Google accounts, providing a seamless and secure authentication experience. The process involves initiating an OAuth flow with Google, handling the callback, exchanging authorization codes for tokens, and ultimately generating a JSON Web Token (JWT) for the authenticated user.
## Configuration
To enable Google OAuth 2.1 authentication, the following environment variables must be configured. These variables are loaded into the central `Env` struct in `imphnen-libs`.
*`GOOGLE_CLIENT_ID`: Your Google OAuth 2.1 Client ID.
*`GOOGLE_CLIENT_SECRET`: Your Google OAuth 2.1 Client Secret.
*`GOOGLE_REDIRECT_URL`: The URL to which Google will redirect the user after successful authentication. This must match one of the authorized redirect URIs configured in your Google Cloud Console (e.g., `http://127.0.0.1:8080/api/v1/auth/google/callback`).
### Obtaining Credentials from Google Cloud Console
1.**Navigate to Google Cloud Console:** Go to the [Google Cloud Console](https://console.cloud.google.com/).
2.**Select/Create a Project:** Choose an existing project or create a new one.
3.**Enable Google People API:** In the navigation menu, go to `APIs & Services` > `Library` and search for "Google People API" and enable it.
4.**Create OAuth Consent Screen:** Go to `APIs & Services` > `OAuth consent screen`.
* Configure your consent screen, including application name, user support email, and developer contact information.
5.**Create Credentials:** Go to `APIs & Services` > `Credentials`.
* Click `Create Credentials` > `OAuth client ID`.
* Select "Web application" as the application type.
* Provide a name for your OAuth 2.0 client.
* Under `Authorized redirect URIs`, add the `GOOGLE_REDIRECT_URL` specified in your environment variables (e.g., `http://127.0.0.1:8080/api/v1/auth/google/callback`).
* Click "Create". Your Client ID and Client Secret will be displayed. Copy these values and set them as `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` in your environment.
## API Endpoints
### 1. Initiate Google OAuth Flow
***Endpoint:**`/api/v1/auth/google/login`
***Method:**`GET`
***Description:** This endpoint initiates the Google OAuth 2.1 authentication flow. When accessed, it generates a Google authorization URL and redirects the user's browser to Google's authentication page. The user will be prompted to grant permissions to your application.
Upon successful execution, this command will return a `302 Found` status with a `Location` header containing the Google authorization URL. Your browser would typically follow this redirect.
* **Description:** This endpoint handles the redirect from Google after the user has authenticated and granted permissions. Google sends an authorization `code` and a `state` parameter to this URL. The application then uses this `code` to exchange it for an access token and user information with Google. Upon successful validation and user creation/login, a JSON response is returned containing authentication tokens and user details, identical to the credential-based login endpoint.
This section outlines how to integrate the Google OAuth 2.1 flow into a React application using the JSON API response approach.
### 1. Initiating the Login Flow
Users will click a button or link to initiate the Google OAuth process. You can implement this using either a popup window or a full page redirect approach.
#### Approach 1: Popup Window (Recommended)
```jsx
// Example React Component for Google Login with Popup