-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
248 lines (209 loc) · 7.36 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Import Deps
require("dotenv").config();
const express = require("express");
const session = require("express-session");
const passport = require("passport");
const consolidate = require("consolidate");
const getUnixTimestamp = require("./helpers/getUnixTimestamp");
const bodyParser = require("body-parser");
const port = process.argv[2] || 8082;
/*
Create a .env file in the root directory of your project.
Add environment-specific variables on new lines in the form of NAME=VALUE. For example:
SALLA_OAUTH_CLIENT_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SALLA_OAUTH_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
...
*/
const {
SALLA_OAUTH_CLIENT_ID,
SALLA_OAUTH_CLIENT_SECRET,
SALLA_OAUTH_CLIENT_REDIRECT_URI,
SALLA_WEBHOOK_SECRET,
SALLA_DATABASE_ORM,
} = process.env;
// Import Salla APIs
const SallaAPIFactory = require("@salla.sa/passport-strategy");
const SallaDatabase = require("./database")(SALLA_DATABASE_ORM || "Sequelize");
const SallaWebhook = require("@salla.sa/webhooks-actions");
SallaWebhook.setSecret(SALLA_WEBHOOK_SECRET);
// Add Listeners
SallaWebhook.on("app.installed", (eventBody, userArgs) => {
// handel app.installed event
});
SallaWebhook.on("app.stroe.authorize", (eventBody, userArgs) => {
// handel app.installed event
});
SallaWebhook.on("all", (eventBody, userArgs) => {
// handel all events even thats not authorized
});
// we initialize our Salla API
const SallaAPI = new SallaAPIFactory({
clientID: SALLA_OAUTH_CLIENT_ID,
clientSecret: SALLA_OAUTH_CLIENT_SECRET,
callbackURL: SALLA_OAUTH_CLIENT_REDIRECT_URI,
});
// set Listener on auth success
SallaAPI.onAuth(async (accessToken, refreshToken, expires_in, data) => {
SallaDatabase.connect()
.then(async (connection) => {
let user_id = await SallaDatabase.saveUser({
username: data.name,
email: data.email,
email_verified_at: getUnixTimestamp(),
verified_at: getUnixTimestamp(),
password: "",
remember_token: "",
});
await SallaDatabase.saveOauth(
{
merchant: data.merchant.id,
access_token: accessToken,
expires_in: expires_in,
refresh_token: refreshToken,
user_id
},
);
})
.catch((err) => {
console.log("Error connecting to database: ", err);
});
});
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete salla user is serialized
// and deserialized.
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
// Use the Salla Strategy within Passport.
passport.use(SallaAPI.getPassportStrategy());
// save token and user data to your selected database
var app = express();
// configure Express
app.set("views", __dirname + "/views");
app.set("view engine", "html");
// set the session secret
// you can store session data in any database (monogdb - mysql - inmemory - etc) for more (https://www.npmjs.com/package/express-session)
app.use(
session({ secret: "keyboard cat", resave: true, saveUninitialized: true })
);
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
// serve static files from public folder
app.use(express.static(__dirname + "/public"));
// set the render engine to nunjucks
app.engine("html", consolidate.nunjucks);
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use((req, res, next) => SallaAPI.setExpressVerify(req, res, next));
// POST /webhook
app.post("/webhook", function (req, res) {
SallaWebhook.checkActions(req.body, req.headers.authorization, {
/* your args to pass to action files or listeners */
});
});
// GET /oauth/redirect
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in salla authentication will involve redirecting
// the user to accounts.salla.sa. After authorization, salla will redirect the user
// back to this application at /oauth/callback
app.get(["/oauth/redirect", "/login"], passport.authenticate("salla"));
// GET /oauth/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get(
"/oauth/callback",
passport.authenticate("salla", { failureRedirect: "/login" }),
function (req, res) {
res.redirect("/");
}
);
// GET /
// render the index page
app.get("/", async function (req, res) {
let userDetails = {
user: req.user,
isLogin: req.user
}
if (req.user){
const userFromDB = await SallaDatabase.retrieveUser({ email: req.user.email }, true);
const accessToken = userFromDB.oauthId.access_token;
const userFromAPI = await SallaAPI.getResourceOwner(accessToken);
// Merge user details with additional information from the API
userDetails = { ...userDetails, ...userFromAPI };
// mind you `req.user` content is almost the same as `user`,
// the main purpose of calling `await SallaAPI.getResourceOwner(access_token) `
// is to show how to make calls with the access_toke
}
res.render("index.html", userDetails);
});
// GET /account
// get account information and ensure user is authenticated
app.get("/account", ensureAuthenticated, function (req, res) {
res.render("account.html", {
user: req.user,
isLogin: req.user,
});
});
// GET /refreshToken
// get new access token
app.get("/refreshToken", ensureAuthenticated, function (req, res) {
SallaAPI.requestNewAccessToken(SallaAPI.getRefreshToken())
.then((token) => {
res.render("token.html", {
token,
isLogin: req.user,
});
})
.catch((err) => res.send(err));
});
// GET /orders
// get all orders from user store
app.get("/orders", ensureAuthenticated, async function (req, res) {
res.render("orders.html", {
orders: await SallaAPI.getAllOrders(),
isLogin: req.user,
});
});
// GET /customers
// get all customers from user store
app.get("/customers", ensureAuthenticated, async function (req, res) {
res.render("customers.html", {
customers: await SallaAPI.getAllCustomers(),
isLogin: req.user,
});
});
// GET /logout
// logout from passport
app.get("/logout", function (req, res) {
SallaAPI.logout();
req.logout(function(err) {
if (err) { return next(err); }
res.redirect("/");
});
});
app.listen(port, function () {
console.log("Listening on port " + port);
});
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/login");
}