routes/login.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 103
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 64
mnd 5
bc 5
fnc 5
dl 0
loc 103
rs 10
bpm 1
cpm 2
noi 4
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C login.js ➔ login 0 67 10
1
var express = require('express');
2
var router = express.Router();
3
// const config = require('../config');
4
const sqlite3 = require('sqlite3').verbose();
0 ignored issues
show
Unused Code introduced by
The constant sqlite3 seems to be never used. Consider removing it.
Loading history...
5
// const db = new sqlite3.Database('./db/texts.sqlite');
6
const db = require("../db/database.js");
7
const bcrypt = require('bcryptjs');
8
const jwt = require('jsonwebtoken');
9
10
let config;
11
12
try {
13
    config = require('../config.js');
14
} catch (error) {
15
    console.error(error);
16
}
17
18
const jwtSecret = process.env.JWT_SECRET || config.secret;
19
20
router.get('/', function(req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
21
    const data = {
22
        data: {
23
            msg:  "Login a user"
24
        }
25
    };
26
27
    res.json(data);
28
});
29
30
router.post("/", (req, res) => {
31
    login(res, req.body);
32
33
});
34
35
function login(res, body) {
36
    const email = body.email;
37
    const password = body.password;
38
39
    if (!email || !password) {
40
        return res.status(401).json({
41
            errors: {
42
                status: 401,
43
                source: "/login",
44
                title: "Email or password missing",
45
                detail: "Email or password missing in request"
46
            }
47
        });
48
    }
49
50
    db.get("SELECT * FROM users WHERE email = ?",
51
        email,
52
        (err, rows) => {
53
        if (rows === undefined) {
54
            return res.status(401).json({
55
                errors: {
56
                    status: 401,
57
                    source: "/login",
58
                    title: "User not found",
59
                    detail: "User with provided email not found."
60
                }
61
            });
62
        }
63
        const user = rows;
64
65
        bcrypt.compare(password, user.password, (err, result) => {
66
            if (err) {
67
                return res.status(500).json({
68
                    errors: {
69
                        status: 500,
70
                        source: "/login",
71
                        title: "bcrypt error",
72
                        detail: "bcrypt error"
73
                    }
74
                });
75
            }
76
77
            if (result) {
78
                let payload = { email: user.email };
79
                let jwtToken = jwt.sign(payload, jwtSecret, { expiresIn: '24h' });
80
81
                return res.json({
82
                    data: {
83
                        type: "success",
84
                        message: "User logged in",
85
                        user: payload,
86
                        token: jwtToken
87
                    }
88
                });
89
            }
90
91
            return res.status(401).json({
92
                errors: {
93
                    status: 401,
94
                    source: "/login",
95
                    title: "Wrong password",
96
                    detail: "Password is incorrect."
97
                }
98
            });
99
        });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
100
    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
101
}
102
103
module.exports = router;
104