Add streamer authentication

Closes #17
master
Dessa Simpson 2020-09-10 22:42:37 -07:00
parent 3c94c25458
commit b06c425352
4 changed files with 45 additions and 16 deletions

6
db/15-streamer.sql Normal file
View File

@ -0,0 +1,6 @@
CREATE TABLE streamer (
userid int NOT NULL,
tokenPair json,
PRIMARY KEY (userid),
FOREIGN KEY (userid) REFERENCES users(userid)
)

View File

@ -1,8 +1,11 @@
INSERT INTO users (userId,displayName,isFollower,isSubscriber) VALUES
INSERT INTO users (userid,displayName,isFollower,isSubscriber) VALUES
(001,'TestUser',false,false),
(002,'TestFollower',true,false),
(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=C5oeWHngDS4',002);

View File

@ -4,6 +4,7 @@ import * as twitch from "./twitch";
import { URLSearchParams } from "url";
import express from "express";
import session from "express-session";
import pg from "pg";
import pgSessionStore from "connect-pg-simple";
import fetch, { Response as FetchResponse } from "node-fetch";
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
// logged in.
async function validateApiToken(session: Express.Session) {
if (session.tokenpair) {
console.log(session.tokenpair)
}
if (session.tokenpair && ! (await twitch.isApiTokenValid(session.tokenpair))) {
session.destroy(()=>{});
}
@ -206,7 +204,6 @@ app.get("/callback", async (request, response) => {
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.user = (await twitch.apiRequest(request.session.tokenpair,"/users")).data[0];
const updateUserQuery = {
name: "updateUser",
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] });
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, '/');
});
@ -235,6 +248,13 @@ 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
app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));

View File

@ -1,29 +1,29 @@
if (!process.env.PORT) {
if (typeof process.env.PORT == undefined) {
console.log("Missing environment variable PORT");
process.exit(1);
}
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");
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");
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");
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");
process.exit(1);
}
export const sessionSecret: string = process.env.SESSION_SECRET;
export const sessionSecret: string = process.env.SESSION_SECRET!;