-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (73 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require('express');
const axios = require('axios');
const { StaticAuthProvider } = require('@twurple/auth');
const { ApiClient } = require('@twurple/api');
const { TwitchAuth } = require('./dbObjects');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', (req,res) => {
res.send(`
Hier gibts nichts!
`);
});
app.get('/callback', (req, res) => {
axios.post('https://id.twitch.tv/oauth2/token', {
client_id: process.env.TWITCH_CLIENT_ID,
client_secret: process.env.TWITCH_CLIENT_SECRET,
code: req.query.code,
grant_type: 'authorization_code',
redirect_uri: process.env.TWITCH_CALLBACK_URL,
})
.then(async function(response) {
const authProvider = new StaticAuthProvider(process.env.TWITCH_CLIENT_ID, response.data.access_token);
const apiClient = new ApiClient({ authProvider });
const me = await apiClient.users.getMe();
const auth = await TwitchAuth.findOne({
where: {
user_id: me.id,
},
});
if (auth) {
await TwitchAuth.update(
{
access_token: response.data.access_token,
refresh_token: response.data.refresh_token,
expires_in: response.data.expires_in,
obtainment_timestamp: 0,
scope: response.data.scope,
},
{
where: {
user_id: me.id,
},
});
res.send(`
Du kannst diese Seite jetzt schließen!
`);
return;
}
try {
await TwitchAuth.create({
access_token: response.data.access_token,
refresh_token: response.data.refresh_token,
expires_in: response.data.expires_in,
obtainment_timestamp: 0,
scope: response.data.scope,
user_id: me.id,
});
}
catch (error) {
// console.log(error);
}
res.send(`
Du kannst diese Seite jetzt schließen!
`);
})
.catch(function(error) {
console.log(error);
});
});
app.listen(process.env.PORT || 3000, () => {
console.log('Server started on port ' + process.env.PORT);
});