chyz 1.0.12-rc.7 → 1.0.13-rc.2

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.
Files changed (96) hide show
  1. package/BaseChyz.ts +76 -14
  2. package/Chyz.ts +0 -3
  3. package/Examples/Controllers/ApiController.ts +88 -30
  4. package/Examples/Controllers/KeyCloakController.ts +100 -0
  5. package/Examples/Controllers/PublicController.ts +5 -7
  6. package/Examples/Controllers/SiteController.ts +82 -32
  7. package/Examples/Models/Categories.ts +36 -0
  8. package/Examples/Models/Customer.ts +115 -0
  9. package/Examples/Models/KeycloakUser.ts +66 -0
  10. package/Examples/Models/Order.ts +64 -0
  11. package/Examples/Models/OrderItem.ts +27 -0
  12. package/Examples/Models/ProductModels.ts +50 -0
  13. package/Examples/Models/ProductToCategories.ts +35 -0
  14. package/Examples/Models/Products.ts +49 -0
  15. package/Examples/Models/Stocks.ts +60 -0
  16. package/Examples/Models/User.ts +66 -35
  17. package/Examples/Models/UserPermission.ts +37 -0
  18. package/Examples/index-keycloack.ts +78 -0
  19. package/Examples/index.ts +16 -15
  20. package/Examples/keycloak.json +7 -0
  21. package/Examples/log/app.log +903 -1034
  22. package/Examples/log/errors.log +79 -256
  23. package/Examples/package.json +46 -44
  24. package/Examples/tsconfig.json +1 -1
  25. package/Examples/yarn.lock +2549 -0
  26. package/README.md +118 -16
  27. package/base/ActionFilter.ts +2 -2
  28. package/base/BaseError.ts +2 -2
  29. package/base/DataErrorDbException.ts +1 -1
  30. package/base/DbConnection.ts +8 -2
  31. package/base/ForbiddenHttpException.ts +1 -1
  32. package/base/InvalidConfigException.ts +1 -1
  33. package/base/Model.ts +204 -15
  34. package/base/NotFoundHttpException.ts +1 -1
  35. package/base/RestClient.ts +28 -0
  36. package/base/UnauthorizedHttpException.ts +2 -1
  37. package/base/ValidationHttpException.ts +14 -0
  38. package/base/db/Exception.ts +14 -0
  39. package/base/index.ts +2 -0
  40. package/dist/BaseChyz.js +72 -25
  41. package/dist/BaseChyz.js.map +1 -1
  42. package/dist/Chyz.js.map +1 -1
  43. package/dist/base/ActionFilter.js +2 -2
  44. package/dist/base/ActionFilter.js.map +1 -1
  45. package/dist/base/BaseError.js +2 -2
  46. package/dist/base/BaseError.js.map +1 -1
  47. package/dist/base/DataErrorDbException.js +1 -1
  48. package/dist/base/DataErrorDbException.js.map +1 -1
  49. package/dist/base/DbConnection.js +9 -2
  50. package/dist/base/DbConnection.js.map +1 -1
  51. package/dist/base/ForbiddenHttpException.js +1 -1
  52. package/dist/base/ForbiddenHttpException.js.map +1 -1
  53. package/dist/base/InvalidConfigException.js +1 -1
  54. package/dist/base/InvalidConfigException.js.map +1 -1
  55. package/dist/base/Model.js +181 -13
  56. package/dist/base/Model.js.map +1 -1
  57. package/dist/base/NotFoundHttpException.js +1 -1
  58. package/dist/base/NotFoundHttpException.js.map +1 -1
  59. package/dist/base/RestClient.js +27 -0
  60. package/dist/base/RestClient.js.map +1 -0
  61. package/dist/base/UnauthorizedHttpException.js +1 -1
  62. package/dist/base/UnauthorizedHttpException.js.map +1 -1
  63. package/dist/base/ValidationHttpException.js +18 -0
  64. package/dist/base/ValidationHttpException.js.map +1 -0
  65. package/dist/base/db/Exception.js +16 -0
  66. package/dist/base/db/Exception.js.map +1 -0
  67. package/dist/base/index.js +2 -0
  68. package/dist/base/index.js.map +1 -1
  69. package/dist/filters/AccessControl.js +2 -2
  70. package/dist/filters/AccessControl.js.map +1 -1
  71. package/dist/filters/AccessRule.js.map +1 -1
  72. package/dist/filters/auth/AuthMethod.js +2 -2
  73. package/dist/filters/auth/AuthMethod.js.map +1 -1
  74. package/dist/filters/auth/HttpHeaderAuth.js.map +1 -1
  75. package/dist/filters/auth/JwtHttpBearerAuth.js +1 -0
  76. package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
  77. package/dist/filters/auth/KeyCloakHttpBearerAuth.js +117 -0
  78. package/dist/filters/auth/KeyCloakHttpBearerAuth.js.map +1 -0
  79. package/dist/index.js +29 -0
  80. package/dist/index.js.map +1 -0
  81. package/dist/web/{User.js → WebUser.js} +12 -7
  82. package/dist/web/WebUser.js.map +1 -0
  83. package/filters/AccessControl.ts +3 -3
  84. package/filters/AccessRule.ts +2 -2
  85. package/filters/auth/AuthMethod.ts +4 -4
  86. package/filters/auth/HttpHeaderAuth.ts +2 -2
  87. package/filters/auth/JwtHttpBearerAuth.ts +4 -5
  88. package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
  89. package/index.ts +14 -0
  90. package/package.json +55 -52
  91. package/web/{User.ts → WebUser.ts} +10 -6
  92. package/.idea/Chy-Nodejs-Framework.iml +0 -12
  93. package/.idea/jsLibraryMappings.xml +0 -6
  94. package/.idea/modules.xml +0 -8
  95. package/.idea/vcs.xml +0 -6
  96. package/dist/web/User.js.map +0 -1
