Skip to content

Commit

Permalink
Merge pull request #109 from Markus-Rost/patch-4
Browse files Browse the repository at this point in the history
Add getCurrentAuthorizationInformation()
  • Loading branch information
reboxer authored Dec 15, 2024
2 parents 62fc30e + b4bf862 commit 0374f74
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,48 @@ oauth.getGuildMember(access_token, guildId).then(console.log);
*/
```

### `getCurrentAuthorizationInformation(access_token)`

`access_token`: The user's access token.

Returns info about the current authorization. Includes the [user](https://discord.com/developers/docs/resources/user#user-object) object of the requester's account if they authorized with the `identify` scope.

```js
const DiscordOauth2 = require("discord-oauth2");
const oauth = new DiscordOauth2();

const access_token = "6qrZcUqja7812RVdnEKjpzOL4CvHBFG";

oauth.getCurrentAuthorizationInformation(access_token).then(console.log);
/*
{
"application": {
"id": "159799960412356608",
"name": "AIRHORN SOLUTIONS",
"icon": "f03590d3eb764081d154a66340ea7d6d",
"description": "",
"hook": true,
"bot_public": true,
"bot_require_code_grant": false,
"verify_key": "c8cde6a3c8c6e49d86af3191287b3ce255872be1fff6dc285bdb420c06a2c3c8"
},
"scopes": [
"guilds.join",
"identify"
],
"expires": "2021-01-23T02:33:17.017000+00:00",
"user": {
"id": "268473310986240001",
"username": "discord",
"avatar": "f749bb0cbeeb26ef21eca719337d20f1",
"discriminator": "0",
"global_name": "Discord",
"public_flags": 131072
}
}
*/
```

### `generateAuthUrl(object)`

Dynamically generate an OAuth2 URL.
Expand Down
24 changes: 21 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare namespace OAuth {
synced_at: number;
subscriber_count: number;
revoked: boolean;
application?: Application;
application?: IntegrationApplication;
}

export interface Connection {
Expand All @@ -66,15 +66,25 @@ declare namespace OAuth {
visibility: 0 | 1;
}

export interface Application {
export interface IntegrationApplication {
id: string;
name: string;
icon: string | null | undefined;
description: string;
summary: string;
bot?: User;
}

export interface PartialApplication {
id: string;
name: string;
icon: string | null | undefined;
description: string;
hook?: boolean | null | undefined;
bot_public: boolean;
bot_require_code_grant: boolean;
verify_key: string;
}

export interface TokenRequestResult {
access_token: string;
token_type: string;
Expand All @@ -84,6 +94,13 @@ declare namespace OAuth {
webhook?: Webhook;
}

export interface AuthorizationInformation {
application: PartialApplication;
scopes: string[];
expires: string;
user?: User;
}

export interface PartialGuild {
id: string;
name: string;
Expand Down Expand Up @@ -167,6 +184,7 @@ declare class OAuth extends EventEmitter {
clientSecret?: string;
}): Promise<OAuth.TokenRequestResult>;
revokeToken(access_token: string, credentials?: string): Promise<string>;
getCurrentAuthorizationInformation(access_token: string): Promise<OAuth.AuthorizationInformation>;
getUser(access_token: string): Promise<OAuth.User>;
getUserGuilds(access_token: string, opts?: {
before?: string;
Expand Down
14 changes: 14 additions & 0 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ class OAuth extends RequestHandler {
);
}

/**
* Request info about the current authorization
* @arg {String} access_token The user access token
* @returns {Promise<Object>}
*/
getCurrentAuthorizationInformation(access_token) {
return this.request("GET", "/oauth2/@me", undefined, {
auth: {
type: "Bearer",
creds: access_token,
},
});
}

/**
* Request basic user data
* Requires the `identify` scope
Expand Down

0 comments on commit 0374f74

Please sign in to comment.