-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
190 lines (157 loc) · 5.67 KB
/
server.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const express = require('express');
const path = require('path');
const cors = require('cors'); // Enable Cross-Origin Resource Sharing
const passport = require('passport');
const session = require('express-session');
const GithubStrategy = require('passport-github2').Strategy;
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const axios = require('axios');
const { Octokit } = require('@octokit/rest');
const app = express();
const port = process.env.PORT || 3000;
const dotenv=require("dotenv")
dotenv.config()
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, '../public')));
// Configure Github Credentials
const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID;
const GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET;
// Configure Google Credentials
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;
const CHANNEL_ID = process.env.CHANNEL_ID;
// set up passport.js
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL_GOOGLE,
scope: ['https://www.googleapis.com/auth/youtube.readonly']
}, (accessToken, refreshToken, profile, done) => {
profile.accessToken = accessToken;
return done(null, profile);
}));
passport.use(new GithubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL_GITHUB,
},
(accessToken, refreshToken, profile, done) => {
profile.accessToken = accessToken;
return done(null, profile);
}
));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
// Serve HTML files directly
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/login_screen.html'));
});
// Routes for Google Login
app.get('/auth/google', passport.authenticate('google', { scope: ['openid', 'profile', 'email', 'https://www.googleapis.com/auth/youtube.readonly'] }));
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/' }), async(req, res) =>
{
const { accessToken } = req.user;
req.session.googleaccessToken = accessToken;
try {
const response = await axios.get(`https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&forChannelId=${CHANNEL_ID}&mine=true`, {
headers: { Authorization: `Bearer ${accessToken}` }
});
const isSubscribed = response.data.items.length > 0;
req.session.isSubscribed = isSubscribed;
if(isSubscribed) {
res.redirect('/login/success');
}else {
res.redirect('/youtube/verification/failed');
}
}
catch(error) {
res.send('Error checking subscription');
}
}
);
// Github Routes
app.get('/auth/github', passport.authenticate('github', { scope: ['user:email'] }));
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/' }),
async (req, res) => {
const { accessToken } = req.user;
const octokit = new Octokit({
auth: accessToken
});
try {
const response = await octokit.request('GET /user/following/bytemait', {
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
if (response.status === 204) {
res.redirect('/login/success');
} else {
res.redirect('/github/verification/failed');
}
} catch (error) {
console.error('Error:', error);
res.redirect('/github/verification/failed');
}
}
);
async function ensureSubscribed(req, res, next) {
if (req.isAuthenticated() && req.session.googleaccessToken) {
const accessToken = req.session.googleaccessToken;
try {
const response = await axios.get(`https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&forChannelId=${CHANNEL_ID}&mine=true`, {
headers: { Authorization: `Bearer ${accessToken}` }
});
const isSubscribed = response.data.items.length > 0;
req.session.isSubscribed = isSubscribed;
if(isSubscribed) {
return next();
}else {
res.redirect('/youtube/verification/failed');
}
} catch (error) {
console.error('Error:', error);
res.redirect('/youtube/verification/failed');
}
} else {
return res.redirect('/');
}
}
app.get('/youtube/verification/failed', (req, res) => {
if(req.isAuthenticated()) {
res.sendFile(path.join(__dirname, 'public/youtube_verification_failed.html'));
}else {
res.redirect('/');
}
});
app.get('/github/verification/failed', (req, res) => {
if(req.isAuthenticated()) {
res.sendFile(path.join(__dirname, 'public/github_verification_fail.html'));
}else {
res.redirect('/');
}
});
app.get('/login/success', ensureSubscribed, (req, res) => {
res.sendFile(path.join(__dirname, 'public/success.html'));
});
app.get('*', (req, res) => {
res.redirect('/'); // Redirect to the homepage
});
// Start server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});