-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (130 loc) · 4.2 KB
/
Copy pathindex.js
File metadata and controls
155 lines (130 loc) · 4.2 KB
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
const express = require('express');
const passport = require('passport');
const session = require('express-session');
const SteamStrategy = require('passport-steam').Strategy;
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const NODE_ENV = process.env.NODE_ENV || 'development';
const envPath = NODE_ENV === 'development' ? '.env.dev' : '.env';
require('dotenv').config({ path: path.resolve(__dirname, envPath) });
console.log(`Loaded environment: ${NODE_ENV} from ${envPath}`);
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((obj, done) => done(null, obj));
passport.use(new SteamStrategy({
returnURL: `${process.env.HOST}/auth/steam/return`,
realm: `${process.env.HOST}/`,
apiKey: process.env.STEAM_API_KEY
},
function(identifier, profile, done) {
process.nextTick(() => {
profile.identifier = identifier;
return done(null, profile);
});
}
));
app.use(session({
secret: process.env.SECRET_KEY,
name: 'steamlogin',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth/steam',
passport.authenticate('steam', { failureRedirect: '/' })
);
app.get('/auth/steam/return',
passport.authenticate('steam', { failureRedirect: '/' }),
(req, res) => {
res.redirect('/profile');
}
);
app.get('/profile', ensureAuthenticated, async (req, res) => {
const steamId = req.user.id;
const apiKey = process.env.STEAM_API_KEY;
const games = await fetchPersonalLibrary(steamId, apiKey);
const friends = await fetchSteamFriends(steamId, apiKey);
const sharedGamesByFriend = {};
for (const friend of friends) {
const friendGames = await fetchFriendLibrary(friend.steamid, apiKey);
sharedGamesByFriend[friend.steamid] = friendGames;
}
res.render('profile', { user: req.user, games, friends, sharedGamesByFriend });
});
async function fetchPersonalLibrary(steamId, apiKey) {
const url = `https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/`;
try {
const response = await axios.get(url, {
params: {
key: apiKey,
steamid: steamId,
include_appinfo: 1,
include_played_free_games: 1,
}
});
return response.data.response.games || [];
} catch (error) {
console.error('Error fetching Steam library:', error.message);
return [];
}
}
async function fetchSteamFriends(steamId, apiKey) {
try {
const friendListRes = await axios.get('https://api.steampowered.com/ISteamUser/GetFriendList/v1/', {
params: {
key: apiKey,
steamid: steamId,
relationship: 'friend',
}
});
const friendIds = friendListRes.data.friendslist.friends.map(f => f.steamid);
const summariesRes = await axios.get('https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/', {
params: {
key: apiKey,
steamids: friendIds.join(','),
}
});
return summariesRes.data.response.players.map(p => ({
steamid: p.steamid,
personaname: p.personaname,
avatar: p.avatarfull,
}));
} catch (error) {
console.error('Error fetching Steam friends:', error.message);
return [];
}
}
async function fetchFriendLibrary(friendSteamId, apiKey) {
try {
const response = await axios.get('https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/', {
params: {
key: apiKey,
steamid: friendSteamId,
include_appinfo: 0, // Don't need full info, just appids
include_played_free_games: 1,
}
});
return response.data.response.games?.map(g => g.appid) || [];
} catch (error) {
console.error(`Error fetching library for ${friendSteamId}:`, error.message);
return [];
}
}
app.get('/logout', (req, res) => {
req.logout(() => {
res.redirect('/');
});
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) return next();
res.redirect('/');
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on http://computer.local:${PORT}`);
});