This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.js
52 lines (41 loc) · 1.6 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
var express = require('express');
var {AccessToken} = require('agora-access-token');
var {Token, Priviledges} = AccessToken;
var PORT = process.env.PORT || 8080;
if (!(process.env.APP_ID && process.env.APP_CERTIFICATE)) {
throw new Error('You must define an APP_ID and APP_CERTIFICATE');
}
var APP_ID = process.env.APP_ID;
var APP_CERTIFICATE = process.env.APP_CERTIFICATE;
var app = express();
function nocache(req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}
var generateAccessToken = function (req, resp) {
resp.header('Access-Control-Allow-Origin', "*")
var channel = req.query.channel;
if (!channel) {
return resp.status(500).json({ 'error': 'channel name is required' });
}
var uid = req.query.uid;
if (!uid) {
uid = 0;
}
var expiredTs = req.query.expiredTs;
if (!expiredTs) {
expiredTs = 0;
}
var token = new Token(APP_ID, APP_CERTIFICATE, channel, uid);
// typically you will ONLY need join channel priviledge
token.addPriviledge(Priviledges.kJoinChannel, expiredTs);
return resp.json({ 'token': token.build() });
};
app.get('/access_token', nocache, generateAccessToken);
app.listen(PORT, function () {
console.log('Service URL http://127.0.0.1:' + PORT + "/");
console.log('Channel Key request, /access_token?uid=[user id]&channel=[channel name]');
console.log('Channel Key with expiring time request, /access_token?uid=[user id]&channel=[channel name]&expiredTs=[expire ts]');
});