-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathisPioneerFound.ts
39 lines (34 loc) · 1.29 KB
/
isPioneerFound.ts
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
import { NextFunction, Request, Response } from "express";
import { platformAPIClient } from "../config/platformAPIclient";
import logger from '../config/loggingConfig';
export const isPioneerFound = async (
req: Request,
res: Response,
next: NextFunction
) => {
const authHeader = req.headers.authorization;
const tokenFromHeader = authHeader && authHeader.split(" ")[1];
try {
logger.info("Verifying user's access token with the /me endpoint.");
// Verify the user's access token with the /me endpoint:
const me = await platformAPIClient.get(`/v2/me`, {
headers: { 'Authorization': `Bearer ${ tokenFromHeader }` }
});
if (me && me.data) {
const user = {
pi_uid: me.data.uid,
pi_username: me.data.username,
user_name: me.data.username
}
req.body.user = user;
logger.info(`Pioneer found: ${user.pi_uid} - ${user.pi_username}`);
return next();
} else {
logger.warn("Pioneer not found.");
return res.status(404).json({message: "Pioneer not found"});
}
} catch (error) {
logger.error('Failed to identify pioneer:', error);
res.status(500).json({ message: 'Failed to identify | pioneer not found; please try again later'});
}
};