Skip to content
Danny SMc edited this page Dec 29, 2021 · 2 revisions

The OAuth module is for managing Discord SSO (OAuth2) with the HTTP plugin for the Turbo engine. The module takes in contexts and certain data to process and get you data through Discord's official SSO, see Configuration for setting up Discord SSO in your bot.

There are a few main functions in the OAuth module, outlined below.


OAuth.DoAuthorisation(redirectPath: string, context: Http.Context): void

This method can be run from a controller, and will accept the http context and redirect path, and process and send the user to Discord to authorise themselves, once they come back, they will then be returned to the redirect path.


OAuth.DoAccept(context: Http.Context): Promise<Record<string, any> | null>

This method should be called from the controller where you set the user's redirect path, and when they come back, they will come with an access token, this code will take and handle the token authentication with Discord, and return the token back to you.


OAuth.GetUser(token: string): Promise<Record<string, any> | null>

This method is a helper method and once you have the token, it will make a request and get the user's details from Discord, this depends on the scope you have requested for, once you have this, you can then process this into your database.


OAuth.DoFetch(token: string, apiPath: string): Promise<Response>

This method is, again, a helper for creating basic get requests, like the one above, but it will take in the token and the api path, and will return the response which you can pull the data passed back by Discord, for example, getting a list of the guilds the user is in.


OAuth.GetHeaders(token: string): Record<string, string>

This method is a helper, but will simply return the headers for the request, including the processed authorization bearer token so you can make more custom requests, included with this is a GetFetch method to return the Fetch function we use (which is cross-fetch).


OAuth.GetFetch(): fetch

This method simply returns the fetch function we use, this is cross-fetch which is a library that allows us to make cross-platform requests, this is included with this module.