Skip to content

Commit 80158c1

Browse files
authored
Merge pull request #11 from fbsamples/feature-use-me
Replace user_id with me keyword
2 parents b1eead0 + f9a050d commit 80158c1

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ app.use(
109109

110110
// Middleware to ensure the user is logged in
111111
const loggedInUserChecker = (req, res, next) => {
112-
if (req.session.access_token && req.session.user_id) {
112+
if (req.session.access_token) {
113113
next();
114114
} else if (initial_access_token && initial_user_id) {
115115
useInitialAuthenticationValues(req);
@@ -121,7 +121,7 @@ const loggedInUserChecker = (req, res, next) => {
121121
};
122122

123123
app.get('/', async (req, res) => {
124-
if (!(req.session.access_token || req.session.user_id) &&
124+
if (!(req.session.access_token) &&
125125
(initial_access_token && initial_user_id)) {
126126
useInitialAuthenticationValues(req);
127127
res.redirect('/account');
@@ -163,7 +163,6 @@ app.get('/callback', async (req, res) => {
163163
},
164164
});
165165
req.session.access_token = response.data.access_token;
166-
req.session.user_id = response.data.user_id;
167166
res.redirect('/account');
168167
} catch (err) {
169168
console.error(err?.response?.data);
@@ -186,6 +185,11 @@ app.get('/account', loggedInUserChecker, async (req, res) => {
186185
try {
187186
const response = await axios.get(getUserDetailsUrl);
188187
userDetails = response.data;
188+
189+
// This value is not currently used but it may come handy in the future
190+
if (!req.session.user_id)
191+
req.session.user_id = response.data.id;
192+
189193
userDetails.user_profile_url = `https://www.threads.net/@${userDetails.username}`;
190194
} catch (e) {
191195
console.error(e);
@@ -217,7 +221,7 @@ app.get('/userInsights', loggedInUserChecker, async (req, res) => {
217221
params.until = until;
218222
}
219223

220-
const queryThreadUrl = buildGraphAPIURL(`${req.session.user_id}/threads_insights`, params, req.session.access_token);
224+
const queryThreadUrl = buildGraphAPIURL(`me/threads_insights`, params, req.session.access_token);
221225

222226
let data = [];
223227
try {
@@ -258,7 +262,7 @@ app.get('/publishingLimit', loggedInUserChecker, async (req, res) => {
258262
].join(','),
259263
};
260264

261-
const publishingLimitUrl = buildGraphAPIURL(`${req.session.user_id}/threads_publishing_limit`, params, req.session.access_token);
265+
const publishingLimitUrl = buildGraphAPIURL(`me/threads_publishing_limit`, params, req.session.access_token);
262266

263267
let data = [];
264268
try {
@@ -325,7 +329,7 @@ app.post('/upload', upload.array(), async (req, res) => {
325329
if (params.media_type === MEDIA_TYPE__CAROUSEL) {
326330
const createChildPromises = params.children.map(child => (
327331
axios.post(
328-
buildGraphAPIURL(`${req.session.user_id}/threads`, child, req.session.access_token),
332+
buildGraphAPIURL(`me/threads`, child, req.session.access_token),
329333
{},
330334
)
331335
));
@@ -346,7 +350,7 @@ app.post('/upload', upload.array(), async (req, res) => {
346350
}
347351
}
348352

349-
const postThreadsUrl = buildGraphAPIURL(`${req.session.user_id}/threads`, params, req.session.access_token);
353+
const postThreadsUrl = buildGraphAPIURL(`me/threads`, params, req.session.access_token);
350354
try {
351355
const postResponse = await axios.post(postThreadsUrl, {});
352356
const containerId = postResponse.data.id;
@@ -394,7 +398,7 @@ app.get('/container/status/:containerId', loggedInUserChecker, async (req, res)
394398

395399
app.post('/publish', upload.array(), async (req, res) => {
396400
const { containerId } = req.body;
397-
const publishThreadsUrl = buildGraphAPIURL(`${req.session.user_id}/threads_publish`, {
401+
const publishThreadsUrl = buildGraphAPIURL(`me/threads_publish`, {
398402
creation_id: containerId,
399403
}, req.session.access_token);
400404

@@ -467,7 +471,7 @@ app.get('/threads', loggedInUserChecker, async (req, res) => {
467471
let threads = [];
468472
let paging = {};
469473

470-
const queryThreadsUrl = buildGraphAPIURL(`${req.session.user_id}/threads`, params, req.session.access_token);
474+
const queryThreadsUrl = buildGraphAPIURL(`me/threads`, params, req.session.access_token);
471475

472476
try {
473477
const queryResponse = await axios.get(queryThreadsUrl);

0 commit comments

Comments
 (0)