package/BaseChyz.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  import 'reflect-metadata';
3
2
  import {RouteDefinition} from "./model/RouteDefinition";
4
3
  import {NextFunction, Request, Response} from "express";
@@ -9,8 +8,8 @@ import Utils from "./requiments/Utils";
9
8
  const express = require("express");
10
9
  const log4js = require("log4js");
11
10
  const fs = require('fs');
12
- const _ = require('lodash');
13
11
 
12
+ var ip = require('ip');
14
13
  var bodyParser = require('body-parser')
15
14
  var methodOverride = require('method-override')
16
15
 
@@ -26,6 +25,8 @@ export default class BaseChyz {
26
25
  private _controllerpath: string = "Controllers"
27
26
  private static controllers: Array<Controller> = []
28
27
  public static components: any = {}
28
+ public static middlewares: any = {}
29
+
29
30
 
30
31
  get logConfig(): any {
31
32
  return this._logConfig;
@@ -62,7 +63,6 @@ export default class BaseChyz {
62
63
  writable: true
63
64
  })
64
65
 
65
-
66
66
  Object.defineProperty(BaseChyz.express.request, 'identity', {
67
67
  configurable: true,
68
68
  enumerable: true,
@@ -114,14 +114,13 @@ export default class BaseChyz {
114
114
  }
115
115
 
116
116
 
117
- async app(config: any = {}) {
117
+ app(config: any = {}): BaseChyz {
118
118
 
119
119
  /**
120
120
  * Config set
121
121
  */
122
122
  this.config = config;
123
123
 
124
- this.init();
125
124
 
126
125
  let components = Utils.findKeyValue(config, "components")
127
126
  if (components) {
@@ -129,10 +128,25 @@ export default class BaseChyz {
129
128
  let comp = components[componentsKey];
130
129
  BaseChyz.debug("Create Component ", componentsKey)
131
130
  BaseChyz.components[componentsKey] = Utils.createObject(new comp.class, comp);
132
- await BaseChyz.components[componentsKey]?.init();
131
+ BaseChyz.components[componentsKey]?.init();
133
132
  }
134
133
  }
135
134
 
135
+
136
+ let middlewares = Utils.findKeyValue(config, "middlewares")
137
+ if (middlewares) {
138
+ for (const middlewareKey in middlewares) {
139
+ let middleware1 = middlewares[middlewareKey];
140
+ BaseChyz.debug("Create middlewares ", middlewareKey)
141
+ BaseChyz.middlewares[middlewareKey] = middleware1;
142
+ // BaseChyz.middlewares[middlewareKey] = Utils.createObject(new middleware1.class, middleware1);
143
+ }
144
+ }
145
+
146
+
147
+
148
+ this.init();
149
+
136
150
  return this;
137
151
  }
138
152
 
@@ -141,23 +155,39 @@ export default class BaseChyz {
141
155
  return log4js;
142
156
  }
143
157
 
158
+ public getLogger() {
159
+ return this.logProvider().getLogger(this.constructor.name);
160
+ }
144
161
 
145
162
  static logs(...args: any[]) {
146
163
  return log4js.getLogger(this.name);
147
164
  }
148
165
 
166
+ public static trace(...args: any[]) {
167
+ BaseChyz.logs().fatal(...arguments)
168
+ }
169
+
170
+ public static debug(...args: any[]) {
171
+ BaseChyz.logs().debug(...arguments)
172
+ }
173
+
149
174
  public static info(...args: any[]) {
150
175
  BaseChyz.logs().info(...arguments)
151
176
  }
152
177
 
178
+ public static warn(...args: any[]) {
179
+ BaseChyz.logs().warn(...arguments)
180
+ }
181
+
153
182
  public static error(...args: any[]) {
154
183
  BaseChyz.logs().error(...arguments)
155
184
  }
156
185
 
157
- public static debug(...args: any[]) {
158
- BaseChyz.logs().debug(...arguments)
186
+ public static fatal(...args: any[]) {
187
+ BaseChyz.logs().fatal(...arguments)
159
188
  }
160
189
 
190
+
161
191
  public static warning(...args: any[]) {
162
192
  BaseChyz.logs().warn(...arguments)
163
193
  }
@@ -194,6 +224,12 @@ export default class BaseChyz {
194
224
  return BaseChyz.components[key] ?? null
195
225
  }
196
226
 
227
+
228
+ public static getMiddlewares(key: any) {
229
+ return BaseChyz.middlewares[key] ?? null
230
+ }
231
+
232
+
197
233
  /**
198
234
  * load contoller
199
235
  */
@@ -245,10 +281,16 @@ export default class BaseChyz {
245
281
  await instance[route.methodName](req, res, next);
246
282
  instance.afterAction(route, req, res);
247
283
  } catch (e) {
248
- BaseChyz.error(e)
249
- // next(e)
250
- res.status(e.statusCode)
251
- res.json({error: {code: e.statusCode, name: e.name, message: e.message}})
284
+ if (e instanceof Error) {
285
+ BaseChyz.error(e)
286
+
287
+ // @ts-ignore
288
+ res.status(e.statusCode || 500)
289
+ // @ts-ignore
290
+ res.json({error: {code: e.statusCode || 500, name: e.name, message: e.message}})
291
+ }else{
292
+ res.json(e )
293
+ }
252
294
  }
253
295
  })
254
296
 
@@ -263,8 +305,8 @@ export default class BaseChyz {
263
305
  BaseChyz.express.use(bodyParser.json())
264
306
  BaseChyz.express.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
265
307
  BaseChyz.express.use(methodOverride());
266
- BaseChyz.express.use(this.errorResponder)
267
- BaseChyz.express.use(this.errorHandler)
308
+ BaseChyz.express.use(methodOverride());
309
+
268
310
 
269
311
 
270
312
  // CORS
@@ -279,7 +321,20 @@ export default class BaseChyz {
279
321
  next();
280
322
  });
281
323
 
324
+ //Middlewares
325
+ for (const middleware1 of Object.keys(BaseChyz.middlewares)) {
326
+ if (!Utils.isFunction(middleware1)) {
327
+ let keycloak = BaseChyz.middlewares[middleware1].keycloak;
328
+ BaseChyz.express.use(keycloak.middleware(BaseChyz.middlewares[middleware1].config));
329
+ } else {
330
+ BaseChyz.express.use(BaseChyz.middlewares[middleware1]);
331
+ }
332
+
333
+ }
334
+
282
335
 
336
+ BaseChyz.express.use(this.errorResponder)
337
+ BaseChyz.express.use(this.errorHandler)
283
338
  }
284
339
 
285
340
 
@@ -289,6 +344,8 @@ export default class BaseChyz {
289
344
  BaseChyz.express.listen(this._port, () => {
290
345
  BaseChyz.info("Express Server Start ")
291
346
  BaseChyz.info(`Liten Port ${this._port}`)
347
+ BaseChyz.info(`http://localhost:${this._port}`)
348
+ BaseChyz.info(`http://${ip.address()}:${this._port}`)
292
349
  })
293
350
  return this;
294
351
  }
@@ -297,3 +354,8 @@ export default class BaseChyz {
297
354
  }
298
355
 
299
356
 
357
+ process.on('uncaughtException', err => {
358
+ BaseChyz.error('There was an uncaught error', err)
359
+ process.exit(1) //mandatory (as per the Node.js docs)
360
+ })
361
+
package/Chyz.ts CHANGED
@@ -6,9 +6,6 @@
6
6
  */
7
7
 
8
8
  import BaseChyz from "./BaseChyz";
9
-
10
-
11
-
12
9
  const Chyz = new BaseChyz();
13
10
 
14
11
  export default Chyz;
@@ -1,19 +1,27 @@
1
1
  /*
2
- * Copyright (c) 2021. Chy Bilgisayar Bilisim
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
3
4
  * Author: Cihan Ozturk
4
- * E-mail: cihan@chy.com.tr
5
- * Github:https://github.com/cihan53/
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
6
8
  */
7
9
 
8
- import {Controller} from "../../base/Controller";
10
+ import {Controller} from "../../base";
9
11
  import BaseChyz from "../../BaseChyz";
10
12
  // @ts-ignore
11
13
  import {Request, Response} from "express";
12
- import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
- import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
14
+ import {get} from "../../decorator";
15
+ import {post} from "../../decorator";
16
+ import {controller} from "../../decorator";
17
+ import {JwtHttpBearerAuth} from "../../filters/auth";
18
+
19
+ import {ValidationHttpException} from "../../base";
20
+ import {ForbiddenHttpException} from "../../base";
21
+ import {OrderClass, Order} from "../Models/Order";
22
+ import {CustomerClass, Customer} from "../Models/Customer";
23
+ import {Products} from "../Models/Products";
24
+ import {ProductModels} from "../Models/ProductModels";
17
25
 
18
26
  @controller("/api")
19
27
  class ApiController extends Controller {
@@ -29,22 +37,22 @@ class ApiController extends Controller {
29
37
  "class": JwtHttpBearerAuth,
30
38
  // "auth": this.myCheck
31
39
  },
32
- 'access': {
33
- 'class': AccessControl,
34
- 'only': ['login', 'logout', 'signup'],
35
- 'rules': [
36
- {
37
- 'allow': true,
38
- 'actions': ['login', 'index'],
39
- 'roles': ['?'],
40
- },
41
- {
42
- 'allow': true,
43
- 'actions': ['logout', "logout2"],
44
- 'roles': ['@'],
45
- }
46
- ]
47
- }
40
+ // 'access': {
41
+ // 'class': AccessControl,
42
+ // 'only': ['login', 'logout', 'signup'],
43
+ // 'rules': [
44
+ // {
45
+ // 'allow': true,
46
+ // 'actions': ['login', 'index'],
47
+ // 'roles': ['?'],
48
+ // },
49
+ // {
50
+ // 'allow': true,
51
+ // 'actions': ['logout', "logout2"],
52
+ // 'roles': ['@'],
53
+ // }
54
+ // ]
55
+ // }
48
56
  }]
49
57
  }
