-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (55 loc) · 1.7 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
// Require all the installs
import express from "express";
import session from "express-session";
import SteamSignIn from "steam-signin";
import dotenv from "dotenv";
dotenv.config();
import axios from "axios";
// Create a new SteamSignIn object
const realm = process.env.API_URL;
const signIn = new SteamSignIn(realm)
// Create an express app
const app = express();
// Let's set a port
const port = 3000;
// Set up the session
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
}));
// Spin up the server
app.listen(port, () => {
console.log('Listening, port ' + port);
});
app.get('/api/v1/auth/steam', (req, res) => {
res.statusCode = 302;
res.setHeader(
"Location",
signIn.getUrl(process.env.API_URL + '/api/v1/auth/steam/return')
)
res.end();
});
app.get('/api/v1/auth/steam/return', async (req, res) => {
res.setHeader('Content-Type', 'text/plain');
try {
let steamId = await signIn.verifyLogin(req.url);
const steamid64 = steamId.getSteamID64();
const response = await axios.get(`https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${process.env.STEAM_API_KEY}&steamids=${steamid64}`)
const { avatarfull, personaname } = response.data.response.players[0];
req.session.user = {
steamid64,
avatarfull,
personaname,
}
await req.session.save()
res.redirect(process.env.API_URL);
} catch (error) {
console.error(error);
return res.status(500).send('There was an error signing in.');
}
});
app.get('/', (req, res) => {
const { user } = req.session;
res.status(200).send(user);
});