Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (86 loc) · 2.97 KB
/
Copy pathserver.js
File metadata and controls
101 lines (86 loc) · 2.97 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
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
import dotenv from "dotenv";
dotenv.config({ path: '.env.credentials' })
import express from "express"
import cors from "cors"
import bodyParser from "body-parser";
import '@shopify/shopify-api/adapters/node';
import { shopifyApi, ApiVersion, Session } from "@shopify/shopify-api";
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// SHOPIFY OAUTH SETUP
const shopify = shopifyApi({
apiKey: process.env.SHOPIFY_API_KEY,
apiSecretKey: process.env.SHOPIFY_API_SECRET,
scopes: process.env.SCOPES.split(","),
apiVersion: ApiVersion.April26,
hostName: 'localhost',
isEmbeddedApp: false
});
app.get('/api/shop/products', async (req, res) => {
// GETTING PRODUCTS WITH MANUAL SESSION AND ACCESS TOKEN
// YOU JUST NEET @SHOPIFY/SHOPIFY-API PACKAGE AND SHOPIFYAPI OBJECT
try {
const session = new Session({
id: 'offline_session_id',
shop: new URL(process.env.SHOPIFY_STORE_URL).hostname,
state: 'state',
isOnline: false,
accessToken: process.env.SHOPIFY_ACCESS_TOKEN
});
const client = new shopify.clients.Rest({ session });
const response = await client.get({
path: 'products',
});
console.log(response.body.products);
} catch(error) {
console.log(error)
}
});
// THIS METHOD IS FOR BROWSER BASED AUTHENTICATION
// import { shopifyApp } from "@shopify/shopify-app-express";
// import { MemorySessionStorage } from "@shopify/shopify-app-session-storage-memory";
// import { ApiVersion } from "@shopify/shopify-api";
// app.use("/api/auth", shopify.auth.begin());
// app.get(
// "/api/auth/callback",
// shopify.auth.callback(),
// (req, res) => {
// res.redirect("/");
// }
// );
// const shopify = shopifyApp({
// api: {
// apiKey: process.env.SHOPIFY_API_KEY,
// apiSecretKey: process.env.SHOPIFY_API_SECRET,
// scopes: process.env.SCOPES.split(","),
// apiVersion: ApiVersion.April25,
// hostName: 'localhost:3000',
// isEmbeddedApp: false,
// },
// auth: {
// path: '/api/auth',
// callbackPath: '/api/auth/callback',
// },
// sessionStorage: new MemorySessionStorage(),
// });
// app.get("/api/shop/products", shopify.validateAuthenticatedSession(), async(req, res) => {
// try {
// const session = res.locals.shopify.session;
// console.log("session", session)
// const client = new shopify.api.clients.Rest({ session });
// const response = await client.get({
// path: "products"
// });
// console.log(response.body.products)
// res.json(response.body.products)
// } catch(error) {
// console.error(error);
// res.status(500).json({ error: error.message });
// }
// });
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});