質問編集履歴

1

認証情報が記入されていた為修正

2022/12/21 14:12

投稿

dauto
dauto

スコア38

test CHANGED
File without changes
test CHANGED
@@ -1,161 +1,81 @@
1
1
  [techiediaries様のIonic 4のログイン処理の記事](https://www.techiediaries.com/ionic-jwt-authentication-httpclient/)を参考にしながらIonic 4のログイン処理を設定していたのですが、Express.jsの認証サーバーでMySQLの設定中に以下のエラーが発生しました。
2
-
3
2
  自分ではエラーの原因が分からなかったので、お手数ですがご助言を頂けると助かります。
4
-
5
3
  ```index.js
6
-
7
4
  "use strict";
8
-
9
5
  const express = require('express');
10
-
11
6
  const bodyParser = require('body-parser');
12
-
13
7
  const cors = require('cors')
14
-
15
8
  const mysql = require('mysql');
16
-
17
9
  const connection = mysql.createConnection({
18
-
19
- host: '133.18.201.159',
10
+ host: 'XXX.XXX.XXX.XXX',
20
-
21
11
  port: 3306,
22
-
23
- user: 'Replication',
12
+ user: '',
24
-
25
- password: 'Hnnk$6084',
13
+ password: '',
26
-
27
14
  database: 'eccube_db_2'
28
-
29
15
  });
30
-
31
16
  const jwt = require('jsonwebtoken');
32
-
33
17
  const bcrypt = require('bcryptjs');
34
-
35
18
  const crypto = require("crypto");
36
-
37
-
38
19
 
39
20
  const SECRET_KEY = "secretkey23456";
40
21
 
41
-
42
-
43
22
  const app = express();
44
-
45
23
  const router = express.Router();
46
-
47
24
  app.use(cors())
48
-
49
-
50
25
 
51
26
  connection.connect()
52
27
 
53
-
54
-
55
28
  router.use(bodyParser.urlencoded({ extended: false }));
56
-
57
29
  router.use(bodyParser.json());
58
-
59
30
  }
60
-
61
31
  const findUserByEmail = connection.query('SELECT * FROM 'dtb_customer' WHERE 'email' = ?', [email], function (err, row, fields) {
62
-
63
32
  });
64
33
 
65
-
66
-
67
34
  router.get('/', (req, res) => {
68
-
69
35
  res.status(200).send('This is an authentication server');
70
-
71
36
  });
72
37
 
73
-
74
-
75
38
  router.post('/login', (req, res) => {
76
-
77
39
  const email = req.body.email;
78
-
79
40
  const default_password = req.body.password;
80
-
81
41
  const hmac = crypto.createHmac('sha256', 'NFXwzxb5o9hfELny04AaoY0SgLRHrJlc');
82
-
83
42
  hmac.update(default_password . ':' . req.body.hash)
84
-
85
43
  findUserByEmail(email, (err, user)=>{
86
-
87
44
  if (err) return res.status(500).send('Server error!');
88
-
89
45
  if (!user) return res.status(404).send('User not found!');
90
-
91
46
  const result = bcrypt.compareSync(password, user.password);
92
-
93
47
  if(!result) return res.status(401).send('Password not valid!');
94
48
 
95
-
96
-
97
49
  const expiresIn = 24 * 60 * 60;
98
-
99
50
  const accessToken = jwt.sign({ id: user.id }, SECRET_KEY, {
100
-
101
51
  expiresIn: expiresIn
102
-
103
52
  });
104
-
105
53
  res.status(200).send({ "user": user, "access_token": accessToken, "expires_in": expiresIn});
106
-
107
54
  });
108
-
109
55
  });
110
56
 
111
-
112
-
113
57
  app.use(router);
114
-
115
58
  const port = process.env.PORT || 3000;
116
-
117
59
  const server = app.listen(port, () => {
118
-
119
60
  console.log('Server listening at http:fuwarun.net');
120
-
121
61
  });
122
62
 
123
-
124
-
125
63
  connection.end()
126
-
127
64
  ```
128
65
 
129
-
130
-
131
66
  ```エラー内容
132
-
133
67
  const findUserByEmail = connection.query('SELECT * FROM 'dtb_customer' WHERE 'email' = ?', [email], function (err, row, fields) {
134
-
135
68
  ^^^^^
136
69
 
137
-
138
-
139
70
  SyntaxError: Unexpected token const
140
-
141
71
  at createScript (vm.js:56:10)
142
-
143
72
  at Object.runInThisContext (vm.js:97:10)
144
-
145
73
  at Module._compile (module.js:549:28)
146
-
147
74
  at Object.Module._extensions..js (module.js:586:10)
148
-
149
75
  at Module.load (module.js:494:32)
150
-
151
76
  at tryModuleLoad (module.js:453:12)
152
-
153
77
  at Function.Module._load (module.js:445:3)
154
-
155
78
  at Module.runMain (module.js:611:10)
156
-
157
79
  at run (bootstrap_node.js:394:7)
158
-
159
80
  at startup (bootstrap_node.js:160:9)
160
-
161
81
  ```