50
58
 
@@ -55,19 +63,69 @@ class ApiController extends Controller {
55
63
  return res.json({message: "index sayfası"})
56
64
  }
57
65
 
58
- @post("login")
59
- Login(req: Request, res: Response) {
60
- BaseChyz.logs().info("Post Controller")
66
+ @post("orderCreate")
67
+ async Login(req: Request, res: Response) {
68
+ let data = req.body;
69
+ data.Customer.status = "true";
70
+ data.Customer["2fa"] = "true";
71
+
72
+ //Customer Model Create
73
+ let customer: CustomerClass = Customer;
74
+ //Order Model Create
75
+ let order: OrderClass = Order;
76
+
77
+
78
+ let transaction
79
+ try {
80
+ // get transaction
81
+ transaction = await BaseChyz.getComponent("db").transaction();
82
+
83
+
84
+ customer.load(data, "Customer");//load customer data
85
+ let cus: any = await customer.save({}, {transaction});
86
+
87
+ if (!cus) {
88
+ throw new ValidationHttpException(customer.errors);
89
+ }
90
+
91
+ data.Order.customer_id = cus.id;
92
+ // data.Order.total = 0;
93
+ // data.Order.status = true;
94
+ order.load(data, "Order");
95
+ let res1 = await order.save({}, {transaction});
96
+ if (!res1) {
97
+ throw new ValidationHttpException(order.errors);
98
+ }
99
+
100
+ // commit
101
+ await transaction.commit();
102
+
103
+ } catch (e) {
104
+ if (transaction) {
105
+ await transaction.rollback();
106
+ BaseChyz.warn("Rollback transaction")
107
+ }
108
+
109
+ if (e instanceof ValidationHttpException)
110
+ throw new ValidationHttpException(e.message)
111
+ else
112
+ throw new ForbiddenHttpException(e.message)
113
+ }
61
114
  return res.send("Post Controller")
62
115
  }
63
116
 
64
117
 
118
+ @get("order/list")
119
+ async listOrder(req: Request, res: Response) {
120
+ let product = await Products.findAll({include: [ProductModels.model()]});
121
+ return res.json(product)
65
122
 
66
-
123
+ }
67
124
 
68
125
  error(req: Request, res: Response) {
69
126
  BaseChyz.logs().info("Error Sayfası")
70
127
  return res.send("Post Controller")
71
128
  }
72
129
  }
