parent
3c94c25458
commit
b06c425352
|
@ -0,0 +1,6 @@
|
||||||
|
CREATE TABLE streamer (
|
||||||
|
userid int NOT NULL,
|
||||||
|
tokenPair json,
|
||||||
|
PRIMARY KEY (userid),
|
||||||
|
FOREIGN KEY (userid) REFERENCES users(userid)
|
||||||
|
)
|
|
@ -1,8 +1,11 @@
|
||||||
INSERT INTO users (userId,displayName,isFollower,isSubscriber) VALUES
|
INSERT INTO users (userid,displayName,isFollower,isSubscriber) VALUES
|
||||||
(001,'TestUser',false,false),
|
(001,'TestUser',false,false),
|
||||||
(002,'TestFollower',true,false),
|
(002,'TestFollower',true,false),
|
||||||
(003,'TestSubscriber',true,true),
|
(003,'TestSubscriber',true,true),
|
||||||
(004,'TestSubNonFollower',false,true);
|
(004,'TestSubNonFollower',false,true),
|
||||||
|
(229815572,'virtualdxs',true,true);
|
||||||
|
|
||||||
|
INSERT INTO streamer (userid) VALUES (229815572);
|
||||||
|
|
||||||
CALL add_request('https://www.youtube.com/watch?v=dQw4w9WgXcQ',001);
|
CALL add_request('https://www.youtube.com/watch?v=dQw4w9WgXcQ',001);
|
||||||
CALL add_request('https://www.youtube.com/watch?v=C5oeWHngDS4',002);
|
CALL add_request('https://www.youtube.com/watch?v=C5oeWHngDS4',002);
|
||||||
|
|
30
src/app.ts
30
src/app.ts
|
@ -4,6 +4,7 @@ import * as twitch from "./twitch";
|
||||||
import { URLSearchParams } from "url";
|
import { URLSearchParams } from "url";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import session from "express-session";
|
import session from "express-session";
|
||||||
|
import pg from "pg";
|
||||||
import pgSessionStore from "connect-pg-simple";
|
import pgSessionStore from "connect-pg-simple";
|
||||||
import fetch, { Response as FetchResponse } from "node-fetch";
|
import fetch, { Response as FetchResponse } from "node-fetch";
|
||||||
import db from "./db";
|
import db from "./db";
|
||||||
|
@ -13,9 +14,6 @@ import errorHandler from "./errors";
|
||||||
// logging out the user. Should be called before checking whether a user is
|
// logging out the user. Should be called before checking whether a user is
|
||||||
// logged in.
|
// logged in.
|
||||||
async function validateApiToken(session: Express.Session) {
|
async function validateApiToken(session: Express.Session) {
|
||||||
if (session.tokenpair) {
|
|
||||||
console.log(session.tokenpair)
|
|
||||||
}
|
|
||||||
if (session.tokenpair && ! (await twitch.isApiTokenValid(session.tokenpair))) {
|
if (session.tokenpair && ! (await twitch.isApiTokenValid(session.tokenpair))) {
|
||||||
session.destroy(()=>{});
|
session.destroy(()=>{});
|
||||||
}
|
}
|
||||||
|
@ -206,7 +204,6 @@ app.get("/callback", async (request, response) => {
|
||||||
if (typeof tokenResponse == 'undefined') throw new Error('tokenResponse is undefined');
|
if (typeof tokenResponse == 'undefined') throw new Error('tokenResponse is undefined');
|
||||||
request.session.tokenpair = { access_token: tokenResponse.access_token, refresh_token: tokenResponse.refresh_token };
|
request.session.tokenpair = { access_token: tokenResponse.access_token, refresh_token: tokenResponse.refresh_token };
|
||||||
request.session.user = (await twitch.apiRequest(request.session.tokenpair,"/users")).data[0];
|
request.session.user = (await twitch.apiRequest(request.session.tokenpair,"/users")).data[0];
|
||||||
|
|
||||||
const updateUserQuery = {
|
const updateUserQuery = {
|
||||||
name: "updateUser",
|
name: "updateUser",
|
||||||
text: "INSERT INTO users (userid,displayName,imageUrl) VALUES ($1,$2,$3)\
|
text: "INSERT INTO users (userid,displayName,imageUrl) VALUES ($1,$2,$3)\
|
||||||
|
@ -214,6 +211,22 @@ app.get("/callback", async (request, response) => {
|
||||||
}
|
}
|
||||||
var query = Object.assign(updateUserQuery,{ values: [request.session.user.id,request.session.user.display_name,request.session.user.profile_image_url] });
|
var query = Object.assign(updateUserQuery,{ values: [request.session.user.id,request.session.user.display_name,request.session.user.profile_image_url] });
|
||||||
db.query(query);
|
db.query(query);
|
||||||
|
if (typeof (tokenResponse as any).scope != 'undefined') { // Scopes requested - update streamer info
|
||||||
|
const getStreamerIdQuery = {
|
||||||
|
name: "getStreamerId",
|
||||||
|
text: "SELECT userid FROM streamer"
|
||||||
|
}
|
||||||
|
var streamerid = await db.query(getStreamerIdQuery).then((result: pg.QueryResult) => result.rows[0]['userid']);
|
||||||
|
if (request.session.user.id == streamerid) {
|
||||||
|
const updateStreamerQuery = {
|
||||||
|
name: "updateStreamer",
|
||||||
|
text: "INSERT INTO streamer (userid,tokenPair) VALUES ($1,$2)\
|
||||||
|
ON CONFLICT (userid) DO UPDATE SET tokenPair = $2"
|
||||||
|
}
|
||||||
|
var query = Object.assign(updateStreamerQuery,{ values: [request.session.user.id,JSON.stringify(request.session.tokenpair)] });
|
||||||
|
db.query(query);
|
||||||
|
}
|
||||||
|
}
|
||||||
response.redirect(307, '/');
|
response.redirect(307, '/');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -235,8 +248,15 @@ app.get("/", async (request, response) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Streamer Panel
|
||||||
|
//app.get("/streamer/", async (request, response) => {
|
||||||
|
//
|
||||||
|
//});
|
||||||
|
|
||||||
|
app.get("/streamer/auth", async ({}, response) => response.redirect(307, `https://id.twitch.tv/oauth2/authorize?client_id=${config.twitchClientId}&redirect_uri=${config.urlPrefix}/callback&response_type=code&scope=channel:read:subscriptions moderation:read`));
|
||||||
|
|
||||||
// Logout
|
// Logout
|
||||||
app.get ("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
|
app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
|
||||||
|
|
||||||
app.listen(config.port, () => {
|
app.listen(config.port, () => {
|
||||||
console.log(`Listening on port ${config.port}`);
|
console.log(`Listening on port ${config.port}`);
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
if (!process.env.PORT) {
|
if (typeof process.env.PORT == undefined) {
|
||||||
console.log("Missing environment variable PORT");
|
console.log("Missing environment variable PORT");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
export const port: number = parseInt(process.env.PORT as string, 10);
|
export const port: number = parseInt(process.env.PORT as string, 10);
|
||||||
|
|
||||||
if (!process.env.URL_PREFIX) {
|
if (typeof process.env.URL_PREFIX == undefined) {
|
||||||
console.log("Missing environment variable URL_PREFIX");
|
console.log("Missing environment variable URL_PREFIX");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
export const urlPrefix: string = process.env.URL_PREFIX;
|
export const urlPrefix: string = process.env.URL_PREFIX!;
|
||||||
|
|
||||||
if (!process.env.TWITCH_CLIENTID) {
|
if (typeof process.env.TWITCH_CLIENTID == undefined) {
|
||||||
console.log("Missing environment variable TWITCH_CLIENTID");
|
console.log("Missing environment variable TWITCH_CLIENTID");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
export const twitchClientId: string = process.env.TWITCH_CLIENTID;
|
export const twitchClientId: string = process.env.TWITCH_CLIENTID!;
|
||||||
|
|
||||||
if (!process.env.TWITCH_SECRET) {
|
if (typeof process.env.TWITCH_SECRET == undefined) {
|
||||||
console.log("Missing environment variable TWITCH_SECRET");
|
console.log("Missing environment variable TWITCH_SECRET");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
export const twitchSecret: string = process.env.TWITCH_SECRET;
|
export const twitchSecret: string = process.env.TWITCH_SECRET!;
|
||||||
|
|
||||||
if (!process.env.SESSION_SECRET) {
|
if (typeof process.env.SESSION_SECRET == undefined) {
|
||||||
console.log("Missing environment variable SESSION_SECRET");
|
console.log("Missing environment variable SESSION_SECRET");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
export const sessionSecret: string = process.env.SESSION_SECRET;
|
export const sessionSecret: string = process.env.SESSION_SECRET!;
|
||||||
|
|
Loading…
Reference in New Issue