twitch-oauth
Version:
Authenticate users with Twitch.
95 lines • 4.14 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwitchOAuth = void 0;
const isomorphic_unfetch_1 = __importDefault(require("isomorphic-unfetch"));
const twitch_oauth_error_1 = require("./twitch-oauth-error");
const utils_1 = require("./utils");
class TwitchOAuth {
constructor(settings) {
this.settings = settings;
}
authenticate() {
const method = this.settings.method || 'code';
const body = {
response_type: method,
client_id: this.settings.clientId,
redirect_uri: this.settings.redirectUri,
scope: this.settings.scope,
};
return 'https://id.twitch.tv/oauth2/authorize?' + (0, utils_1.jsonToURLEncoded)(body);
}
verifyCodeResponse(code) {
return __awaiter(this, void 0, void 0, function* () {
const body = {
client_id: this.settings.clientId,
client_secret: this.settings.clientSecret,
code: code,
grant_type: 'authorization_code',
redirect_uri: this.settings.redirectUri,
};
const req = yield (0, isomorphic_unfetch_1.default)('https://id.twitch.tv/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: (0, utils_1.jsonToURLEncoded)(body),
});
const res = yield req.json();
if (res.message) {
throw new twitch_oauth_error_1.TwitchOAuthError(res.status, res.message);
}
return res;
});
}
validate(accessToken) {
return __awaiter(this, void 0, void 0, function* () {
const req = yield (0, isomorphic_unfetch_1.default)('https://id.twitch.tv/oauth2/validate', {
headers: {
'Client-Id': this.settings.clientId,
Authorization: `bearer ${accessToken}`,
},
});
const res = yield req.json();
if (res.message) {
throw new twitch_oauth_error_1.TwitchOAuthError(res.status, res.message);
}
return res;
});
}
refresh(refresh_token) {
return __awaiter(this, void 0, void 0, function* () {
const body = {
client_id: this.settings.clientId,
client_secret: this.settings.clientSecret,
grant_type: 'refresh_token',
refresh_token,
};
const req = yield (0, isomorphic_unfetch_1.default)('https://id.twitch.tv/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: (0, utils_1.jsonToURLEncoded)(body),
});
const res = yield req.json();
if (res.message) {
throw new twitch_oauth_error_1.TwitchOAuthError(res.status, res.message);
}
return res;
});
}
}
exports.TwitchOAuth = TwitchOAuth;
//# sourceMappingURL=twitch-oauth.js.map