ztechno_core 0.0.43 → 0.0.45

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.
@@ -1,5 +1,5 @@
1
1
  export type ZUser = {
2
- id: number;
2
+ user_id: number;
3
3
  email: string;
4
4
  session: string;
5
5
  role: string | null;
@@ -12,7 +12,15 @@ export declare class ZUserService {
12
12
  register({ email, pass, role, admin }: ZRequiredUserColumns): Promise<{
13
13
  session: string;
14
14
  }>;
15
- find(opt: { email: string }): Promise<ZUser | undefined>;
15
+ find(
16
+ opt:
17
+ | {
18
+ email: string;
19
+ }
20
+ | {
21
+ user_id: number;
22
+ },
23
+ ): Promise<ZUser | undefined>;
16
24
  auth(opt: ZUserSession | ZUserCredentials): Promise<{
17
25
  user?: ZUser;
18
26
  session?: string;
@@ -18,14 +18,14 @@ class ZUserService {
18
18
  }
19
19
  async checkTableHasAdmin() {
20
20
  const res = await this.sqlService.query(`
21
- SELECT id FROM \`${this.tableName}\` WHERE admin=1
21
+ SELECT user_id FROM \`${this.tableName}\` WHERE admin=1
22
22
  `);
23
23
  return res.length > 0;
24
24
  }
25
25
  async createTable() {
26
26
  await this.sqlService.query(`
27
27
  CREATE TABLE \`${this.tableName}\` (
28
- \`id\` int(10) unsigned NOT NULL AUTO_INCREMENT,
28
+ \`user_id\` int(10) unsigned NOT NULL AUTO_INCREMENT,
29
29
  \`email\` varchar(64) NOT NULL,
30
30
  \`role\` varchar(64) DEFAULT NULL,
31
31
  \`pass\` varchar(512) NOT NULL,
@@ -33,7 +33,7 @@ class ZUserService {
33
33
  \`admin\` tinyint(1) NOT NULL,
34
34
  \`updated_at\` datetime NOT NULL DEFAULT current_timestamp(),
35
35
  \`created_at\` datetime NOT NULL DEFAULT current_timestamp(),
36
- PRIMARY KEY (\`id\`),
36
+ PRIMARY KEY (\`user_id\`),
37
37
  UNIQUE KEY \`email_UNIQUE\` (\`email\`),
38
38
  KEY \`email\` (\`email\`),
39
39
  KEY \`createdat\` (\`created_at\`),
@@ -60,13 +60,25 @@ class ZUserService {
60
60
  return { session };
61
61
  }
62
62
  async find(opt) {
63
- const rows = await this.sqlService.query(
64
- `
65
- SELECT id, name, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
66
- WHERE name=?`,
67
- [opt.email],
68
- );
69
- return rows[0];
63
+ if (opt.email !== undefined) {
64
+ const rows = await this.sqlService.query(
65
+ `
66
+ SELECT user_id, email, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
67
+ WHERE email=?`,
68
+ [opt.email],
69
+ );
70
+ return rows[0];
71
+ } else if (opt.user_id !== undefined) {
72
+ const rows = await this.sqlService.query(
73
+ `
74
+ SELECT user_id, email, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
75
+ WHERE user_id=?`,
76
+ [opt.user_id],
77
+ );
78
+ return rows[0];
79
+ } else {
80
+ throw new Error(`Unexpected Input for ZUserService.find(${JSON.stringify(opt)})`);
81
+ }
70
82
  }
71
83
  async auth(opt) {
72
84
  if (!opt.session && !opt.email && !opt.pass) {
@@ -75,25 +87,25 @@ class ZUserService {
75
87
  const res = await (opt.session
76
88
  ? this.sqlService.query(
77
89
  `
78
- SELECT id, name, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
90
+ SELECT user_id, email, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
79
91
  WHERE session=?`,
80
92
  [opt.session],
81
93
  )
82
94
  : this.sqlService.query(
83
95
  `
84
- SELECT id, name, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
85
- WHERE name=? AND pass=?`,
96
+ SELECT user_id, email, session, role, admin, updated_at, created_at FROM \`${this.tableName}\`
97
+ WHERE email=? AND pass=?`,
86
98
  [opt.email, this.hashPass(opt)],
87
99
  ));
88
100
  return res.length === 0 ? { authenticated: false } : { user: res[0], session: res[0].session, authenticated: true };
89
101
  }
90
- genSession({ email: name }) {
102
+ genSession({ email }) {
91
103
  const salt = this.salt;
92
- const data = name + Date.now() * Math.random();
104
+ const data = email + Date.now() * Math.random();
93
105
  return crypto_service_1.ZCryptoService.hash('sha256', data, { saltMode: 'simple', salt });
94
106
  }
95
107
  hashPass({ email, pass }) {
96
- const salt = name + this.salt;
108
+ const salt = email + this.salt;
97
109
  return crypto_service_1.ZCryptoService.hash('sha256', pass, { saltMode: 'simple', salt });
98
110
  }
99
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ztechno_core",
3
- "version": "0.0.43",
3
+ "version": "0.0.45",
4
4
  "description": "Core files for ztechno framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",