Skip to content

Commit affad7f

Browse files
committed
Step 2 - Setup the server
1 parent bd893a2 commit affad7f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

server.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var jwt = require("jsonwebtoken")
2+
//You must use your own API key
3+
//For a dummy API key I2E_JQ.79AfrA:sw2y9zarxwl0Lw5a
4+
var appId = 'I2E_JQ'
5+
var keyId = '79AfrA'
6+
var keySecret = 'sw2y9zarxwl0Lw5a'
7+
var ttlSeconds = 60
8+
9+
var jwtPayload =
10+
{
11+
'x-ably-capability': JSON.stringify({ '*': ['publish', 'subscribe'] })
12+
}
13+
var jwtOptions =
14+
{
15+
expiresIn: ttlSeconds,
16+
keyid: `${appId}.${keyId}`
17+
}
18+
19+
var express = require('express'),
20+
app = express();
21+
app.use('/', express.static(__dirname))
22+
app.get('/auth', function (req, res) {
23+
console.log('Sucessfully connected to the server auth endpoint')
24+
jwt.sign(jwtPayload, keySecret, jwtOptions, function (err, tokenId) {
25+
console.log('JSON Web Token signed by auth server')
26+
if (err) {
27+
console.trace()
28+
return
29+
}
30+
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
31+
res.setHeader('Content-Type', 'application/json');
32+
console.log('Sending signed JWT token back to client')
33+
res.send(JSON.stringify(tokenId));
34+
})
35+
});
36+
app.listen(3000, function () {
37+
console.log('Web server listening on port 3000');
38+
});

0 commit comments

Comments
 (0)