73
- module.exports=ApiController
130
+
131
+ module.exports = ApiController
@@ -0,0 +1,100 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
4
+ * Author: Cihan Ozturk
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
8
+ */
9
+
10
+ import {Controller} from "../../base/Controller";
11
+ import BaseChyz from "../../BaseChyz";
12
+ // @ts-ignore
13
+ import {Request, Response} from "express";
14
+ import {get,post,controller} from "../../decorator";
15
+
16
+ import {ValidationHttpException,ForbiddenHttpException} from "../../base";
17
+ import {KeyCloakHttpBearerAuth} from "../../filters/auth/KeyCloakHttpBearerAuth";
18
+
19
+ import {OrderClass, Order} from "../Models/Order";
20
+ import {CustomerClass, Customer} from "../Models/Customer";
21
+
22
+ @controller("/oauth2.0")
23
+ class ApiController extends Controller {
24
+
25
+ public behaviors(): any[] {
26
+
27
+ return [{
28
+ 'authenticator': {
29
+ "class": KeyCloakHttpBearerAuth,
30
+ // "auth": this.myCheck
31
+ }
32
+ }]
33
+ }
34
+
35
+ @get("/")
36
+ Index(req: Request, res: Response) {
37
+
38
+ BaseChyz.logs().info("Site Controller Burası", this.id)
39
+ return res.json({message: "index sayfası"})
40
+ }
41
+
42
+ @post("orderCreate")
43
+ async Login(req: Request, res: Response) {
44
+ let data = req.body;
45
+ data.Customer.status = "true";
46
+ data.Customer["2fa"] = "true";
47
+
48
+ //Customer Model Create
49
+ let customer: CustomerClass = Customer;
50
+ //Order Model Create
51
+ let order: OrderClass = Order ;
52
+
53
+
54
+ let transaction
55
+ try {
56
+ // get transaction
57
+ transaction = await BaseChyz.getComponent("db").transaction();
58
+
59
+
60
+ customer.load(data, "Customer");//load customer data
61
+ let cus: any = await customer.save({}, {transaction});
62
+
63
+ if (!cus) {
64
+ throw new ValidationHttpException(customer.errors);
65
+ }
66
+
67
+ data.Order.customer_id = cus.id;
68
+ // data.Order.total = 0;
69
+ // data.Order.status = true;
70
+ order.load(data, "Order");
71
+ let res1 = await order.save({}, {transaction});
72
+ if (!res1) {
73
+ throw new ValidationHttpException(order.errors);
74
+ }
75
+
76
+ // commit
77
+ await transaction.commit();
78
+
79
+ } catch (e) {
80
+ if (transaction) {
81
+ await transaction.rollback();
82
+ BaseChyz.warn("Rollback transaction")
83
+ }
84
+
85
+ if (e instanceof ValidationHttpException)
86
+ throw new ValidationHttpException(e.message)
87
+ else
88
+ throw new ForbiddenHttpException(e.message)
89
+ }
90
+ return res.send("Post Controller")
91
+ }
92
+
93
+
94
+ error(req: Request, res: Response) {
95
+ BaseChyz.logs().info("Error Sayfası")
96
+ return res.send("Post Controller")
97
+ }
98
+ }
99
+
100
+ module.exports = ApiController
@@ -1,8 +1,10 @@
1
1
  /*
2
- * Copyright (c) 2021. Chy Bilgisayar Bilisim
2
+ *
3
+ * Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
3
4
  * Author: Cihan Ozturk
4
- * E-mail: cihan@chy.com.tr
5
- * Github:https://github.com/cihan53/
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
6
8
  */
