Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

README.md

title OAuth2
keywords
oauth2
golang
authentication
github
api
description Implementing GitHub OAuth2 authentication with GoFiber.

OAuth2 (GitHub)

Github StackBlitz

This project demonstrates how to implement GitHub OAuth2 authentication in a GoFiber application.

Prerequisites

  • Go 1.21+
  • A GitHub OAuth App
    • Set Authorization callback URL to http://localhost:8080/oauth/redirect

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/oauth2
  2. Copy the example env file and fill in your credentials:

    cp .env.example .env
  3. Install dependencies:

    go mod download

Running the Application

go run app.go

Then open http://localhost:8080 in your browser.

Environment Variables

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_secret

OAuth2 Flow

Browser β†’ 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

Example: GitHub OAuth2 token exchange

// POST https://github.com/login/oauth/access_token
// with client_id, client_secret, and code
// Response:
// {"access_token":"gho_...","token_type":"bearer","scope":""}

References