UIWindowNewPassword.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import UIWindow from './UIWindow.js'
  20. import UIAlert from './UIAlert.js'
  21. import UIWindowLogin from './UIWindowLogin.js'
  22. async function UIWindowNewPassword(options){
  23. return new Promise(async (resolve) => {
  24. options = options ?? {};
  25. const internal_id = window.uuidv4();
  26. let h = '';
  27. h += `<div class="change-password" style="padding: 20px; border-bottom: 1px solid #ced7e1;">`;
  28. // error msg
  29. h += `<div class="form-error-msg"></div>`;
  30. // success msg
  31. h += `<div class="form-success-msg"></div>`;
  32. // new password
  33. h += `<div style="overflow: hidden; margin-top: 20px; margin-bottom: 20px;">`;
  34. h += `<label for="new-password-${internal_id}">New Password</label>`;
  35. h += `<input class="new-password" id="new-password-${internal_id}" type="password" name="new-password" autocomplete="off" />`;
  36. h += `</div>`;
  37. // confirm new password
  38. h += `<div style="overflow: hidden; margin-top: 20px; margin-bottom: 20px;">`;
  39. h += `<label for="confirm-new-password-${internal_id}">Confirm New Password</label>`;
  40. h += `<input class="confirm-new-password" id="confirm-new-password-${internal_id}" type="password" name="confirm-new-password" autocomplete="off" />`;
  41. h += `</div>`;
  42. // Change Password
  43. h += `<button class="change-password-btn button button-primary button-block button-normal">Set New Password</button>`;
  44. h += `</div>`;
  45. const el_window = await UIWindow({
  46. title: 'Set New Password',
  47. app: 'change-passowrd',
  48. single_instance: true,
  49. icon: null,
  50. uid: null,
  51. is_dir: false,
  52. body_content: h,
  53. has_head: true,
  54. selectable_body: false,
  55. draggable_body: false,
  56. allow_context_menu: false,
  57. is_resizable: false,
  58. is_droppable: false,
  59. init_center: true,
  60. allow_native_ctxmenu: false,
  61. allow_user_select: false,
  62. width: 350,
  63. height: 'auto',
  64. dominant: true,
  65. show_in_taskbar: false,
  66. onAppend: function(this_window){
  67. $(this_window).find(`.new-password`).get(0)?.focus({preventScroll:true});
  68. },
  69. window_class: 'window-publishWebsite',
  70. body_css: {
  71. width: 'initial',
  72. height: '100%',
  73. 'background-color': 'rgb(245 247 249)',
  74. 'backdrop-filter': 'blur(3px)',
  75. }
  76. })
  77. $(el_window).find('.change-password-btn').on('click', function(e){
  78. const new_password = $(el_window).find('.new-password').val();
  79. const confirm_new_password = $(el_window).find('.confirm-new-password').val();
  80. if(new_password === '' || confirm_new_password === ''){
  81. $(el_window).find('.form-error-msg').html('All fields are required.');
  82. $(el_window).find('.form-error-msg').fadeIn();
  83. return;
  84. }
  85. else if(new_password !== confirm_new_password){
  86. $(el_window).find('.form-error-msg').html('`New Password` and `Confirm New Password` do not match.');
  87. $(el_window).find('.form-error-msg').fadeIn();
  88. return;
  89. }
  90. $(el_window).find('.form-error-msg').hide();
  91. $.ajax({
  92. url: api_origin + "/set-pass-using-token",
  93. type: 'POST',
  94. async: true,
  95. contentType: "application/json",
  96. data: JSON.stringify({
  97. password: new_password,
  98. token: options.token,
  99. user_id: options.user,
  100. }),
  101. success: async function (data){
  102. $(el_window).close();
  103. await UIAlert({
  104. message: 'Password changed successfully.',
  105. body_icon: window.icons['c-check.svg'],
  106. stay_on_top: true,
  107. backdrop: true,
  108. buttons:[
  109. {
  110. label: 'Proceed to Login',
  111. type: 'primary',
  112. },
  113. ],
  114. window_options: {
  115. backdrop: true,
  116. close_on_backdrop_click: false,
  117. }
  118. })
  119. await UIWindowLogin({
  120. reload_on_success: true,
  121. window_options:{
  122. has_head: false
  123. }
  124. });
  125. },
  126. error: function (err){
  127. $(el_window).find('.form-error-msg').html(err.responseText);
  128. $(el_window).find('.form-error-msg').fadeIn();
  129. }
  130. });
  131. })
  132. })
  133. }
  134. export default UIWindowNewPassword