-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (58 loc) · 1.78 KB
/
Copy pathapp.js
File metadata and controls
69 lines (58 loc) · 1.78 KB
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
const express = require("express");
const { ZeplinApi, Configuration } = require("@zeplin/sdk");
const PORT = 3000;
const CLIENT_ID = "CLIENT_ID";
const CLIENT_SECRET = "CLIENT_SECRET";
const REDIRECT_URI = "http://localhost:3000/oauth/callback";
const app = express();
function htmlPage(content) {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Zeplin app</title>
</head>
<body>
${content}
</body>
</html>`
}
app.get("/", (req, res) => {
res.set("Content-Type", "text/html");
res.send(
htmlPage(`<p>
Heyo Zepliner! 🙋<br><br>
Click <a href="/oauth/authorize">here</a> to authorize this app.
</p>`)
);
});
app.get("/oauth/authorize", (req, res) => {
const zeplinClient = new ZeplinApi();
res.redirect(zeplinClient.authorization.getAuthorizationUrl({
clientId: CLIENT_ID,
redirectUri: REDIRECT_URI
}));
});
app.get("/oauth/callback", async (req, res) => {
const authCode = req.query.code;
let zeplinClient = new ZeplinApi();
// Create an access token using the authorization code
const { data: tokenData } = await zeplinClient.authorization.createToken({
code: authCode,
clientId: CLIENT_ID,
redirectUri: REDIRECT_URI,
clientSecret: CLIENT_SECRET
});
// Get current user's details
zeplinClient = new ZeplinApi(new Configuration({ accessToken: tokenData.accessToken }))
const { data: userData } = await zeplinClient.users.getCurrentUser();
res.set("Content-Type", "text/html");
res.send(
htmlPage(`<p>
Welcome back, <strong>${userData.username}</strong>! 🙋
</p>`)
);
});
app.listen(PORT, () => {
console.log(`App listening at http://localhost:${PORT}`);
});