-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
46 lines (39 loc) · 1.45 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
const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();
const port = process.env.PORT || 3000;
const gitbookSignKey = process.env.GITBOOK_JWT_KEY
const gitbookSpaceURL = process.env.GITBOOK_URL;
//
// Welcome page to simulate your application.
//
app.get('/', (req, res) => {
res.send(`
<p>Click <a href="/auth">here</a> to access the documentation !</p>
`)
});
//
// Page prompting the authentication of the user on the application side.
// This endpoint is the equivalent of your login page.
//
app.get('/auth', (req, res) => {
res.send(`
<p>Click <a href="/auth/confirm?location=${encodeURIComponent(req.query.location || '')}">here</a> to authenticate !</p>
`)
});
//
// Redirect to the documentation with the JWT token signed when auth has been "completed".
// The user session on your application should be checked here to validate that the user can access the documentation.
//
// ==> This endpoint is the fallback URL to configure on GitBook side.
// GitBook will redirect the visitor to this url when authentication is needed.
//
app.get('/auth/confirm', (req, res) => {
const token = jwt.sign({}, gitbookSignKey, { expiresIn: '1h' });
const uri = new URL(`${gitbookSpaceURL}${req.query.location || ''}`);
uri.searchParams.set('jwt_token', token)
res.redirect(uri.toString());
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});