-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (92 loc) · 2.89 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
const express = require('express')
const cors = require('cors')
const path = require('path')
const session = require('express-session')
const app = express()
const SteamAuth = require('node-steam-openid')
const backendPort = process.env.PORT || 8080
const steamApiKey = process.env.STEAM_API_KEY
const backendUrl = `http://localhost:${backendPort}`
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:3000'
const steamCallbackPath = '/auth/steam/callback'
const steam = new SteamAuth({
realm: backendUrl,
returnUrl: `${backendUrl}${steamCallbackPath}`,
apiKey: steamApiKey,
})
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {secure: true}
}))
app.use(express.static(path.join(__dirname, 'public')))
const allowedOrigins = [frontendUrl]
app.use(cors({
origin: (origin, callback) => {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true)
} else {
callback(new Error(`Origin ${origin} not allowed by CORS`))
}
}
}))
app.get('/ping', function (req, res) {
return res.send('pong')
})
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
app.get('/auth/steam', async (req, res) => {
console.log('Steam authentication started, redirecting...')
const redirectUrl = await steam.getRedirectUrl()
return res.redirect(redirectUrl)
})
app.get(steamCallbackPath, async (req, res) => {
console.log('Steam authentication callback...')
try {
const user = await steam.authenticate(req)
console.log('authenticated as Steam user', user.username)
req.session.user = user
return res.redirect(`${frontendUrl}?steamid=${user.steamid}`)
} catch (error) {
console.error('Steam auth error:', error)
}
})
app.get('/api/steam/user', (req, res, next) => {
if (req.session.user) {
res.send(req.session.user)
} else {
res.status(401).send({ error: 'Not logged in' })
}
})
app.get('/api/steam', async (req, res, next) => {
let url = req.query.path
for (var key in req.query) {
if (key !== 'path') {
const joiner = url.includes('?') ? '&' : '?'
url = `${url}${joiner}${key}=${encodeURIComponent(req.query[key])}`
}
}
console.log('Steam JSON API request:', url)
const joiner = url.includes('?') ? '&' : '?'
url = `http://api.steampowered.com${url}${joiner}key=${steamApiKey}`
const response = await fetch(url)
let data
try {
data = await response.json()
} catch (e) {
res.status(400)
console.error('Error parsing response for', req.query.path, e)
data = { error: e.message }
}
res.send(data)
})
console.log("Starting server on port " + backendPort)
if (typeof steamApiKey === 'undefined' || steamApiKey.length < 1) {
console.error("No Steam API key found, set in STEAM_API_KEY")
} else {
console.log("Have Steam API key")
}
app.listen(backendPort)