This package provides a simple API for authenticating users using OAuth2 with Google as the provider. It includes endpoints to initiate the authentication process and handle callback responses.
- OAuth2 Integration: Supports Google OAuth2 for user authentication.
- User Information Retrieval: Fetches and returns authenticated user information such as email, name, and profile picture.
- Scalable Design: Implements the Gin web framework for efficient routing and request handling.
- Go 1.16+
- Google OAuth2 credentials
.env
file for storing sensitive credentials
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Install dependencies:
go mod tidy
-
Create a
.env
file in the root directory with the following content:CLIENT_ID=<your-google-client-id> CLIENT_SECRET=<your-google-client-secret>
-
Run the application locally:
go run main.go
-
Open your browser and navigate to
http://localhost:8000
.
For deploying on a cloud provider (e.g., AWS, Google Cloud, etc.), use the provided Handler
function as the entry point for handling requests.
Redirects users to Google's OAuth2 authorization page.
Handles OAuth2 callback, retrieves the access token, and fetches user information.
- Visit the root endpoint (
http://localhost:8000
) to start the OAuth2 process. - Log in with your Google account.
- Upon successful authentication, the callback endpoint fetches and displays user information as JSON.
{
"id": "1234567890",
"email": "user@example.com",
"verified_email": true,
"name": "John Doe",
"given_name": "John",
"family_name": "Doe",
"picture": "https://example.com/photo.jpg",
"locale": "en"
}
├── main.go # Main application entry point
├── go.mod # Module dependencies
├── go.sum # Dependency checksums
├── .env # Environment variables
└── README.md # Documentation