Code Duplication    Length = 61-61 lines in 2 locations

v1/models/auth.js 1 location

@@ 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) {

v2/models/auth.js 1 location

@@ 274-334 (lines=61) @@
271
        db.get("SELECT * FROM users WHERE apiKey = ? AND email = ?",
272
            apiKey,
273
            email,
274
            (err, rows) => {
275
                if (err) {
276
                    return res.status(500).json({
277
                        errors: {
278
                            status: 500,
279
                            source: "/login",
280
                            title: "Database error",
281
                            detail: err.message
282
                        }
283
                    });
284
                }
285
286
                if (rows === undefined) {
287
                    return res.status(401).json({
288
                        errors: {
289
                            status: 401,
290
                            source: "/login",
291
                            title: "User not found",
292
                            detail: "User with provided email not found."
293
                        }
294
                    });
295
                }
296
297
                const user = rows;
298
299
                bcrypt.compare(password, user.password, (err, result) => {
300
                    if (err) {
301
                        return res.status(500).json({
302
                            errors: {
303
                                status: 500,
304
                                source: "/login",
305
                                title: "bcrypt error",
306
                                detail: "bcrypt error"
307
                            }
308
                        });
309
                    }
310
311
                    if (result) {
312
                        let payload = { api_key: user.apiKey, email: user.email };
313
                        let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' });
314
315
                        return res.json({
316
                            data: {
317
                                type: "success",
318
                                message: "User logged in",
319
                                user: payload,
320
                                token: jwtToken
321
                            }
322
                        });
323
                    }
324
325
                    return res.status(401).json({
326
                        errors: {
327
                            status: 401,
328
                            source: "/login",
329
                            title: "Wrong password",
330
                            detail: "Password is incorrect."
331
                        }
332
                    });
333
                });
334
            });
335
    },
336
337
    register: function(res, body) {