aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/users.js
blob: 79fe22f38e6fc375090afcefa570ab672184627f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import express from 'express';
import * as userCtrl from '../controllers/userController.js';

const router = express.Router();

const USERNAME_MIN = 3;
const USERNAME_MAX = 30;
const DISPLAY_NAME_MAX = 100;
const DEFAULT_LIMIT = 50;
const MAX_LIMIT = 200;

function isValidUsername(username) {
    if (typeof username !== 'string') {
        return false;
    }
    if (username.length < USERNAME_MIN || username.length > USERNAME_MAX) {
        return false;
    }
    const allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
    for (let i = 0; i < username.length; i++) {
        if (allowed.indexOf(username[i]) === -1) {
            return false;
        }
    }
    return true;
}

function isValidDisplayName(displayName) {
    if (displayName === undefined || displayName === null) {
        return true;
    }
    if (typeof displayName !== 'string') {
        return false;
    }
    return displayName.length <= DISPLAY_NAME_MAX;
}

// Search users (must be before /:id)
router.get('/search', async function(req, res) {
    const q = req.query.q;
    if (typeof q !== 'string' || q.length < 1) {
        return res.status(400).json({ error: 'invalid_query' });
    }
    const results = await userCtrl.searchUsers({ q: q, fields: ['username', 'displayName'] });
    res.json(results);
});

// List users with pagination
router.get('/', async function(req, res) {
    let limit = parseInt(req.query.limit, 10);
    let offset = parseInt(req.query.offset, 10);

    if (isNaN(limit) || limit < 1) {
        limit = DEFAULT_LIMIT;
    }
    if (limit > MAX_LIMIT) {
        limit = MAX_LIMIT;
    }
    if (isNaN(offset) || offset < 0) {
        offset = 0;
    }

    const users = await userCtrl.listUsers({ limit: limit, offset: offset });
    res.json(users);
});

// Get user by id
router.get('/:id', async function(req, res) {
    const user = await userCtrl.getUserById(req.params.id);
    if (!user) {
        return res.status(404).json({ error: 'not_found' });
    }
    res.json(user);
});

// Create user
router.post('/', async function(req, res) {
    const username = req.body.username;
    const displayName = req.body.displayName;

    if (!isValidUsername(username)) {
        return res.status(400).json({ error: 'invalid_username' });
    }
    if (!isValidDisplayName(displayName)) {
        return res.status(400).json({ error: 'invalid_display_name' });
    }

    const user = await userCtrl.createUser({ username: username, displayName: displayName });
    res.status(201).json(user);
});

// Update user
router.put('/:id', async function(req, res) {
    const username = req.body.username;
    const displayName = req.body.displayName;

    if (username !== undefined && !isValidUsername(username)) {
        return res.status(400).json({ error: 'invalid_username' });
    }
    if (!isValidDisplayName(displayName)) {
        return res.status(400).json({ error: 'invalid_display_name' });
    }

    const updates = {};
    if (username !== undefined) {
        updates.username = username;
    }
    if (displayName !== undefined) {
        updates.displayName = displayName;
    }

    const user = await userCtrl.updateUser(req.params.id, updates);
    if (!user) {
        return res.status(404).json({ error: 'not_found' });
    }
    res.json(user);
});

// Delete user
router.delete('/:id', async function(req, res) {
    const ok = await userCtrl.deleteUser(req.params.id);
    if (!ok) {
        return res.status(404).json({ error: 'not_found' });
    }
    res.status(204).send();
});

// Login - get token by username
router.post('/login', async function(req, res) {
    const username = req.body.username;

    if (!isValidUsername(username)) {
        return res.status(400).json({ error: 'invalid_username' });
    }

    const user = await userCtrl.getUserByUsername(username);
    if (!user) {
        return res.status(404).json({ error: 'user_not_found' });
    }

    res.json({ token: user.token, userId: user._id });
});

export default router;