title | keywords | |||
---|---|---|---|---|
Google OAuth2 |
|
This example demonstrates how to implement Google OAuth2 authentication in a Fiber application.
- Go 1.16 or higher
- Go modules
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/oauth2-google
-
Install dependencies:
go mod tidy
-
Obtain OAuth credentials from Google Developers Console.
-
Create a
.env
file in the root directory and add your Google OAuth credentials:GOOGLE_CLIENT_ID=your_client_id GOOGLE_CLIENT_SECRET=your_client_secret GOOGLE_REDIRECT_URL=http://localhost:3000/api/auth/google/callback
-
Run the application:
go run main.go
-
The server will start on
http://localhost:3000
.
Method | URL | Description |
---|---|---|
GET | /api/ | Redirects to Google login URL |
GET | /api/auth/google/callback | Handles Google OAuth2 callback and returns user's email |
curl -X GET http://localhost:3000/api/
curl -X GET http://localhost:3000/api/auth/google/callback?state=state&code=code