@@ -16,14 +16,20 @@ const saltRounds = 10;
1616const SYSTEM_USER = UUID . fetchBootUUID ( ) || 'admin' ;
1717
1818let devicePassword = '' ;
19+ let changeNameStatus ;
1920let changePasswordStatus ;
2021
22+ resetChangeNameStatus ( ) ;
2123resetChangePasswordStatus ( ) ;
2224
2325function resetChangePasswordStatus ( ) {
2426 changePasswordStatus = { percent : 0 } ;
2527}
2628
29+ function resetChangeNameStatus ( ) {
30+ changeNameStatus = { percent : 0 } ;
31+ }
32+
2733async 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.
4491async 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+
120171function getChangePasswordStatus ( ) {
121172 return changePasswordStatus ;
122173}
@@ -266,8 +317,10 @@ async function refresh(user) {
266317
267318
268319module . exports = {
320+ changeName,
269321 changePassword,
270322 getCachedPassword,
323+ getChangeNameStatus,
271324 getChangePasswordStatus,
272325 hashCredentials,
273326 isRegistered,
0 commit comments