| title | OAuth2 | |||||
|---|---|---|---|---|---|---|
| keywords |
|
|||||
| description | Implementing GitHub OAuth2 authentication with GoFiber. |
This project demonstrates how to implement GitHub OAuth2 authentication in a GoFiber application.
- Go 1.21+
- A GitHub OAuth App
- Set Authorization callback URL to
http://localhost:8080/oauth/redirect
- Set Authorization callback URL to
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/oauth2 -
Copy the example env file and fill in your credentials:
cp .env.example .env
-
Install dependencies:
go mod download
go run app.goThen open http://localhost:8080 in your browser.
Create a .env file in the root directory (see .env.example):
# GitHub OAuth2 App credentials
CLIENT_ID=your_github_client_id
CLIENT_SECRET=your_github_client_secretBrowser β GET /oauth/begin
β generates CSRF state, stores in session
β redirects to https://github.com/login/oauth/authorize
GitHub β GET /oauth/redirect?code=...&state=...
β validates CSRF state
β exchanges code for access token via GitHub API
β stores token in session
β redirects to /welcome.html
GET /protected β OAUTHProtected middleware checks session token
// POST https://github.com/login/oauth/access_token
// with client_id, client_secret, and code
// Response:
// {"access_token":"gho_...","token_type":"bearer","scope":""}