| @@ 141-201 (lines=61) @@ | ||
| 138 | db.get("SELECT * FROM users WHERE apiKey = ? AND email = ?", |
|
| 139 | apiKey, |
|
| 140 | email, |
|
| 141 | (err, rows) => { |
|
| 142 | if (err) { |
|
| 143 | return res.status(500).json({ |
|
| 144 | errors: { |
|
| 145 | status: 500, |
|
| 146 | source: "/login", |
|
| 147 | title: "Database error", |
|
| 148 | detail: err.message |
|
| 149 | } |
|
| 150 | }); |
|
| 151 | } |
|
| 152 | ||
| 153 | if (rows === undefined) { |
|
| 154 | return res.status(401).json({ |
|
| 155 | errors: { |
|
| 156 | status: 401, |
|
| 157 | source: "/login", |
|
| 158 | title: "User not found", |
|
| 159 | detail: "User with provided email not found." |
|
| 160 | } |
|
| 161 | }); |
|
| 162 | } |
|
| 163 | ||
| 164 | const user = rows; |
|
| 165 | ||
| 166 | bcrypt.compare(password, user.password, (err, result) => { |
|
| 167 | if (err) { |
|
| 168 | return res.status(500).json({ |
|
| 169 | errors: { |
|
| 170 | status: 500, |
|
| 171 | source: "/login", |
|
| 172 | title: "bcrypt error", |
|
| 173 | detail: "bcrypt error" |
|
| 174 | } |
|
| 175 | }); |
|
| 176 | } |
|
| 177 | ||
| 178 | if (result) { |
|
| 179 | let payload = { api_key: user.apiKey, email: user.email }; |
|
| 180 | let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' }); |
|
| 181 | ||
| 182 | return res.json({ |
|
| 183 | data: { |
|
| 184 | type: "success", |
|
| 185 | message: "User logged in", |
|
| 186 | user: payload, |
|
| 187 | token: jwtToken |
|
| 188 | } |
|
| 189 | }); |
|
| 190 | } else { |
|
| 191 | return res.status(401).json({ |
|
| 192 | errors: { |
|
| 193 | status: 401, |
|
| 194 | source: "/login", |
|
| 195 | title: "Wrong password", |
|
| 196 | detail: "Password is incorrect." |
|
| 197 | } |
|
| 198 | }); |
|
| 199 | } |
|
| 200 | }); |
|
| 201 | }); |
|
| 202 | } |
|
| 203 | ||
| 204 | function register(res, body) { |
|
| @@ 180-240 (lines=61) @@ | ||
| 177 | db.get("SELECT * FROM users WHERE apiKey = ? AND email = ?", |
|
| 178 | apiKey, |
|
| 179 | email, |
|
| 180 | (err, rows) => { |
|
| 181 | if (err) { |
|
| 182 | return res.status(500).json({ |
|
| 183 | errors: { |
|
| 184 | status: 500, |
|
| 185 | source: "/login", |
|
| 186 | title: "Database error", |
|
| 187 | detail: err.message |
|
| 188 | } |
|
| 189 | }); |
|
| 190 | } |
|
| 191 | ||
| 192 | if (rows === undefined) { |
|
| 193 | return res.status(401).json({ |
|
| 194 | errors: { |
|
| 195 | status: 401, |
|
| 196 | source: "/login", |
|
| 197 | title: "User not found", |
|
| 198 | detail: "User with provided email not found." |
|
| 199 | } |
|
| 200 | }); |
|
| 201 | } |
|
| 202 | ||
| 203 | const user = rows; |
|
| 204 | ||
| 205 | bcrypt.compare(password, user.password, (err, result) => { |
|
| 206 | if (err) { |
|
| 207 | return res.status(500).json({ |
|
| 208 | errors: { |
|
| 209 | status: 500, |
|
| 210 | source: "/login", |
|
| 211 | title: "bcrypt error", |
|
| 212 | detail: "bcrypt error" |
|
| 213 | } |
|
| 214 | }); |
|
| 215 | } |
|
| 216 | ||
| 217 | if (result) { |
|
| 218 | let payload = { api_key: user.apiKey, email: user.email }; |
|
| 219 | let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' }); |
|
| 220 | ||
| 221 | return res.json({ |
|
| 222 | data: { |
|
| 223 | type: "success", |
|
| 224 | message: "User logged in", |
|
| 225 | user: payload, |
|
| 226 | token: jwtToken |
|
| 227 | } |
|
| 228 | }); |
|
| 229 | } |
|
| 230 | ||
| 231 | return res.status(401).json({ |
|
| 232 | errors: { |
|
| 233 | status: 401, |
|
| 234 | source: "/login", |
|
| 235 | title: "Wrong password", |
|
| 236 | detail: "Password is incorrect." |
|
| 237 | } |
|
| 238 | }); |
|
| 239 | }); |
|
| 240 | }); |
|
| 241 | }, |
|
| 242 | ||
| 243 | register: function(res, body) { |
|