This is an AuthProvider implementation for d-fischer twitch
package family that will automatically pop up a Twitch OAuth dialog in default user browser as soon as new scopes are requested.
To add this library to your project, just execute:
yarn add twitch-node-auth-provider
or using npm:
npm install twitch-node-auth-provider
To instantiate an ApiClient with this auth provider, just pass it to its constructor:
import { ApiClient } from "twitch";
import { NodeAuthProvider } from "twitch-node-auth-provider";
const authProvider = new NodeAuthProvider({
clientId: "7m7uv94xk5lu42H2G27ay7nd1rpg",
redirectUri: "https://localhost:4242/auth",
});
const client = new ApiClient({
authProvider,
});
client.helix.users.getMe(true);
name | type | optional | description |
---|---|---|---|
clientId | string |
false |
The client ID found in your Twitch Dev Console. |
redirectUri | string |
false |
The redirect URI you provided in your Twitch Dev Console. |
scopes | string | string[] |
true |
A list of scopes that will always be requested. |
accessToken | AccessToken | true |
Represents the data of an OAuth access token returned by Twitch. |
To allow the user to "log out" and change to another account, use enableForceVerify()
:
(async () => {
await client.helix.users.getMe(); // <- open login page
await client.helix.users.getMe(); // <- already logged-in
authProvider.enableForceVerify(); // <- force login for all the latter calls.
await client.helix.users.getMe(); // <- open login page
await client.helix.users.getMe(); // <- open login page
authProvider.enableForceVerify(true); // <- force login on the next call only.
await client.helix.users.getMe(); // <- open login page
await client.helix.users.getMe(); // <- already logged-in
})();
Listen all events:
authProvider.server.on("event", ({ eventName, data }) =>
console.log(eventName, data)
);
Or listen one event:
authProvider.server.on("BROWSER_OPEN", () =>
console.log("Opened Twitch login page in your default browser, take a look!")
);
- ERROR
- CLOSE
- LISTEN
- LISTENING
- BROWSER_OPEN
- ACCESS_TOKEN
- UNKNOW
- INVALIDATED
- LOGIN_TIMEOUT
- INVALID_STATE
- ACCESS_DENIED