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

Commit 9836253

Browse files
committed
Add the change-name route
1 parent d65fafa commit 9836253

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

logic/auth.js

Lines changed: 53 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,47 @@ 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 = 40; // eslint-disable-line no-magic-numbers
56+
57+
let complete = false;
58+
try {
59+
// get user data
60+
const user = await diskLogic.readUserFile();
61+
62+
// update user name
63+
user.name = name;
64+
65+
// update user file
66+
await diskLogic.writeUserFile({ ...user });
67+
68+
complete = true;
69+
70+
changeNameStatus.percent = 100;
71+
} catch (error) {
72+
if (error.response.status === constants.STATUS_CODES.UNAUTHORIZED) {
73+
changeNameStatus.unauthorized = true;
74+
} else {
75+
changeNameStatus.error = true;
76+
changeNameStatus.percent = 100;
77+
78+
throw error;
79+
}
80+
}
81+
82+
if (!complete) {
83+
changeNameStatus.error = true;
84+
changeNameStatus.percent = 100;
85+
86+
throw new Error('Unable to change name');
87+
}
88+
}
89+
4390
// Change the device and lnd password.
4491
async function changePassword(currentPassword, newPassword, jwt) {
4592

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

118165
}
119166

167+
function getChangeNameStatus() {
168+
return changeNameStatus;
169+
}
170+
120171
function getChangePasswordStatus() {
121172
return changePasswordStatus;
122173
}
@@ -266,8 +317,10 @@ async function refresh(user) {
266317

267318

268319
module.exports = {
320+
changeName,
269321
changePassword,
270322
getCachedPassword,
323+
getChangeNameStatus,
271324
getChangePasswordStatus,
272325
hashCredentials,
273326
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)