Reinin

A prospective computer science student living in the American North East. New blog posts every weekend

Invalidate a Discord OAuth2 token in NodeJS

2024-01-23 oauth2 discordjs

The discordjs documentation does not include how to invalidate a token using JS. After a lot of trouble, I figured it out. In order to reduce dependency count, I am using the node:fetch module.

async function invalidateToken(access_token) {
    const response = await fetch('https://discord.com/api/oauth2/token/revoke', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: new URLSearchParams({
            'client_id': process.env.CLIENT_ID,
            'client_secret': process.env.CLIENT_SECRET,
            'token': access_token,
        }),
    });
    console.log(response);
}

We can see that this function creates a response variable which awaits a post request to the revocation endpoint using URLSearchparams in the body containing the token to revoke, the client id, and the client secret. If this is successful we should see a status of 200 in the console log.