-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared.ts
98 lines (89 loc) · 2.77 KB
/
shared.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
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
import fetchApi from "./fetchApi";
import * as jwt from "jsonwebtoken";
import type { NextApiRequest, NextApiResponse } from "next";
import {
ShopAndBearerHeaders,
TokenPayloadResponse,
} from "./model/responses.model";
const getAccessTokenFromDB = async (
sessionToken: string
): Promise<string | null> => {
console.log("getAccessTokenFromDB");
try {
const response: TokenPayloadResponse = await fetchApi({
method: "get",
url: `${process.env.RM_SERVER_URL}/shopify/access_token`,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${sessionToken}`,
},
});
if (!response || !response.success! || !response.accessToken) {
console.log("accessTokenResponse error", response?.message);
return null;
}
return response.accessToken;
} catch (err) {
console.log("err", err);
return null;
}
};
const getShopFromBearerHeader = async (
req: NextApiRequest
): Promise<string> => {
console.log("getShopFromBearerHeader");
return new Promise((resolve, reject) => {
try {
if (!req.headers.authorization) {
throw "malformed header";
}
const encrypted = req.headers.authorization.replace("Bearer ", "");
// console.log(secret, process.env.TOKEN_KEY);
jwt.verify(
encrypted,
process.env.SHOPIFY_API_SECRET,
(err, decoded: any) => {
// console.log('err', err);
// console.log('decoded', decoded);
console.log("decoded", decoded);
console.log("exp", new Date(decoded.exp));
console.log("nbf", new Date(decoded.nbf));
console.log("dest", decoded.dest);
const shop = decoded.dest.replace("https://", "");
return resolve(shop);
}
);
//console.log('flo20DecodedToken: ',decodedToken);
} catch (err) {
console.log("getShopFromBearerHeader error", err);
return resolve(""); // will be seen as an error
}
});
};
const getShopAndBearerHeaders = async (
req: NextApiRequest
): Promise<ShopAndBearerHeaders> => {
console.log("getShopAndBearerHeaders");
const shop = await getShopFromBearerHeader(req);
if (!shop) {
console.log("no shop");
throw { success: false, message: "no shop" };
}
console.log("shop response", shop);
const sessionToken = req.headers.authorization.replace("Bearer ", "");
const accessToken = await getAccessTokenFromDB(sessionToken);
if (!accessToken) {
throw { success: false, message: "accessToken value null" };
}
console.log("accessToken response", accessToken);
const headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": accessToken,
};
return { shop, headers };
};
export {
getAccessTokenFromDB,
getShopFromBearerHeader,
getShopAndBearerHeaders,
};