File size: 5,247 Bytes
b72f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * Copyright (C) 2024 Puter Technologies Inc.
 *
 * This file is part of Puter.
 *
 * Puter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import UIWindow from './UIWindow.js'

async function UIWindowChangePassword(){
    const internal_id = window.uuidv4();
    let h = '';
    h += `<div class="change-password" style="padding: 20px; border-bottom: 1px solid #ced7e1;">`;
        // error msg
        h += `<div class="form-error-msg"></div>`;
        // success msg
        h += `<div class="form-success-msg"></div>`;
        // current password
        h += `<div style="overflow: hidden; margin-bottom: 20px;">`;
            h += `<label for="current-password-${internal_id}">Current Password</label>`;
            h += `<input id="current-password-${internal_id}" class="current-password" type="password" name="current-password" autocomplete="current-password" />`;
        h += `</div>`;
        // new password
        h += `<div style="overflow: hidden; margin-top: 20px; margin-bottom: 20px;">`;
            h += `<label for="new-password-${internal_id}">New Password</label>`;
            h += `<input id="new-password-${internal_id}" type="password" class="new-password" name="new-password" autocomplete="off" />`;
        h += `</div>`;
        // confirm new password
        h += `<div style="overflow: hidden; margin-top: 20px; margin-bottom: 20px;">`;
            h += `<label for="confirm-new-password-${internal_id}">Confirm New Password</label>`;
            h += `<input id="confirm-new-password-${internal_id}" type="password" name="confirm-new-password" class="confirm-new-password" autocomplete="off" />`;
        h += `</div>`;

        // Change Password
        h += `<button class="change-password-btn button button-primary button-block button-normal">Change Password</button>`;
    h += `</div>`;

    const el_window = await UIWindow({
        title: 'Change Password',
        app: 'change-passowrd',
        single_instance: true,
        icon: null,
        uid: null,
        is_dir: false,
        body_content: h,
        has_head: true,
        selectable_body: false,
        draggable_body: false,
        allow_context_menu: false,
        is_resizable: false,
        is_droppable: false,
        init_center: true,
        allow_native_ctxmenu: false,
        allow_user_select: false,
        width: 350,
        height: 'auto',
        dominant: true,
        show_in_taskbar: false,
        onAppend: function(this_window){
            $(this_window).find(`.current-password`).get(0).focus({preventScroll:true});
        },
        window_class: 'window-publishWebsite',
        body_css: {
            width: 'initial',
            height: '100%',
            'background-color': 'rgb(245 247 249)',
            'backdrop-filter': 'blur(3px)',
        }    
    })

    $(el_window).find('.change-password-btn').on('click', function(e){
        const current_password = $(el_window).find('.current-password').val();
        const new_password = $(el_window).find('.new-password').val();
        const confirm_new_password = $(el_window).find('.confirm-new-password').val();

        let data;

        if(current_password === '' || new_password === '' || confirm_new_password === ''){
            $(el_window).find('.form-error-msg').html('All fields are required.');
            $(el_window).find('.form-error-msg').fadeIn();
            return;
        }
        else if(new_password !== confirm_new_password){
            $(el_window).find('.form-error-msg').html('`New Password` and `Confirm New Password` do not match.');
            $(el_window).find('.form-error-msg').fadeIn();
            return;
        }
        
        $(el_window).find('.form-error-msg').hide();
    
        $.ajax({
            url: api_origin + "/passwd",
            type: 'POST',
            async: true,
            headers: {
                "Authorization": "Bearer "+auth_token
            },
            contentType: "application/json",
            data: JSON.stringify({ 
                old_pass: current_password, 
                new_pass: new_password,
            }),				
            success: function (data){
                $(el_window).find('.form-success-msg').html('Password changed successfully.');
                $(el_window).find('.form-success-msg').fadeIn();
                $(el_window).find('input').val('');
            },
            error: function (err){
                $(el_window).find('.form-error-msg').html(err.responseText);
                $(el_window).find('.form-error-msg').fadeIn();
            }
        });	
    })
}

export default UIWindowChangePassword