Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Add user settings API + Logic #58

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions logic/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const JWTHelper = require('utils/jwt.js');
const constants = require('utils/const.js');
const UUID = require('utils/UUID.js');

const defaultSettings = require('resources/db-default-settings.json');

const saltRounds = 10;
const SYSTEM_USER = UUID.fetchBootUUID() || 'admin';

Expand Down Expand Up @@ -199,6 +201,38 @@ async function getInfo() {
}
};

async function getSettings() {
try {
const { settings } = await diskLogic.readUserFile();

return { ...defaultSettings, ...settings };
} catch (error) {
throw new NodeError('Unable to get account settings');
}
};

async function updateSetting(setting, value) {
try {
const settings = await getSettings();

if(!setting) return settings;

if(typeof value !== undefined) {
settings[setting] = value;
} else {
delete settings[setting];
}

const user = await diskLogic.readUserFile();
user.settings = settings;
await diskLogic.writeUserFile(user);

return settings;
} catch (error) {
throw new NodeError(`Unable to update setting`);
}
};

async function seed(user) {

//Decrypt mnemonic seed
Expand Down Expand Up @@ -228,9 +262,9 @@ async function register(user, seed) {
throw new NodeError('Unable to encrypt mnemonic seed');
}

//save user
//save user and init settings
try {
await diskLogic.writeUserFile({ name: user.name, password: user.password, seed: encryptedSeed });
await diskLogic.writeUserFile({ name: user.name, password: user.password, seed: encryptedSeed, settings: {} });
} catch (error) {
throw new NodeError('Unable to register user');
}
Expand Down Expand Up @@ -289,6 +323,8 @@ module.exports = {
hashCredentials,
isRegistered,
getInfo,
getSettings,
updateSetting,
seed,
login,
register,
Expand Down
10 changes: 0 additions & 10 deletions logic/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ async function readUserFile() {
return {...defaultProperties, ...userFile};
}

function readSettingsFile() {
return diskService.readJsonFile(constants.SETTINGS_FILE);
}

function writeSettingsFile(json) {
return diskService.writeJsonFile(constants.SETTINGS_FILE, json);
}

async function writeUserFile(json) {
return diskService.writeJsonFile(constants.USER_FILE, json);
}
Expand Down Expand Up @@ -256,10 +248,8 @@ module.exports = {
fileExists,
getBuildDetails,
listVersionsForApp,
readSettingsFile,
readUserFile,
writeAppVersionFile,
writeSettingsFile,
writeUserFile,
writeUmbrelSeedFile,
umbrelSeedFileExists,
Expand Down
3 changes: 3 additions & 0 deletions resources/db-default-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"currency": "USD"
}
18 changes: 18 additions & 0 deletions routes/v1/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ router.get('/info', auth.jwt, safeHandler(async (req, res) => {
return res.status(constants.STATUS_CODES.OK).json(info);
}));

router.get('/settings', auth.jwt, safeHandler(async (req, res) => {
const settings = await authLogic.getSettings();

return res.status(constants.STATUS_CODES.OK).json(settings);
}));

router.post('/settings', auth.jwt, safeHandler(async (req, res) => {
const { setting, value } = req.body;

try {
const settings = await authLogic.updateSetting(setting, value);

return res.status(constants.STATUS_CODES.OK).json(settings);
} catch (error) {
return next(error);
}
}));

router.post('/seed', auth.convertReqBodyToBasicAuth, auth.basic, incorrectPasswordAuthHandler, safeHandler(async (req, res) => {
const seed = await authLogic.seed(req.user);

Expand Down