routes/register.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 60
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 31
mnd 2
bc 2
fnc 5
dl 0
loc 60
rs 10
bpm 0.4
cpm 1.4
noi 4
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B register.js ➔ register 0 30 7
1
var express = require('express');
2
var router = express.Router();
3
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...
4
// const db = new sqlite3.Database('./db/texts.sqlite');
5
const db = require("../db/database.js");
6
const bcrypt = require('bcryptjs');
7
8
9
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...
10
    const data = {
11
        data: {
12
            msg:  "Register a user"
13
        }
14
    };
15
16
    res.json(data);
17
});
18
19
router.post("/", (req, res) => {
20
    register(res, req.body);
21
22
    // res.status(201).json({
23
    //     data: {
24
    //         msg: "Got a POST request"
25
    //     }
26
    // });
27
});
28
29
function register(res, body) {
30
    const email = body.email;
31
    const password = body.password;
32
33
    if (!email || !password) {
34
        return res.status(401).json({
35
            errors: {
36
                status: 401,
37
                source: "/register",
38
                title: "Email or password missing",
39
                detail: "Email or password missing in request"
40
            }
41
        });
42
    }
43
    bcrypt.hash(password, 10, function(err, hash) {
44
45
        db.run("INSERT INTO users (email, password) VALUES (?, ?)",
46
            email,
47
            hash, (err) => {
48
                if (err) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
49
                    // console.log(err);
50
                }
51
                return res.status(201).json({
52
                    data: {
53
                        message: "User successfully registered."
54
                    }
55
                });
56
            });
57
    });
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...
58
}
59
60
module.exports = router;
61