| 1 | // This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt |
| 2 | |
| 3 | import { state, useSnapState } from './state' |
| 4 | import { createElement as h } from 'react' |
| 5 | import { alertDialog, newDialog, promptDialog } from './dialog' |
| 6 | import { apiCall } from '@hfs/shared/api' |
| 7 | import { logout } from './login' |
| 8 | import { Btn, CustomCode } from './components' |
| 9 | import { formatTimestamp, hIcon, fallbackToBasicAuth, working, apiNewPassword } from './misc' |
| 10 | import i18n from './i18n' |
| 11 | const { t } = i18n |
| 12 | |
| 13 | export default function showUserPanel() { |
| 14 | const { close } = newDialog({ |
| 15 | title: t`User panel`, |
| 16 | className: 'user-dialog', |
| 17 | icon: () => hIcon('user'), |
| 18 | Content() { |
| 19 | const snap = useSnapState() |
| 20 | return h('div', { id: 'user-panel' }, |
| 21 | h('div', {}, t`Username`, ': ', snap.username), |
| 22 | snap.accountExp && h('div', {}, t`Account expiration`, ': ', formatTimestamp(snap.accountExp)), |
| 23 | h(CustomCode, { name: 'userPanelAfterInfo' }), |
| 24 | snap.canChangePassword && !fallbackToBasicAuth() && h(Btn, { |
| 25 | icon: 'password', |
| 26 | label: t`Change password`, |
| 27 | id: 'change-password', |
| 28 | onClickAnimation: false, |
| 29 | onClick: changePassword, |
| 30 | }), |
| 31 | h(Btn, { |
| 32 | icon: 'logout', |
| 33 | label: t`Logout`, |
| 34 | id: 'logout', |
| 35 | onClick() { |
| 36 | if (fallbackToBasicAuth()) // this is effective on ff52, but not on chrome125 |
| 37 | return location.href = `//LOGOUT%00:@${location.host}/?get=logout` // redirect, to execute the body content |
| 38 | logout().then(close, alertDialog) |
| 39 | } |
| 40 | }) |
| 41 | ) |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | export async function changePassword(required=false) { |
| 47 | const pwd = await promptDialog(t('enter_pass', "Enter new password"), { |
| 48 | type: 'password', |
| 49 | helperText: required && t('required_change_password', "You are required to change your password") |
| 50 | }) |
| 51 | if (!pwd) return |
| 52 | const check = await promptDialog(t('enter_pass2', "Re-enter the same new password"), { type: 'password' }) |
| 53 | if (!check) return |
| 54 | if (check !== pwd) |
| 55 | return alertDialog(t('pass2_mismatch', "The second password you entered did not match the first. Procedure aborted."), 'warning') |
| 56 | |
| 57 | const modal = working() |
| 58 | await apiNewPassword(state.username, pwd) |
| 59 | .then(() => alertDialog(t('password_changed', "Password changed")), alertDialog) |
| 60 | .finally(modal) |
| 61 | } |