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

Commit 377b9bb

Browse files
committed
Add the change-name route
Verify that name is alphanum+space & remove condition in catch Remove alphanum+space verification Update percentage
1 parent d65fafa commit 377b9bb

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

logic/auth.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ const saltRounds = 10;
1616
const SYSTEM_USER = UUID.fetchBootUUID() || 'admin';
1717

1818
let devicePassword = '';
19+
let changeNameStatus;
1920
let changePasswordStatus;
2021

22+
resetChangeNameStatus();
2123
resetChangePasswordStatus();
2224

2325
function resetChangePasswordStatus() {
2426
changePasswordStatus = { percent: 0 };
2527
}
2628

29+
function resetChangeNameStatus() {
30+
changeNameStatus = { percent: 0 };
31+
}
32+
2733
async function sleepSeconds(seconds) {
2834
return new Promise(resolve => {
2935
setTimeout(resolve, seconds * constants.TIME.ONE_SECOND_IN_MILLIS);
@@ -40,6 +46,34 @@ function getCachedPassword() {
4046
return devicePassword;
4147
}
4248

49+
// Change the device name
50+
async function changeName(name) {
51+
resetChangeNameStatus();
52+
53+
changeNameStatus.percent = 1; // eslint-disable-line no-magic-numbers
54+
55+
changeNameStatus.percent = 30; // eslint-disable-line no-magic-numbers
56+
57+
try {
58+
// get user data
59+
const user = await diskLogic.readUserFile();
60+
changeNameStatus.percent = 60; // eslint-disable-line no-magic-numbers
61+
62+
// update user name
63+
user.name = name;
64+
65+
// update user file
66+
await diskLogic.writeUserFile({ ...user });
67+
68+
changeNameStatus.percent = 100;
69+
} catch (error) {
70+
changeNameStatus.error = true;
71+
changeNameStatus.percent = 100;
72+
73+
throw error;
74+
}
75+
}
76+
4377
// Change the device and lnd password.
4478
async function changePassword(currentPassword, newPassword, jwt) {
4579

@@ -117,6 +151,10 @@ async function changePassword(currentPassword, newPassword, jwt) {
117151

118152
}
119153

154+
function getChangeNameStatus() {
155+
return changeNameStatus;
156+
}
157+
120158
function getChangePasswordStatus() {
121159
return changePasswordStatus;
122160
}
@@ -266,8 +304,10 @@ async function refresh(user) {
266304

267305

268306
module.exports = {
307+
changeName,
269308
changePassword,
270309
getCachedPassword,
310+
getChangeNameStatus,
271311
getChangePasswordStatus,
272312
hashCredentials,
273313
isRegistered,

routes/v1/account.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ const validator = require('utils/validator.js');
1313

1414
const COMPLETE = 100;
1515

16+
router.post('/change-name', auth.jwt, safeHandler(async (req, res, next) => {
17+
const newName = req.body.newName;
18+
19+
try {
20+
validator.isString(newName);
21+
} catch (error) {
22+
return next(error);
23+
}
24+
25+
const status = await authLogic.getChangeNameStatus();
26+
27+
// return a conflict if a change name process is already running
28+
if (status.percent > 0 && status.percent !== COMPLETE) {
29+
return res.status(constants.STATUS_CODES.CONFLICT).json();
30+
}
31+
32+
try {
33+
// start change name process in the background and immediately return
34+
await authLogic.changeName(newName);
35+
return res.status(constants.STATUS_CODES.OK).json();
36+
} catch (error) {
37+
return next(error);
38+
}
39+
}));
40+
1641
// Endpoint to change your lnd password. Wallet must exist and be unlocked.
1742
router.post('/change-password', auth.convertReqBodyToBasicAuth, auth.basic, incorrectPasswordAuthHandler, safeHandler(async (req, res, next) => {
1843
// Use password from the body by default. Basic auth has issues handling special characters.

0 commit comments

Comments
 (0)