`).join('')}
+ title: 'Account & Profile Settings',
+ sub: `@${cur.username} ยท ${cur.role_display}`,
+ wide: 1,
+ body: `
+
+
${getPreviewHtml(curImgUrl, curColor, cur.name)}
+
+
${esc(cur.name || cur.username)}
+
+ ${esc(cur.role_display)}
+ @${esc(cur.username)}
-
-
-
-
-
-
PNG, JPG or WebP. Max 5MB.
+
+
+
+
- ${fld('name','Full name',{req:1,v:cur.name})}
-
-
-
+
+
+
+
+ ${colors.map(c => `
+
+ `).join('')}
+
+
+
+
+
+
+
+
+
+
`,
- ok:'Save Changes',
- alt:['Sign out',async()=>{await api('/khira/api/auth/logout','POST');localStorage.removeItem('khira_active_user');location.reload()}],
- init:f=>{
- updatePreview();
- const tabs=f.querySelectorAll('#avModeTabs button');
- const paneE=f.querySelector('#paneEmoji');
- const paneI=f.querySelector('#paneImage');
- const hidType=f.querySelector('#hidAvType');
- const hidEmoji=f.querySelector('#hidAvEmoji');
- const hidImg=f.querySelector('#hidAvImg');
+ ok: 'Save Changes',
+ alt: ['Sign out', async () => {
+ await api('/khira/api/auth/logout', 'POST');
+ localStorage.removeItem('khira_session_token');
+ localStorage.removeItem('khira_active_user');
+ location.reload();
+ }],
+ init: f => {
+ const prev = f.querySelector('#profileAvatarPreview');
+ const fileInp = f.querySelector('#profileFileInput');
+ const btnUp = f.querySelector('#btnUploadPhoto');
+ const btnInits = f.querySelector('#btnUseInitials');
+ const hidImg = f.querySelector('#hidProfImg');
+ const hidColor = f.querySelector('#hidProfColor');
+ const hidType = f.querySelector('#hidProfType');
+ const nameInp = f.querySelector('#profNameInp');
+ const colorSec = f.querySelector('#initialsColorSection');
- tabs.forEach(btn=>{
- btn.onclick=()=>{
- tabs.forEach(b=>b.classList.remove('on'));
- btn.classList.add('on');
- curType=btn.dataset.avm;
- hidType.value=curType;
- if(curType==='emoji'){
- paneE.hidden=false;paneI.hidden=true;
- }else{
- paneE.hidden=true;paneI.hidden=false;
- }
- updatePreview();
+ const refreshPreview = () => {
+ prev.innerHTML = getPreviewHtml(curImgUrl, curColor, nameInp.value);
+ colorSec.hidden = !!curImgUrl;
+ };
+
+ nameInp.addEventListener('input', refreshPreview);
+
+ btnUp.onclick = () => fileInp.click();
+
+ btnInits.onclick = () => {
+ curImgUrl = '';
+ curType = 'initials';
+ hidImg.value = '';
+ hidType.value = 'initials';
+ refreshPreview();
+ toast('Switched to initials avatar');
+ };
+
+ f.querySelectorAll('.color-swatch-btn').forEach(b => {
+ b.onclick = () => {
+ f.querySelectorAll('.color-swatch-btn').forEach(btn => btn.classList.remove('active'));
+ b.classList.add('active');
+ curColor = b.dataset.color;
+ hidColor.value = curColor;
+ refreshPreview();
};
});
- f.querySelectorAll('.av-opt').forEach(opt=>{
- opt.onclick=()=>{
- f.querySelectorAll('.av-opt').forEach(b=>b.classList.remove('on'));
- opt.classList.add('on');
- curEmoji=opt.dataset.emoji;
- hidEmoji.value=curEmoji;
- curType='emoji';
- hidType.value='emoji';
- updatePreview();
- };
- });
-
- const fileInp=f.querySelector('#avFileInput');
- if(fileInp){
- fileInp.onchange=async()=>{
- const file=fileInp.files[0];
- if(!file)return;
- const fd=new FormData();
- fd.append('file',file);
- try{
- toast('Uploading avatar photo...');
- const un=S.u?.username||ls('khira_active_user');
- const tok=ls('khira_session_token');
- const upHeaders={'X-Khira-User':un};
- if(tok)upHeaders['Authorization']='Bearer '+tok;
- const upRes=await fetch('/khira/api/upload',{method:'POST',headers:upHeaders,credentials:'same-origin',body:fd});
- const upJson=await upRes.json();
- const url=upJson.url||upJson.attachment?.url;
- if(url){
- curImgUrl=url;
- hidImg.value=curImgUrl;
- curType='image';
- hidType.value='image';
- tabs.forEach(b=>b.classList.toggle('on',b.dataset.avm==='image'));
- paneE.hidden=true;paneI.hidden=false;
- updatePreview();
- toast('Avatar uploaded');
- }
- }catch(err){
- console.error('Failed to upload avatar',err);
- toast('Failed to upload image','err');
+ fileInp.onchange = async () => {
+ const file = fileInp.files[0];
+ if (!file) return;
+ const fd = new FormData();
+ fd.append('file', file);
+ try {
+ toast('Uploading avatar photo...');
+ const un = S.u?.username || ls('khira_active_user');
+ const tok = ls('khira_session_token');
+ const upHeaders = { 'X-Khira-User': un };
+ if (tok) upHeaders['Authorization'] = 'Bearer ' + tok;
+ const upRes = await fetch('/khira/api/upload', {
+ method: 'POST',
+ headers: upHeaders,
+ credentials: 'same-origin',
+ body: fd
+ });
+ const upJson = await upRes.json();
+ const url = upJson.url || upJson.attachment?.url;
+ if (url) {
+ curImgUrl = url;
+ curType = 'image';
+ hidImg.value = url;
+ hidType.value = 'image';
+ refreshPreview();
+ toast('Avatar uploaded successfully');
+ } else {
+ toast(upJson.error || 'Failed to upload image', 'err');
}
- };
- }
+ } catch (err) {
+ toast('Failed to upload image', 'err');
+ }
+ };
+
+ // Toggle Security accordion
+ const toggleBtn = f.querySelector('#btnToggleSecurity');
+ const secBody = f.querySelector('#securityBody');
+ toggleBtn.onclick = () => {
+ secBody.hidden = !secBody.hidden;
+ };
},
- go:async v=>{
- const r=await api('/khira/api/auth/profile','PUT',{
- name:v.name,
- avatar_type:v.avatar_type,
- avatar:v.avatar,
- profile_image:v.profile_image
- });
- if(r._f){toast(r.error||'Failed to update profile','err');return false}
- S.u=r.user||{...S.u,...v};
+ go: async v => {
+ // Validate password if supplied
+ if (v.new_password) {
+ if (!v.current_password) {
+ toast('Please enter your current password to set a new password', 'err');
+ return false;
+ }
+ if (v.new_password.length < 6) {
+ toast('New password must be at least 6 characters', 'err');
+ return false;
+ }
+ if (v.new_password !== v.confirm_password) {
+ toast('New passwords do not match', 'err');
+ return false;
+ }
+ }
+
+ const payload = {
+ name: v.name,
+ avatar_type: v.avatar_type || curType,
+ avatar: cur.avatar || '๐ค',
+ profile_image: v.profile_image
+ };
+ if (v.new_password) {
+ payload.current_password = v.current_password;
+ payload.new_password = v.new_password;
+ }
+
+ const r = await api('/khira/api/auth/profile', 'PUT', payload);
+ if (r._f) {
+ toast(r.error || 'Failed to update profile', 'err');
+ return false;
+ }
+
+ S.u = r.user || { ...S.u, ...payload };
updateMe(S.u);
- toast('Profile updated');
+ toast('Profile updated successfully');
await refresh();
}
});
diff --git a/web/khira/index.html b/web/khira/index.html
index 9d04ee2..c8ca3d8 100644
--- a/web/khira/index.html
+++ b/web/khira/index.html
@@ -9,7 +9,7 @@
-
+
@@ -447,6 +447,6 @@
-
+