-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocketAuth.js
116 lines (87 loc) · 2.72 KB
/
pocketAuth.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const express = require('express');
const fetch = require('node-fetch');
const { makeUrl } = require('./utils');
const app = express();
const POCKET_CONSUMER_KEY = '74412-a0426e3e632f7a2b98582457';
const POCKET_API = 'https://getpocket.com/v3/';
const POCKET_AUTH_URL = 'https://getpocket.com/auth/authorize';
const log = require('./log');
const commonBody = {
consumer_key: POCKET_CONSUMER_KEY,
};
const fetchHeaders = {
'Content-Type': 'application/json',
'X-Accept': 'application/json',
};
let code;
const doRequest = async (url, options) => {
options = {
method: 'POST',
headers: fetchHeaders,
...options,
};
if (options.body && typeof options.body !== 'string') {
options.body = JSON.stringify(options.body);
}
const response = await fetch(url, options)
.catch((reason) => {
log.error('doRequest', reason);
})
;
let error;
let data = await ((response.ok) ? response.json() : response.text());
if (!response.ok) {
error = response.headers['x-error'] || data;
}
return { response, data, error, ok: response.ok };
};
const pocketAuthCode = async (req, res) => {
const requesthUrl = `${POCKET_API}/oauth/request`;
const redirectUri = makeUrl(Object.assign({}, req, { pathname: 'pocket/callback' }));
const body = Object.assign({}, commonBody, { redirect_uri: redirectUri });
let { data, error } = await doRequest(requesthUrl, { body });
code = typeof data === 'object' ? data.code : null;
if (!code) {
error = `Response no have \`code\``;
}
if (error) {
const errorMsg = log.error('[pocketAuthCode]', error);
res.send(errorMsg.join(' '));
} else {
const url = `${POCKET_AUTH_URL}?request_token=${code}&redirect_uri=${redirectUri}`;
res.redirect(url);
}
return res;
};
const pocketAuthCallback = async (req, res) => {
const requesthUrl = `${POCKET_API}/oauth/authorize`;
const redirectUri = makeUrl(Object.assign({}, req, { pathname: 'callback' }));
const body = Object.assign({}, commonBody, {
code: code,
});
let { data, error } = await doRequest(requesthUrl, { body });
const { access_token, username } = typeof data === 'object' ? data : {};
if (!access_token) {
error = `Response no have \`access_token\``;
}
if (error) {
const errorMsg = log.error('[pocketAuthCallback]', error);
res.send(errorMsg.join(' '));
} else {
const data = { code, access_token, username, consumer_key: POCKET_CONSUMER_KEY };
log.success('to auth in pocket', data);
req.app.locals = {
...req.app.locals,
auth: {
...req.app.locals.auth,
pocket: data,
},
};
res.redirect('/');
}
return res;
}
module.exports = {
pocketAuthCode,
pocketAuthCallback,
};