add twitch oauth login

This commit is contained in:
Jay
2018-08-12 23:31:09 +08:00
parent 89b24e0bf6
commit 334fce3b9b
11 changed files with 140 additions and 77 deletions
+5 -3
View File
@@ -1,9 +1,11 @@
const twitch = require('./twitch')
const twitchPublic = require('./twitch-public')
const line = require('./line')
const google = require('./google')
const twitchNew = require('./twitch-new')
module.exports = {
twitch,
twitchPublic,
line,
google
google,
twitchNew
}
+32
View File
@@ -0,0 +1,32 @@
const axios = require('axios')
const config = require('@config/index')
const baseURL = `https://api.twitch.tv/helix`
const client = axios.create({
baseURL,
headers: {
'Client-ID': config.twitch.clientid
}
})
const tokenHeader = (token) => {
if (typeof token !== 'string' || token.trim().length === 0) return null
return {
'Authorization': `Bearer ${token}`
}
}
module.exports.getUserData = async (token = '') => {
let headers = tokenHeader(token)
if (headers === null) return null
let url = '/users'
let result = await client({
method: 'get',
headers,
url
})
if (!('data' in result) || !('data' in result.data) || !Array.isArray(result.data.data)) return null
if (result.data.data.length === 0) return null
return result.data.data[0]
}
+1 -1
View File
@@ -236,7 +236,7 @@ const addTwitch = async (txt = '', source = {}, db) => {
return { reply }
}
let twitchUser = await api.twitch.getUserIDByName(name)
let twitchUser = await api.twitchPublic.getUserIDByName(name)
if (twitchUser === null) return null
// check channel count