Skip to main content

OAuth2

Github StackBlitz

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

Prerequisites

Ensure you have the following installed:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/oauth2
  2. Install dependencies:

    go get

Running the Application

  1. Start the application:
    go run main.go

Environment Variables

Create a .env file in the root directory and add the following variables:

# CLIENT_ID is the OAuth2 client ID
CLIENT_ID=

# CLIENT_SECRET is the OAuth2 client secret
CLIENT_SECRET=

# REDIRECT_URL is the OAuth2 redirect URL
REDIRECT_URL=

# AUTH_URL is the OAuth2 authorization URL
AUTH_URL=

# TOKEN_URL is the OAuth2 token URL
TOKEN_URL=

Example

Here is an example of how to set up an OAuth2 configuration:

package main

import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)

func main() {
conf := &oauth2.Config{
ClientID: "your-client-id",
ClientSecret: "your-client-secret",
RedirectURL: "your-redirect-url",
Endpoint: google.Endpoint,
}

// Your code here
}

References