beech-api 3.5.12 → 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 +400 -185
- package/index.js +2 -3
- package/package.json +9 -6
- package/packages/cli/beech +2 -2
- package/packages/cli/bin/{beech.js → beech-app.js} +86 -28
- package/packages/cli/bin/beech-service.js +40 -212
- package/packages/cli/core/auth/Credentials.js +76 -35
- package/packages/cli/core/auth/Passport.js +318 -188
- 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 +12 -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 -107
- 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/{_service → _help_service} +4 -1
- 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 +4 -3
- 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 +96 -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 -203
- 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 -7
- package/packages/cli/core/generator/_create +0 -7
- package/packages/lib/beech.js +0 -136
- package/packages/lib/salt.js +0 -3
- package/packages/src-/Add-on.js +0 -9
- package/packages/src-/endpoints/hello-endpoints.js +0 -118
- package/packages/src-/endpoints/test2-endpoints.js +0 -75
- package/packages/src-/endpoints/testSequalize-endpoints.js +0 -23
- package/packages/src-/helpers/Test2.js +0 -11
- package/packages/src-/helpers/my/Test.js +0 -11
- package/packages/src-/helpers/my/Test2.js +0 -11
- package/packages/src-/helpers/my/Test3.js +0 -11
- package/packages/src-/helpers/my/Test4.js +0 -11
- package/packages/src-/models/Jubu.js +0 -29
- package/packages/src-/models/Jubu2.js +0 -20
- package/packages/src-/models/Map_master.js +0 -22
- package/packages/src-/models/Test.js +0 -29
- package/packages/src-/models/Test2.js +0 -29
- package/packages/src-/models/User2Sequelize.js +0 -23
- package/packages/src-/models/Users-sqlite.js +0 -21
- package/packages/src-/models/Users.js +0 -53
- package/packages/src-/models/Uuuuuxxx.js +0 -23
- package/packages/src-/models/xxx/Uuuuuxxx.js +0 -23
- /package/packages/cli/core/generator/{_basic-helpers → _helpers_basic} +0 -0
|
@@ -10,15 +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
|
-
console.log('[102m[90m Passed [0m[0m Service is started at:');
|
|
14
|
-
console.log('- [91mLocal[0m: [36mhttp://' + _config_.main_config.app_host + ':' + ExpressServer.address().port + '[0m');
|
|
15
|
-
console.log('- [91mNetwork[0m: [36m' + _config_.main_config.client_host + '[0m');
|
|
16
|
-
this.authentication();
|
|
17
|
-
await this.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
reject(err);
|
|
21
|
-
});
|
|
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);
|
|
22
20
|
});
|
|
23
21
|
} catch (error) {
|
|
24
22
|
reject(error);
|
|
@@ -36,228 +34,365 @@ module.exports = {
|
|
|
36
34
|
status: "SUCCESS",
|
|
37
35
|
message: `Welcome to ${package.name} (version ${package.version})`
|
|
38
36
|
});
|
|
39
|
-
resolve(200);
|
|
40
37
|
});
|
|
41
38
|
// request 404 not found
|
|
42
39
|
_app_.use((req, res, next) => {
|
|
43
40
|
res.status(404).json({
|
|
44
41
|
code: 404,
|
|
45
|
-
status: "
|
|
46
|
-
|
|
42
|
+
status: "404_NOT_FOUND",
|
|
43
|
+
message: "The Endpoint not found!.",
|
|
47
44
|
});
|
|
48
|
-
resolve(404);
|
|
49
45
|
next();
|
|
50
46
|
});
|
|
47
|
+
// resolve it.
|
|
48
|
+
resolve(true);
|
|
51
49
|
} catch (error) {
|
|
52
50
|
reject(error);
|
|
53
51
|
}
|
|
54
52
|
});
|
|
55
53
|
},
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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);
|
|
69
72
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
} else {
|
|
73
|
-
return;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
reject(error);
|
|
74
75
|
}
|
|
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
|
-
}
|
|
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);
|
|
104
104
|
} else {
|
|
105
|
-
|
|
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
|
+
});
|
|
106
122
|
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
code: 200,
|
|
110
|
-
status: "AUTHORIZED",
|
|
111
|
-
message: "success.",
|
|
112
|
-
user,
|
|
113
|
-
accessToken
|
|
114
|
-
});
|
|
123
|
+
} catch (error) {
|
|
124
|
+
reject(error);
|
|
115
125
|
}
|
|
116
|
-
} else if (opt) {
|
|
117
|
-
res.status(422).json({ code: 422, message: "Unprocessable Entity." });
|
|
118
|
-
} else {
|
|
119
|
-
res.status(401).json({ code: 401, message: "Unauthorized." });
|
|
120
|
-
}
|
|
121
|
-
})(req, res, next);
|
|
122
|
-
});
|
|
123
|
-
// create users endpoints
|
|
124
|
-
_app_.post(auth_endpoint + '/users', (req, res) => {
|
|
125
|
-
Beech.store(req.body, (err, result) => {
|
|
126
|
-
if (err) {
|
|
127
|
-
res.status(500).json({ code: 500, status: "CREATE_FAILED", error: err });
|
|
128
126
|
} else {
|
|
129
|
-
|
|
127
|
+
passport_config_file_exists = false;
|
|
128
|
+
resolve(true);
|
|
130
129
|
}
|
|
131
130
|
});
|
|
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
|
-
|
|
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
|
+
}
|
|
183
374
|
} else {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
expiresIn: passport_config.token_expired
|
|
187
|
-
});
|
|
188
|
-
// response JWT
|
|
189
|
-
res.status(201).json({
|
|
190
|
-
code: 201,
|
|
191
|
-
status: "AUTHORIZED",
|
|
192
|
-
message: "success.",
|
|
193
|
-
user: {
|
|
194
|
-
google: req.user.google,
|
|
195
|
-
user
|
|
196
|
-
},
|
|
197
|
-
accessToken
|
|
198
|
-
});
|
|
375
|
+
// checkPassport is Catch.
|
|
376
|
+
resolve(false);
|
|
199
377
|
}
|
|
200
378
|
});
|
|
379
|
+
} catch (error) {
|
|
380
|
+
reject(error);
|
|
201
381
|
}
|
|
202
382
|
});
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (passport_config.strategy.facebook.allow) {
|
|
209
|
-
_app_.get(auth_endpoint + '/facebook', passport.authenticate('facebook', { scope: [ 'email', 'public_profile' ] }));
|
|
210
|
-
// facebook callback
|
|
211
|
-
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";
|
|
212
|
-
_app_.get(auth_endpoint + facebookCallback, passport.authenticate('facebook', { failureRedirect: passport_config.strategy.facebook.failureRedirect, failureMessage: true }), (req, res) => {
|
|
213
|
-
if (typeof req.user.user !== 'undefined') {
|
|
214
|
-
// declare user for sign JWT
|
|
215
|
-
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
216
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
217
|
-
expiresIn: passport_config.token_expired
|
|
218
|
-
});
|
|
219
|
-
// response JWT
|
|
220
|
-
res.status(200).json({
|
|
221
|
-
code: 200,
|
|
222
|
-
status: "AUTHORIZED",
|
|
223
|
-
message: "success.",
|
|
224
|
-
user: req.user,
|
|
225
|
-
accessToken
|
|
226
|
-
});
|
|
383
|
+
// Promise all
|
|
384
|
+
Promise.all([ signJWT ]).then(async (final) => {
|
|
385
|
+
// [signJWT=init auth endpoint]
|
|
386
|
+
if(final[0]) {
|
|
387
|
+
resolve(true);
|
|
227
388
|
} else {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
Beech.findOne([], condUser, (err, result) => {
|
|
231
|
-
if (err) {
|
|
232
|
-
res.status(500).json({
|
|
233
|
-
code: 500,
|
|
234
|
-
status: "INTERNAL_SERVER_ERR",
|
|
235
|
-
message: "Internal server error.",
|
|
236
|
-
error: err
|
|
237
|
-
});
|
|
238
|
-
} else {
|
|
239
|
-
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
240
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
241
|
-
expiresIn: passport_config.token_expired
|
|
242
|
-
});
|
|
243
|
-
// response JWT
|
|
244
|
-
res.status(201).json({
|
|
245
|
-
code: 201,
|
|
246
|
-
status: "AUTHORIZED",
|
|
247
|
-
message: "success.",
|
|
248
|
-
user: {
|
|
249
|
-
facebook: req.user.facebook,
|
|
250
|
-
user
|
|
251
|
-
},
|
|
252
|
-
accessToken
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
});
|
|
389
|
+
// resolve false to JWT turn OFF
|
|
390
|
+
resolve(false);
|
|
256
391
|
}
|
|
257
|
-
});
|
|
392
|
+
}).catch((err) => reject(err));
|
|
393
|
+
} catch (error) {
|
|
394
|
+
reject(error);
|
|
258
395
|
}
|
|
259
|
-
}
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
396
|
+
});
|
|
262
397
|
}
|
|
263
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);
|