blob: 0798fa7d3a03e2a1451ab9a9a024d1455b9b5b35 [file] [log] [blame]
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -04001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { Router } from 'express';
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040019import { AccountDetails } from 'jami-web-common';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040020import { Container } from 'typedi';
21
Misha Krieger-Raynauldaddd6fe2022-10-22 12:46:04 -040022import { Jamid } from '../jamid/jamid.js';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040023import { authenticateToken } from '../middleware/auth.js';
24
25const jamid = Container.get(Jamid);
26
27export const accountRouter = Router();
28
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040029accountRouter.use(authenticateToken);
30
31// TODO: If tokens can be generated on one daemon and used on another (transferrable between daemons),
32// then add middleware to check that the currently logged-in accountId is stored in this daemon instance
33
34accountRouter.get('/', (_req, res) => {
35 const accountId = res.locals.accountId;
36
37 res.json({
38 id: accountId,
39 details: jamid.getAccountDetails(accountId),
40 volatileDetails: jamid.getVolatileAccountDetails(accountId),
41 defaultModerators: jamid.getDefaultModerators(accountId),
42 devices: jamid.getDevices(accountId),
43 });
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040044});
45
Misha Krieger-Raynauldb6f1c322022-10-23 20:42:57 -040046accountRouter.post('/', (req, res) => {
47 const accountId = res.locals.accountId;
48 const currentAccountDetails = jamid.getAccountDetails(accountId);
49 const newAccountDetails: AccountDetails = { ...currentAccountDetails, ...req.body };
50 jamid.setAccountDetails(res.locals.accountId, newAccountDetails);
51 res.end();
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040052});