/** * 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 . */ import UIWindow from './UIWindow.js' import UIWindowSignup from './UIWindowSignup.js' import UIWindowRecoverPassword from './UIWindowRecoverPassword.js' async function UIWindowLogin(options){ options = options ?? {}; options.reload_on_success = options.reload_on_success ?? false; options.has_head = options.has_head ?? true; options.send_confirmation_code = options.send_confirmation_code ?? false; options.show_password = options.show_password ?? false; return new Promise(async (resolve) => { const internal_id = window.uuidv4(); let h = ``; h += `
`; if(!options.has_head && options.show_close_button !== false) h += `
×
`; h += `
`; // title h += `

Log In

`; // login form h += ``; h += `
`; // create account link if(options.show_signup_button === undefined || options.show_signup_button){ h += `
`; h += ``; h += `
`; } h += `
`; const el_window = await UIWindow({ title: null, app: 'login', 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_draggable: options.is_draggable ?? true, is_droppable: false, is_resizable: false, stay_on_top: false, allow_native_ctxmenu: true, allow_user_select: true, ...options.window_options, width: 350, dominant: true, on_close: ()=>{ resolve(false) }, onAppend: function(this_window){ $(this_window).find(`.email_or_username`).get(0).focus({preventScroll:true}); }, window_class: 'window-login', window_css:{ height: 'initial', }, body_css: { width: 'initial', padding: '0', 'background-color': 'rgb(255 255 255)', 'backdrop-filter': 'blur(3px)', 'display': 'flex', 'flex-direction': 'column', 'justify-content': 'center', 'align-items': 'center', } }) $(el_window).find('.forgot-password-link').on('click', function(e){ UIWindowRecoverPassword({ window_options: { backdrop: true, close_on_backdrop_click: false, } }); }) $(el_window).find('.login-btn').on('click', function(e){ const email_username = $(el_window).find('.email_or_username').val(); const password = $(el_window).find('.password').val(); let data; if(is_email(email_username)){ data = JSON.stringify({ email: email_username, password: password }) }else{ data = JSON.stringify({ username: email_username, password: password }) } $(el_window).find('.login-error-msg').hide(); let headers = {}; if(window.custom_headers) headers = window.custom_headers; $.ajax({ url: gui_origin + "/login", type: 'POST', async: false, headers: headers, contentType: "application/json", data: data, success: function (data){ update_auth_data(data.token, data.user); if(options.reload_on_success){ window.onbeforeunload = null; window.location.replace('/'); }else resolve(true); $(el_window).close(); }, error: function (err){ $(el_window).find('.login-error-msg').html(err.responseText); $(el_window).find('.login-error-msg').fadeIn(); } }); }) $(el_window).find('.login-form').on('submit', function(e){ e.preventDefault(); e.stopPropagation(); return false; }) $(el_window).find('.signup-c2a-clickable').on('click', async function(e){ //destroy this window $(el_window).close(); // create Signup window const signup = await UIWindowSignup({ referrer: options.referrer, show_close_button: options.show_close_button, reload_on_success: options.reload_on_success, window_options: options.window_options, send_confirmation_code: options.send_confirmation_code, }); if(signup) resolve(true); }) $(el_window).find(`#toggle-show-password-${internal_id}`).on("click", function (e) { options.show_password = !options.show_password; // hide/show password and update icon $(el_window).find(".password").attr("type", options.show_password ? "text" : "password"); $(el_window).find(".toggle-show-password-icon").attr("src", options.show_password ? window.icons["eye-closed.svg"] : window.icons["eye-open.svg"], ) }) }) } export default UIWindowLogin