This repository contains a simple Discord OAuth2 API built using the Fiber web framework in the Go programming language. It also integrates with a PostgreSQL database to store user information after successful authentication through Discord. This README will guide you through setting up and using the API.
Before you begin, make sure you have the following tools and dependencies installed:
- Go (Golang): Install from Go Downloads
- PostgreSQL: Install from PostgreSQL Downloads
- Discord Developer Application: Create one at Discord Developer Portal
- Clone the repository to your local machine:
git clone https://github.com/hackirby/discord-oauth2-api-go.git
- Navigate to the project directory:
cd discord-oauth2-api-go
- Install Go dependencies:
go mod tidy
go run ./cmd/app
Before launching the application, you must configure it. Create a .env file in the project's root directory and populate it with the following variables:
PORT=3333
CLIENT_ID=YOUR_DISCORD_CLIENT_ID
CLIENT_SECRET=YOUR_DISCORD_CLIENT_SECRET
REDIRECT_URI=YOUR_DISCORD_REDIRECT_URI
DATABASE_DSN="user=<USER> password=<PASSWORD> dbname=<DATABASE> sslmode=disable"
Ensure that you replace YOUR_DISCORD_CLIENT_ID
, YOUR_DISCORD_CLIENT_SECRET
, YOUR_DISCORD_REDIRECT_URI
and DATABASE_DSN
with your actual Discord OAuth2 credentials and PostgreSQL database details.
GET /api/oauth2/url
GET /api/oauth2/token
Parameter | Description |
---|---|
code |
Required. code from callback |
GET /api/user/
Header | Description |
---|---|
Authorization |
Required. user access token |
To authenticate users through Discord OAuth2, adhere to these steps:
- Get OAuth2 Discord page by calling /api/oauth2/url.
- Redirect the user to the page
- Upon successful authentication, the user will be redirected back to your frontend redirect URL.
- Use /api/oauth2/token to sign-in/sign-up the user and get his access token.
As an example, you can access the /api/user endpoint to retrieve their record in the Database with the access token as Authorization
header.
User data is stored securely in a PostgreSQL database. The database schema is automatically generated upon application startup. After a successful Discord OAuth2 authentication, user data is stored in this database.
If you wish to contribute to this project, kindly open an issue or submit a pull request with your proposed changes. We warmly welcome contributions from the community.
This project is licensed under the MIT License. For more information, please consult the LICENSE file.