Skip to content

Latest commit

 

History

History

oauth2-google

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
title keywords description
Google OAuth2
oauth2
google
authentication
Implementing Google OAuth2 authentication.

Fiber with Google OAuth2

Github StackBlitz

This example demonstrates how to implement Google OAuth2 authentication in a Fiber application.

Prerequisites

  • Go 1.16 or higher
  • Go modules

Setup

  1. Clone the repository:

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

    go mod tidy
  3. Obtain OAuth credentials from Google Developers Console.

  4. 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

Running the Application

  1. Run the application:

    go run main.go
  2. The server will start on http://localhost:3000.

Endpoints

Method URL Description
GET /api/ Redirects to Google login URL
GET /api/auth/google/callback Handles Google OAuth2 callback and returns user's email

Example Requests

Redirect to Google Login

curl -X GET http://localhost:3000/api/

Google OAuth2 Callback

curl -X GET http://localhost:3000/api/auth/google/callback?state=state&code=code

Packages Used