India Project - oAuth2 - Discord
oAuth2 client for discord, built with typescript and can be used with both typescript and javascript. Examples are displayed in typescript but usage in javascript will be mostly the same. Open a pullrequest or send me a messaged on discord (Eskpil#1212) if you have any questions.
Examples
Example usage:
import { Client } from "@india-project/discord-oauth";
import express from "express";
const app = express();
const OauthClient = new Client({
client_id: "Client id here",
client_secret: "Client secret here",
version: "What version of the api you wish to use, example 8",
callback: "Client callback url here",
scopes: ["A string array of scopes go here"],
});
app.get("/auth", (req, res) =>
// The authenticate method takes 3 params, the req and res param from express route and a state of your choosing, example some idenfifyer of some sort
OauthClient.authenticate(req, res, "Identifyer/state here")
);
app.get("/callback", async (req, res) => {
const payload = await OauthClient.callback(req);
// The returned payload returns a user object, access and refresh token and the state you passed earlier
});
app.listen(5000, () => console.log("Server started on port 5000"));
Example payload data:
{
"options": {
"accessToken": "fiofuiehfuiehfuiheuifheuifhiuehfuidhf",
"refreshToken": "FJEHFYEfejihfwyehiuHIUFHuihUIh83re32",
"expiresIn": 432434,
"state": "My state",
"user": {
"options": {
"id": "163674707013402625",
"username": "Eskpil",
"avatar": "9d266991ee9afb097dc2ae4766ebf510",
"discriminator": "1212",
"publicFlags": 64,
"flags": 64,
"email": "example@example.com",
"verified": true,
"locale": "en-US",
"mfaEnabled": true
}
}
}
}
Example refresh method usage:
app.get("/newTokens", async (req, res) => {
// The refresh method takes one param, the refresh_token
const newTokens = await OauthClient.refresh("fjhrfewddjfhdfuhefhdsfs");
});
And example of the refresh method response:
{
"options": {
"accessToken": "jfierwhfuihreuigfheruihgeruihg",
"refreshToken": "fpjkwefohreugyergyergierhgergr",
"expiresIn": 235564
}
}
Example GetUserGuilds usage:
app.get("/guilds", async (req, res) => {
// The GetUserGuilds method has the access token as a required param
const guilds = await GetUserGuilds("token");
});
Example User guilds response:
[
{
"id": "684898665143206084",
"name": "Deno",
"icon": "a_a5f8c917998d95e14dd3c48ea9cd0924",
"owner": false,
"permissions": "104188993",
"features": ["ANIMATED_ICON", "BANNER", "VANITY_URL", "INVITE_SPLASH"]
},
{
"id": "714657695717916683",
"name": "Ontwik",
"icon": "80be1f5fb1ccb274df25a3d91caed02c",
"owner": false,
"permissions": "104320577",
"features": []
},
{
"id": "732637179485880382",
"name": "Liv’s Sweet Shop!",
"icon": "af71dd8567fd7cc48e4e394c2c8765d2",
"owner": false,
"permissions": "104320577",
"features": [
"ANIMATED_ICON",
"NEWS",
"INVITE_SPLASH",
"COMMUNITY",
"WELCOME_SCREEN_ENABLED"
]
}
]
Installation
With yarn:
yarn add @india-project/discord-oauth
with npm:
npm i --save @india-project/discord-oauth