7
9
 
8
10
  import {Controller} from "../../base/Controller";
@@ -10,11 +12,7 @@ import BaseChyz from "../../BaseChyz";
10
12
  // @ts-ignore
11
13
  import {Request, Response} from "express";
12
14
  import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
15
  import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
17
- import Utils from "../../requiments/Utils";
18
16
 
19
17
  @controller("/public")
20
18
  class PublicController extends Controller {
@@ -5,16 +5,20 @@
5
5
  * Github:https://github.com/cihan53/
6
6
  */
7
7
 
8
- import {Controller} from "../../base/Controller";
8
+ import {Controller} from "../../base";
9
9
  import BaseChyz from "../../BaseChyz";
10
10
  // @ts-ignore
11
11
  import {Request, Response} from "express";
12
- import {get} from "../../decorator/get";
13
- import {post} from "../../decorator/post";
14
- import {controller} from "../../decorator/controller";
15
- import {AccessControl} from "../../filters/AccessControl";
16
- import {JwtHttpBearerAuth} from "../../filters/auth/JwtHttpBearerAuth";
17
- import Utils from "../../requiments/Utils";
12
+ import {get} from "../../decorator";
13
+ import {post} from "../../decorator";
14
+ import {controller} from "../../decorator";
15
+ import {User} from "../Models/User";
16
+ import {ForbiddenHttpException} from "../../base";
17
+ import {JwtHttpBearerAuth} from "../../filters/auth";
18
+
19
+ const bcrypt = require('bcrypt');
20
+ const JsonWebToken = require("jsonwebtoken");
21
+
18
22
 
19
23
  @controller("/site")
20
24
  class SiteController extends Controller {
@@ -22,33 +26,43 @@ class SiteController extends Controller {
22
26
  public myCheck(token) {
23
27
  console.log("myyyyyyyyyyyyyyyyyyyyy")
24
28
  }
25
-
26
29
  public behaviors(): any[] {
27
-
28
30
  return [{
29
- // 'authenticator': {
30
- // "class": JwtHttpBearerAuth,
31
- // // "auth": this.myCheck
32
- // },
33
- 'access': {
34
- 'class': AccessControl,
35
- 'only': ['login', 'logout','index' ],
36
- 'rules': [
37
- {
38
- 'allow': false,
39
- 'actions': ['login', 'index' ],
40
- 'roles': ['?'],
41
- },
42
- {
43
- 'allow': true,
44
- 'actions': ['logout', "logout2"],
45
- 'roles': ['@'],
46
- }
47
- ]
31
+ 'authenticator': {
32
+ "class": JwtHttpBearerAuth,
33
+ "except":["index","login"]
34
+ // "auth": this.myCheck
48
35
  }
49
36
  }]
50
37
  }
51
38
 
39
+
40
+ // public behaviors(): any[] {
41
+ //
42
+ // return [{
43
+ // // 'authenticator': {
44
+ // // "class": JwtHttpBearerAuth,
45
+ // // // "auth": this.myCheck
46
+ // // },
47
+ // 'access': {
48
+ // 'class': AccessControl,
49
+ // 'only': ['login', 'logout','index' ],
50
+ // 'rules': [
51
+ // {
52
+ // 'allow': false,
53
+ // 'actions': ['login', 'index' ],
54
+ // 'roles': ['?'],
55
+ // },
56
+ // {
57
+ // 'allow': true,
58
+ // 'actions': ['logout', "logout2"],
59
+ // 'roles': ['@'],
60
+ // }
61
+ // ]
62
+ // }
63
+ // }]
64
+ // }
65
+
52
66
  @get("index")
53
67
  Index(req: Request, res: Response) {
54
68
 
@@ -57,9 +71,45 @@ class SiteController extends Controller {
57
71
  }
58
72
 
59
73
  @post("login")
60
- Login(req: Request, res: Response) {
61
- BaseChyz.logs().info("Post Controller")
62
- return res.send("Post Controller")
74
+ async login(req: Request, res: Response) {
75
+ let UserModel: User = new User();
76
+ let token
77
+ let username = req.body.username;
78
+ let password = req.body.password;
79
+
80
+
81
+ let user = await UserModel.findOne({where: {username: username}})
82
+ if (user) {
83
+ BaseChyz.debug("Db found user", username)
84
+ const match = await bcrypt.compare(password, user.password);
85
+ if (match) {
86
+ BaseChyz.debug("Db user verify", username)
87
+ //login
88
+ // @ts-ignore
89
+ let xForwardedFor = (req.headers['x-forwarded-for'] || '').replace(/:\d+$/, '');
90
+ let ip = xForwardedFor || req.socket.remoteAddress;
91
+ var source: string = req.headers['user-agent'] || '';
92
+ if (req.headers['x-ucbrowser-ua']) { //special case of UC Browser
93
+ source = req.headers['x-ucbrowser-ua']+"";
94
+ }
95
+ token = await JsonWebToken.sign({
96
+ user: user.id,
97
+ ip: ip,
98
+ agent: source,
99
+ }, user.authkey, {expiresIn: '1h'});
100
+
101
+ BaseChyz.debug("Db user create access token", username,"expiresIn","1h")
102
+ return res.json({token: token})
103
+ } else {
104
+ let error: any = new ForbiddenHttpException(BaseChyz.t('You are not allowed to perform this action.'))
105
+ res.status(500).json( error.toJSON())
106
+ }
107
+ } else {
108
+ let error: any = new ForbiddenHttpException(BaseChyz.t('You are not allowed to perform this action.'))
109
+ res.status(500).json( error.toJSON())
110
+ }
111
+
112
+
63
113
  }
64
114
 
65
115
  @get("logout")
@@ -68,7 +118,7 @@ class SiteController extends Controller {
68
118
  // @ts-ignore
69
119
  let identity = req.user ?? BaseChyz.getComponent("user").getIdentity();
70
120
  // console.log("logout2", identity.id)
71
- console.log(identity)
121
+ //console.log(identity)
72
122
 
73
123
  BaseChyz.logs().info("Logout Controller")
74
124
  return res.send("Logout Controller")
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright (c) 2021. Chy Bilgisayar Bilisim
3
+ * Author: Cihan Ozturk
4
+ * E-mail: cihan@chy.com.tr
5
+ * Github:https://github.com/cihan53/
6
+ */
7
+ import {DataTypes, Model} from "../../base";
8
+
9
+ export class CategoriesClass extends Model {
10
+ [x: string]: any;
11
+
12
+ tableName() {
13
+ return 'categories';
14
+ }
15
+
16
+
17
+ attributes() {
18
+ return {
19
+ // Model attributes are defined here
20
+ title: {
21
+ type: DataTypes.STRING,
22
+ allowNull: false
23
+ },
24
+ properties: {
25
+ type: DataTypes.STRING,
26
+ allowNull: false
27
+ }
28
+
29
+ }
30
+ }
31
+
32
+
33
+ }
34
+
35
+ const Categories = new CategoriesClass();
36
+ export {Categories};