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

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