beech-api 3.5.7 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +401 -186
- package/index.js +2 -3
- package/package.json +15 -8
- package/packages/cli/beech +2 -2
- package/packages/cli/bin/{beech.js → beech-app.js} +105 -43
- package/packages/cli/bin/beech-service.js +133 -0
- package/packages/cli/core/auth/Credentials.js +76 -35
- package/packages/cli/core/auth/Passport.js +318 -184
- package/packages/cli/core/auth/_Request.js +12 -0
- package/packages/cli/core/configure/_gitignore +9 -1
- package/packages/cli/core/configure/app.config-basic.js +10 -10
- package/packages/cli/core/configure/app.config-sequelize.js +17 -12
- package/packages/cli/core/configure/beech.config.js +8 -0
- package/packages/cli/core/configure/global.config-basic.js +4 -4
- package/packages/cli/core/configure/global.config-sequelize.js +4 -4
- package/packages/cli/core/configure/passport.config.js +5 -15
- package/packages/cli/core/databases/mysql.js +53 -29
- package/packages/cli/core/databases/sequelize.js +135 -105
- package/packages/cli/core/databases/test.js +168 -0
- package/packages/cli/core/file-walk/file-walk.js +31 -14
- package/packages/cli/core/generator/_add-on +15 -2
- package/packages/cli/core/generator/_endpoints +15 -82
- package/packages/cli/core/generator/_endpoints_basic +39 -0
- package/packages/cli/core/generator/_help +12 -11
- package/packages/cli/core/generator/_help_create +11 -0
- package/packages/cli/core/generator/_help_service +11 -0
- package/packages/cli/core/generator/_models +20 -15
- package/packages/cli/core/generator/{_basic-models → _models_basic} +0 -2
- package/packages/cli/core/generator/_package +5 -4
- package/packages/cli/core/generator/index.js +302 -95
- package/packages/cli/core/helpers/math.js +55 -0
- package/packages/cli/core/helpers/poolEntity.js +31 -0
- package/packages/cli/core/index.js +97 -28
- package/packages/cli/core/origin/index.js +2 -0
- package/packages/cli/core/origin/whitelist/cors.js +91 -0
- package/packages/cli/core/services/http.express.js +338 -201
- package/packages/cli/core/test/utils.js +5 -5
- package/packages/lib/index.js +4 -0
- package/packages/lib/src/endpoint.js +98 -0
- package/packages/lib/src/salt.js +3 -0
- package/packages/lib/src/schema.js +62 -0
- package/packages/lib/src/user.js +196 -0
- package/packages/package.json +4 -6
- package/packages/public/icon/beech_128.png +0 -0
- package/packages/cli/core/generator/_create +0 -7
- package/packages/lib/beech.js +0 -136
- package/packages/lib/salt.js +0 -3
- /package/packages/cli/core/generator/{_basic-helpers → _helpers_basic} +0 -0
|
@@ -10,13 +10,13 @@ module.exports = {
|
|
|
10
10
|
try {
|
|
11
11
|
// Create express server
|
|
12
12
|
const ExpressServer = _app_.listen(_config_.main_config.app_port, async () => {
|
|
13
|
-
await console.log('
|
|
14
|
-
await
|
|
15
|
-
await
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
await console.log('[102m[90m Passed [0m[0m Service is started at:');
|
|
14
|
+
await console.log('- [91mLocal[0m: [36mhttp://' + _config_.main_config.app_host + ':' + ExpressServer.address().port + '[0m');
|
|
15
|
+
await console.log('- [91mNetwork[0m: [36m' + _config_.main_config.client_host + '[0m');
|
|
16
|
+
await new Promise((resolve) => resolve(this.authentication()));
|
|
17
|
+
await new Promise((resolve) => resolve(this.addOn()));
|
|
18
|
+
await new Promise((resolve) => resolve(this.badRequest()));
|
|
19
|
+
await resolve(ExpressServer);
|
|
20
20
|
});
|
|
21
21
|
} catch (error) {
|
|
22
22
|
reject(error);
|
|
@@ -34,228 +34,365 @@ module.exports = {
|
|
|
34
34
|
status: "SUCCESS",
|
|
35
35
|
message: `Welcome to ${package.name} (version ${package.version})`
|
|
36
36
|
});
|
|
37
|
-
resolve(200);
|
|
38
37
|
});
|
|
39
38
|
// request 404 not found
|
|
40
39
|
_app_.use((req, res, next) => {
|
|
41
40
|
res.status(404).json({
|
|
42
41
|
code: 404,
|
|
43
|
-
status: "
|
|
44
|
-
|
|
42
|
+
status: "404_NOT_FOUND",
|
|
43
|
+
message: "The Endpoint not found!.",
|
|
45
44
|
});
|
|
46
|
-
resolve(404);
|
|
47
45
|
next();
|
|
48
46
|
});
|
|
47
|
+
// resolve it.
|
|
48
|
+
resolve(true);
|
|
49
49
|
} catch (error) {
|
|
50
50
|
reject(error);
|
|
51
51
|
}
|
|
52
52
|
});
|
|
53
53
|
},
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
54
|
+
addOn() {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
try {
|
|
57
|
+
// check add-on file exists ?
|
|
58
|
+
if (_config_.addOn) {
|
|
59
|
+
if (fs.existsSync(appRoot + "/src/Add-on.js")) {
|
|
60
|
+
console.log("- [91mAdd-On[0m: [93mON[0m");
|
|
61
|
+
let add_on = require(appRoot + "/src/Add-on.js");
|
|
62
|
+
if(add_on.init()) {
|
|
63
|
+
resolve(true);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
console.log("- [91mAdd-On[0m: [90mOFF[0m");
|
|
67
|
+
resolve(true);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
console.log("- [91mAdd-On[0m: [90mOFF[0m");
|
|
71
|
+
resolve(true);
|
|
67
72
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} else {
|
|
71
|
-
return;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
reject(error);
|
|
72
75
|
}
|
|
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
|
-
}
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
// Authentication request
|
|
79
|
+
authentication() {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
try {
|
|
82
|
+
var passport_config = null;
|
|
83
|
+
var jwt = null;
|
|
84
|
+
var passport = null;
|
|
85
|
+
var User = null;
|
|
86
|
+
var passport_config_file_exists = true;
|
|
87
|
+
var jwt_allow = false;
|
|
88
|
+
var jwt_db_allow = null;
|
|
89
|
+
// First promise
|
|
90
|
+
const checkPassport = new Promise((resolve, reject) => {
|
|
91
|
+
// require jwt & passport
|
|
92
|
+
if (fs.existsSync(appRoot + passport_config_file)) {
|
|
93
|
+
try {
|
|
94
|
+
// assign prepare data
|
|
95
|
+
passport_config = require(appRoot + passport_config_file);
|
|
96
|
+
global._passport_config_ = passport_config;
|
|
97
|
+
jwt = require('jsonwebtoken');
|
|
98
|
+
passport = require('passport');
|
|
99
|
+
User = require("../../../lib/src/user");
|
|
100
|
+
if (!passport_config.jwt_allow) {
|
|
101
|
+
// jwt is false
|
|
102
|
+
console.log("- [91mJWT[0m: [90mOFF[0m");
|
|
103
|
+
resolve(true);
|
|
102
104
|
} else {
|
|
103
|
-
|
|
105
|
+
/// jwt is true
|
|
106
|
+
jwt_allow = true;
|
|
107
|
+
console.log("- [91mJWT[0m: [93mON[0m");
|
|
108
|
+
// loop check db connect is true
|
|
109
|
+
fs.readFile("./app.config.js", "utf-8", (err, data) => {
|
|
110
|
+
if(err) {
|
|
111
|
+
throw err;
|
|
112
|
+
} else {
|
|
113
|
+
let mineConfDb = eval(data).database_config;
|
|
114
|
+
mineConfDb.filter((e) => {
|
|
115
|
+
if(e.name == passport_config.model.name) {
|
|
116
|
+
jwt_db_allow = e.is_connect;
|
|
117
|
+
resolve(jwt_db_allow);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
});
|
|
104
122
|
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
code: 200,
|
|
108
|
-
status: "AUTHORIZED",
|
|
109
|
-
message: "success.",
|
|
110
|
-
user,
|
|
111
|
-
accessToken
|
|
112
|
-
});
|
|
123
|
+
} catch (error) {
|
|
124
|
+
reject(error);
|
|
113
125
|
}
|
|
114
|
-
} else if (opt) {
|
|
115
|
-
res.status(422).json({ code: 422, message: "Unprocessable Entity." });
|
|
116
|
-
} else {
|
|
117
|
-
res.status(401).json({ code: 401, message: "Unauthorized." });
|
|
118
|
-
}
|
|
119
|
-
})(req, res, next);
|
|
120
|
-
});
|
|
121
|
-
// create users endpoints
|
|
122
|
-
_app_.post(auth_endpoint + '/users', (req, res) => {
|
|
123
|
-
Beech.store(req.body, (err, result) => {
|
|
124
|
-
if (err) {
|
|
125
|
-
res.status(500).json({ code: 500, status: "CREATE_FAILED", error: err });
|
|
126
126
|
} else {
|
|
127
|
-
|
|
127
|
+
passport_config_file_exists = false;
|
|
128
|
+
resolve(true);
|
|
128
129
|
}
|
|
129
130
|
});
|
|
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
|
-
|
|
131
|
+
|
|
132
|
+
// Second promise
|
|
133
|
+
const signJWT = new Promise((resolve, reject) => {
|
|
134
|
+
try {
|
|
135
|
+
checkPassport.then(passportChecked => {
|
|
136
|
+
if(passportChecked) {
|
|
137
|
+
if (passport_config_file_exists && jwt_allow && jwt_db_allow) {
|
|
138
|
+
// declare authentication endpoint name
|
|
139
|
+
const auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[ 0 ] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
140
|
+
// authentication endpoints
|
|
141
|
+
_app_.post(auth_endpoint, (req, res, next) => {
|
|
142
|
+
passport.authenticate('local', { session: false }, (err, user, opt) => {
|
|
143
|
+
if (err) {
|
|
144
|
+
res.status(502).json({
|
|
145
|
+
code: 502,
|
|
146
|
+
status: 'BAD_GATEWAY',
|
|
147
|
+
message: err
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (user) {
|
|
151
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
152
|
+
expiresIn: passport_config.token_expired
|
|
153
|
+
});
|
|
154
|
+
if (passport_config.app_key_allow) {
|
|
155
|
+
if (req.headers.app_key) {
|
|
156
|
+
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
157
|
+
res.status(200).json({
|
|
158
|
+
code: 200,
|
|
159
|
+
status: "AUTHORIZED",
|
|
160
|
+
user,
|
|
161
|
+
accessToken
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
res.status(401).json({ code: 401, message: "Unauthorized with wrong key." });
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
res.status(422).json({ code: 422, message: "Unprocessable Entity." });
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
res.status(200).json({
|
|
171
|
+
code: 200,
|
|
172
|
+
status: "AUTHORIZED",
|
|
173
|
+
user,
|
|
174
|
+
accessToken
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
} else if (opt) {
|
|
178
|
+
res.status(422).json({ code: 422, message: "Unprocessable Entity." });
|
|
179
|
+
} else {
|
|
180
|
+
res.status(401).json({ code: 401, message: "Unauthorized." });
|
|
181
|
+
}
|
|
182
|
+
})(req, res, next);
|
|
183
|
+
});
|
|
184
|
+
// create auth data endpoints
|
|
185
|
+
_app_.post(auth_endpoint + '/create', (req, res) => {
|
|
186
|
+
const promise = new Promise((resolve) => {
|
|
187
|
+
if (passport_config.app_key_allow) {
|
|
188
|
+
if (req.headers.app_key) {
|
|
189
|
+
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
190
|
+
resolve(true);
|
|
191
|
+
} else {
|
|
192
|
+
res.status(401).json({ code: 401, message: "Unauthorized with wrong key." });
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
res.status(422).json({ code: 422, message: "Unprocessable Entity." });
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
resolve(true);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
// store data
|
|
202
|
+
Promise.all([promise])
|
|
203
|
+
.then((secret) => {
|
|
204
|
+
if(secret) {
|
|
205
|
+
User.Store(req.body, (err, result) => {
|
|
206
|
+
if (err) {
|
|
207
|
+
res.status(501).json({ code: 501, status: "CREATE_FAILED", error: err });
|
|
208
|
+
} else {
|
|
209
|
+
res.status(201).json({ code: 201, status: "CREATE_SUCCESS", result });
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
} else {
|
|
213
|
+
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED" });
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
.catch(err => {
|
|
217
|
+
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED", error: err
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
// patch auth data endpoints
|
|
222
|
+
_app_.patch(auth_endpoint + '/update/:id', auth.credentials, (req, res) => {
|
|
223
|
+
const promise = new Promise((resolve) => {
|
|
224
|
+
if (passport_config.app_key_allow) {
|
|
225
|
+
if (req.headers.app_key) {
|
|
226
|
+
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
227
|
+
resolve(true);
|
|
228
|
+
} else {
|
|
229
|
+
res.status(401).json({ code: 401, message: "Unauthorized with wrong key." });
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
res.status(422).json({ code: 422, message: "Unprocessable Entity." });
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
resolve(true);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
// update data
|
|
239
|
+
Promise.all([promise])
|
|
240
|
+
.then((secret) => {
|
|
241
|
+
if(secret) {
|
|
242
|
+
// require some fields with body params
|
|
243
|
+
User.Update(req.body, req.params.id, (err, result) => {
|
|
244
|
+
if (err) {
|
|
245
|
+
res.status(501).json({ code: 501, status: "UPDATE_FAILED", error: err });
|
|
246
|
+
} else {
|
|
247
|
+
res.status(200).json({ code: 200, status: "UPDATE_SUCCESS", result });
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED" });
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
.catch(err => {
|
|
255
|
+
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED", error: err
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
/**
|
|
260
|
+
* Google Strategy
|
|
261
|
+
*
|
|
262
|
+
*/
|
|
263
|
+
if (passport_config.strategy.google.allow) {
|
|
264
|
+
_app_.get(auth_endpoint + '/google', passport.authenticate('google', {
|
|
265
|
+
scope: [
|
|
266
|
+
'https://www.googleapis.com/auth/userinfo.email',
|
|
267
|
+
'https://www.googleapis.com/auth/plus.login'
|
|
268
|
+
]
|
|
269
|
+
}));
|
|
270
|
+
// google auth callback
|
|
271
|
+
const googleCallback = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[ 0 ] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
272
|
+
_app_.get(auth_endpoint + googleCallback, passport.authenticate('google', { failureRedirect: passport_config.strategy.google.failureRedirect, failureMessage: true }), (req, res) => {
|
|
273
|
+
if (typeof req.user.user !== 'undefined') {
|
|
274
|
+
// declare user for sign JWT
|
|
275
|
+
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
276
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
277
|
+
expiresIn: passport_config.token_expired
|
|
278
|
+
});
|
|
279
|
+
// response JWT
|
|
280
|
+
res.status(200).json({
|
|
281
|
+
code: 200,
|
|
282
|
+
status: "AUTHORIZED",
|
|
283
|
+
user: req.user,
|
|
284
|
+
accessToken
|
|
285
|
+
});
|
|
286
|
+
} else {
|
|
287
|
+
let condUser = {};
|
|
288
|
+
condUser[ (passport_config.strategy.google.local_profile_fields.google_id) ? passport_config.strategy.google.local_profile_fields.google_id : "google_id" ] = req.user.google.id;
|
|
289
|
+
User.FindOne([], condUser, (err, result) => {
|
|
290
|
+
if (err) {
|
|
291
|
+
res.status(500).json({
|
|
292
|
+
code: 500,
|
|
293
|
+
status: "INTERNAL_SERVER_ERR",
|
|
294
|
+
error: err
|
|
295
|
+
});
|
|
296
|
+
} else {
|
|
297
|
+
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
298
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
299
|
+
expiresIn: passport_config.token_expired
|
|
300
|
+
});
|
|
301
|
+
// response JWT
|
|
302
|
+
res.status(201).json({
|
|
303
|
+
code: 201,
|
|
304
|
+
status: "AUTHORIZED",
|
|
305
|
+
user: {
|
|
306
|
+
google: req.user.google,
|
|
307
|
+
user
|
|
308
|
+
},
|
|
309
|
+
accessToken
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Facebook strategy
|
|
318
|
+
*
|
|
319
|
+
*/
|
|
320
|
+
if (passport_config.strategy.facebook.allow) {
|
|
321
|
+
_app_.get(auth_endpoint + '/facebook', passport.authenticate('facebook', { scope: [ 'email', 'public_profile' ] }));
|
|
322
|
+
// facebook callback
|
|
323
|
+
const facebookCallback = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[ 0 ] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
324
|
+
_app_.get(auth_endpoint + facebookCallback, passport.authenticate('facebook', { failureRedirect: passport_config.strategy.facebook.failureRedirect, failureMessage: true }), (req, res) => {
|
|
325
|
+
if (typeof req.user.user !== 'undefined') {
|
|
326
|
+
// declare user for sign JWT
|
|
327
|
+
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
328
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
329
|
+
expiresIn: passport_config.token_expired
|
|
330
|
+
});
|
|
331
|
+
// response JWT
|
|
332
|
+
res.status(200).json({
|
|
333
|
+
code: 200,
|
|
334
|
+
status: "AUTHORIZED",
|
|
335
|
+
user: req.user,
|
|
336
|
+
accessToken
|
|
337
|
+
});
|
|
338
|
+
} else {
|
|
339
|
+
let condUser = {};
|
|
340
|
+
condUser[ (passport_config.strategy.facebook.local_profile_fields.facebook_id) ? passport_config.strategy.facebook.local_profile_fields.facebook_id : "facebook_id" ] = req.user.facebook.id;
|
|
341
|
+
User.FindOne([], condUser, (err, result) => {
|
|
342
|
+
if (err) {
|
|
343
|
+
res.status(500).json({
|
|
344
|
+
code: 500,
|
|
345
|
+
status: "INTERNAL_SERVER_ERR",
|
|
346
|
+
error: err
|
|
347
|
+
});
|
|
348
|
+
} else {
|
|
349
|
+
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
350
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
351
|
+
expiresIn: passport_config.token_expired
|
|
352
|
+
});
|
|
353
|
+
// response JWT
|
|
354
|
+
res.status(201).json({
|
|
355
|
+
code: 201,
|
|
356
|
+
status: "AUTHORIZED",
|
|
357
|
+
user: {
|
|
358
|
+
facebook: req.user.facebook,
|
|
359
|
+
user
|
|
360
|
+
},
|
|
361
|
+
accessToken
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
// Perfectly and resolve it.
|
|
369
|
+
resolve(true);
|
|
370
|
+
} else {
|
|
371
|
+
// Perfectly and resolve it.
|
|
372
|
+
resolve(false);
|
|
373
|
+
}
|
|
181
374
|
} else {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
expiresIn: passport_config.token_expired
|
|
185
|
-
});
|
|
186
|
-
// response JWT
|
|
187
|
-
res.status(201).json({
|
|
188
|
-
code: 201,
|
|
189
|
-
status: "AUTHORIZED",
|
|
190
|
-
message: "success.",
|
|
191
|
-
user: {
|
|
192
|
-
google: req.user.google,
|
|
193
|
-
user
|
|
194
|
-
},
|
|
195
|
-
accessToken
|
|
196
|
-
});
|
|
375
|
+
// checkPassport is Catch.
|
|
376
|
+
resolve(false);
|
|
197
377
|
}
|
|
198
378
|
});
|
|
379
|
+
} catch (error) {
|
|
380
|
+
reject(error);
|
|
199
381
|
}
|
|
200
382
|
});
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (passport_config.strategy.facebook.allow) {
|
|
207
|
-
_app_.get(auth_endpoint + '/facebook', passport.authenticate('facebook', { scope: ['email', 'pages_show_list'] }));
|
|
208
|
-
// facebook callback
|
|
209
|
-
const facebookCallback = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[0] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
210
|
-
_app_.get(auth_endpoint + facebookCallback, passport.authenticate('facebook', { failureRedirect: passport_config.strategy.facebook.failureRedirect, failureMessage: true }), (req, res) => {
|
|
211
|
-
if (typeof req.user.user !== 'undefined') {
|
|
212
|
-
// declare user for sign JWT
|
|
213
|
-
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
214
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
215
|
-
expiresIn: passport_config.token_expired
|
|
216
|
-
});
|
|
217
|
-
// response JWT
|
|
218
|
-
res.status(200).json({
|
|
219
|
-
code: 200,
|
|
220
|
-
status: "AUTHORIZED",
|
|
221
|
-
message: "success.",
|
|
222
|
-
user: req.user,
|
|
223
|
-
accessToken
|
|
224
|
-
});
|
|
383
|
+
// Promise all
|
|
384
|
+
Promise.all([ signJWT ]).then(async (final) => {
|
|
385
|
+
// [signJWT=init auth endpoint]
|
|
386
|
+
if(final[0]) {
|
|
387
|
+
resolve(true);
|
|
225
388
|
} else {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
Beech.findOne([], condUser, (err, result) => {
|
|
229
|
-
if (err) {
|
|
230
|
-
res.status(500).json({
|
|
231
|
-
code: 500,
|
|
232
|
-
status: "INTERNAL_SERVER_ERR",
|
|
233
|
-
message: "Internal server error.",
|
|
234
|
-
error: err
|
|
235
|
-
});
|
|
236
|
-
} else {
|
|
237
|
-
let user = JSON.parse(JSON.stringify(result[0]));
|
|
238
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
239
|
-
expiresIn: passport_config.token_expired
|
|
240
|
-
});
|
|
241
|
-
// response JWT
|
|
242
|
-
res.status(201).json({
|
|
243
|
-
code: 201,
|
|
244
|
-
status: "AUTHORIZED",
|
|
245
|
-
message: "success.",
|
|
246
|
-
user: {
|
|
247
|
-
facebook: req.user.facebook,
|
|
248
|
-
user
|
|
249
|
-
},
|
|
250
|
-
accessToken
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
});
|
|
389
|
+
// resolve false to JWT turn OFF
|
|
390
|
+
resolve(false);
|
|
254
391
|
}
|
|
255
|
-
});
|
|
392
|
+
}).catch((err) => reject(err));
|
|
393
|
+
} catch (error) {
|
|
394
|
+
reject(error);
|
|
256
395
|
}
|
|
257
|
-
}
|
|
258
|
-
throw error;
|
|
259
|
-
}
|
|
396
|
+
});
|
|
260
397
|
}
|
|
261
398
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
process.env.NODE_ENV =
|
|
1
|
+
process.env.NODE_ENV = "test";
|
|
2
2
|
process.setMaxListeners(0);
|
|
3
|
-
global.axios = require(
|
|
4
|
-
const path = require(
|
|
3
|
+
global.axios = require("axios");
|
|
4
|
+
const path = require("path");
|
|
5
5
|
const basePath = path.resolve();
|
|
6
|
-
const config = require(path.join(basePath,
|
|
7
|
-
global.baseUrl =
|
|
6
|
+
const config = require(path.join(basePath, "app.config")).main_config;
|
|
7
|
+
global.baseUrl = "http://" + config.app_host.concat(":" + config.app_port);
|