| @@ 270-330 (lines=61) @@ | ||
| 267 | db.get("SELECT * FROM users WHERE apiKey = ? AND email = ?", |
|
| 268 | apiKey, |
|
| 269 | email, |
|
| 270 | (err, rows) => { |
|
| 271 | if (err) { |
|
| 272 | return res.status(500).json({ |
|
| 273 | errors: { |
|
| 274 | status: 500, |
|
| 275 | source: "/login", |
|
| 276 | title: "Database error", |
|
| 277 | detail: err.message |
|
| 278 | } |
|
| 279 | }); |
|
| 280 | } |
|
| 281 | ||
| 282 | if (rows === undefined) { |
|
| 283 | return res.status(401).json({ |
|
| 284 | errors: { |
|
| 285 | status: 401, |
|
| 286 | source: "/login", |
|
| 287 | title: "User not found", |
|
| 288 | detail: "User with provided email not found." |
|
| 289 | } |
|
| 290 | }); |
|
| 291 | } |
|
| 292 | ||
| 293 | const user = rows; |
|
| 294 | ||
| 295 | bcrypt.compare(password, user.password, (err, result) => { |
|
| 296 | if (err) { |
|
| 297 | return res.status(500).json({ |
|
| 298 | errors: { |
|
| 299 | status: 500, |
|
| 300 | source: "/login", |
|
| 301 | title: "bcrypt error", |
|
| 302 | detail: "bcrypt error" |
|
| 303 | } |
|
| 304 | }); |
|
| 305 | } |
|
| 306 | ||
| 307 | if (result) { |
|
| 308 | let payload = { api_key: user.apiKey, email: user.email }; |
|
| 309 | let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' }); |
|
| 310 | ||
| 311 | return res.json({ |
|
| 312 | data: { |
|
| 313 | type: "success", |
|
| 314 | message: "User logged in", |
|
| 315 | user: payload, |
|
| 316 | token: jwtToken |
|
| 317 | } |
|
| 318 | }); |
|
| 319 | } |
|
| 320 | ||
| 321 | return res.status(401).json({ |
|
| 322 | errors: { |
|
| 323 | status: 401, |
|
| 324 | source: "/login", |
|
| 325 | title: "Wrong password", |
|
| 326 | detail: "Password is incorrect." |
|
| 327 | } |
|
| 328 | }); |
|
| 329 | }); |
|
| 330 | }); |
|
| 331 | }, |
|
| 332 | ||
| 333 | register: function(res, body) { |
|
| @@ 134-194 (lines=61) @@ | ||
| 131 | db.get("SELECT * FROM users WHERE apiKey = ? AND email = ?", |
|
| 132 | apiKey, |
|
| 133 | email, |
|
| 134 | (err, rows) => { |
|
| 135 | if (err) { |
|
| 136 | return res.status(500).json({ |
|
| 137 | errors: { |
|
| 138 | status: 500, |
|
| 139 | source: "/login", |
|
| 140 | title: "Database error", |
|
| 141 | detail: err.message |
|
| 142 | } |
|
| 143 | }); |
|
| 144 | } |
|
| 145 | ||
| 146 | if (rows === undefined) { |
|
| 147 | return res.status(401).json({ |
|
| 148 | errors: { |
|
| 149 | status: 401, |
|
| 150 | source: "/login", |
|
| 151 | title: "User not found", |
|
| 152 | detail: "User with provided email not found." |
|
| 153 | } |
|
| 154 | }); |
|
| 155 | } |
|
| 156 | ||
| 157 | const user = rows; |
|
| 158 | ||
| 159 | bcrypt.compare(password, user.password, (err, result) => { |
|
| 160 | if (err) { |
|
| 161 | return res.status(500).json({ |
|
| 162 | errors: { |
|
| 163 | status: 500, |
|
| 164 | source: "/login", |
|
| 165 | title: "bcrypt error", |
|
| 166 | detail: "bcrypt error" |
|
| 167 | } |
|
| 168 | }); |
|
| 169 | } |
|
| 170 | ||
| 171 | if (result) { |
|
| 172 | let payload = { api_key: user.apiKey, email: user.email }; |
|
| 173 | let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' }); |
|
| 174 | ||
| 175 | return res.json({ |
|
| 176 | data: { |
|
| 177 | type: "success", |
|
| 178 | message: "User logged in", |
|
| 179 | user: payload, |
|
| 180 | token: jwtToken |
|
| 181 | } |
|
| 182 | }); |
|
| 183 | } else { |
|
| 184 | return res.status(401).json({ |
|
| 185 | errors: { |
|
| 186 | status: 401, |
|
| 187 | source: "/login", |
|
| 188 | title: "Wrong password", |
|
| 189 | detail: "Password is incorrect." |
|
| 190 | } |
|
| 191 | }); |
|
| 192 | } |
|
| 193 | }); |
|
| 194 | }); |
|
| 195 | } |
|
| 196 | ||
| 197 | function register(res, body) { |
|