Account.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, getContext } from 'svelte';
  4. import { user, config, settings } from '$lib/stores';
  5. import { updateUserProfile, createAPIKey, getAPIKey, getSessionUser } from '$lib/apis/auths';
  6. import { WEBUI_BASE_URL } from '$lib/constants';
  7. import UpdatePassword from './Account/UpdatePassword.svelte';
  8. import { getGravatarUrl } from '$lib/apis/utils';
  9. import { generateInitialsImage, canvasPixelTest } from '$lib/utils';
  10. import { copyToClipboard } from '$lib/utils';
  11. import Plus from '$lib/components/icons/Plus.svelte';
  12. import Tooltip from '$lib/components/common/Tooltip.svelte';
  13. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  14. import Textarea from '$lib/components/common/Textarea.svelte';
  15. const i18n = getContext('i18n');
  16. export let saveHandler: Function;
  17. export let saveSettings: Function;
  18. let profileImageUrl = '';
  19. let name = '';
  20. let bio = '';
  21. let webhookUrl = '';
  22. let showAPIKeys = false;
  23. let JWTTokenCopied = false;
  24. let APIKey = '';
  25. let APIKeyCopied = false;
  26. let profileImageInputElement: HTMLInputElement;
  27. const submitHandler = async () => {
  28. if (name !== $user?.name) {
  29. if (profileImageUrl === generateInitialsImage($user?.name) || profileImageUrl === '') {
  30. profileImageUrl = generateInitialsImage(name);
  31. }
  32. }
  33. if (webhookUrl !== $settings?.notifications?.webhook_url) {
  34. saveSettings({
  35. notifications: {
  36. ...$settings.notifications,
  37. webhook_url: webhookUrl
  38. }
  39. });
  40. }
  41. const updatedUser = await updateUserProfile(localStorage.token, name, profileImageUrl).catch(
  42. (error) => {
  43. toast.error(`${error}`);
  44. }
  45. );
  46. if (updatedUser) {
  47. // Get Session User Info
  48. const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
  49. toast.error(`${error}`);
  50. return null;
  51. });
  52. await user.set(sessionUser);
  53. return true;
  54. }
  55. return false;
  56. };
  57. const createAPIKeyHandler = async () => {
  58. APIKey = await createAPIKey(localStorage.token);
  59. if (APIKey) {
  60. toast.success($i18n.t('API Key created.'));
  61. } else {
  62. toast.error($i18n.t('Failed to create API Key.'));
  63. }
  64. };
  65. onMount(async () => {
  66. name = $user?.name;
  67. profileImageUrl = $user?.profile_image_url;
  68. webhookUrl = $settings?.notifications?.webhook_url ?? '';
  69. APIKey = await getAPIKey(localStorage.token).catch((error) => {
  70. console.log(error);
  71. return '';
  72. });
  73. });
  74. </script>
  75. <div id="tab-account" class="flex flex-col h-full justify-between text-sm">
  76. <div class=" overflow-y-scroll max-h-[28rem] lg:max-h-full">
  77. <input
  78. id="profile-image-input"
  79. bind:this={profileImageInputElement}
  80. type="file"
  81. hidden
  82. accept="image/*"
  83. on:change={(e) => {
  84. const files = profileImageInputElement.files ?? [];
  85. let reader = new FileReader();
  86. reader.onload = (event) => {
  87. let originalImageUrl = `${event.target.result}`;
  88. const img = new Image();
  89. img.src = originalImageUrl;
  90. img.onload = function () {
  91. const canvas = document.createElement('canvas');
  92. const ctx = canvas.getContext('2d');
  93. // Calculate the aspect ratio of the image
  94. const aspectRatio = img.width / img.height;
  95. // Calculate the new width and height to fit within 250x250
  96. let newWidth, newHeight;
  97. if (aspectRatio > 1) {
  98. newWidth = 250 * aspectRatio;
  99. newHeight = 250;
  100. } else {
  101. newWidth = 250;
  102. newHeight = 250 / aspectRatio;
  103. }
  104. // Set the canvas size
  105. canvas.width = 250;
  106. canvas.height = 250;
  107. // Calculate the position to center the image
  108. const offsetX = (250 - newWidth) / 2;
  109. const offsetY = (250 - newHeight) / 2;
  110. // Draw the image on the canvas
  111. ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
  112. // Get the base64 representation of the compressed image
  113. const compressedSrc = canvas.toDataURL('image/jpeg');
  114. // Display the compressed image
  115. profileImageUrl = compressedSrc;
  116. profileImageInputElement.files = null;
  117. };
  118. };
  119. if (
  120. files.length > 0 &&
  121. ['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(files[0]['type'])
  122. ) {
  123. reader.readAsDataURL(files[0]);
  124. }
  125. }}
  126. />
  127. <div class="space-y-1">
  128. <div>
  129. <div class="text-base font-medium">{$i18n.t('Your Account')}</div>
  130. <div class="text-xs text-gray-500 mt-0.5">
  131. {$i18n.t('Manage your account information.')}
  132. </div>
  133. </div>
  134. <!-- <div class=" text-sm font-medium">{$i18n.t('Account')}</div> -->
  135. <div class="flex space-x-5 mt-4">
  136. <div class="flex flex-col self-start group">
  137. <div class="self-center flex">
  138. <button
  139. class="relative rounded-full dark:bg-gray-700"
  140. type="button"
  141. on:click={() => {
  142. profileImageInputElement.click();
  143. }}
  144. >
  145. <img
  146. src={profileImageUrl !== '' ? profileImageUrl : generateInitialsImage(name)}
  147. alt="profile"
  148. class=" rounded-full size-14 md:size-20 object-cover"
  149. />
  150. <div class="absolute bottom-0 right-0 opacity-0 group-hover:opacity-100 transition">
  151. <div class="p-1 rounded-full bg-white text-black border-gray-100 shadow">
  152. <svg
  153. xmlns="http://www.w3.org/2000/svg"
  154. viewBox="0 0 20 20"
  155. fill="currentColor"
  156. class="size-3"
  157. >
  158. <path
  159. d="m2.695 14.762-1.262 3.155a.5.5 0 0 0 .65.65l3.155-1.262a4 4 0 0 0 1.343-.886L17.5 5.501a2.121 2.121 0 0 0-3-3L3.58 13.419a4 4 0 0 0-.885 1.343Z"
  160. />
  161. </svg>
  162. </div>
  163. </div>
  164. </button>
  165. </div>
  166. <div class="flex flex-col w-full justify-center mt-2">
  167. <button
  168. class=" text-xs text-center text-gray-500 rounded-lg py-0.5 opacity-0 group-hover:opacity-100 transition-all"
  169. on:click={async () => {
  170. profileImageUrl = `${WEBUI_BASE_URL}/user.png`;
  171. }}>{$i18n.t('Remove')}</button
  172. >
  173. <button
  174. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-lg py-0.5 opacity-0 group-hover:opacity-100 transition-all"
  175. on:click={async () => {
  176. if (canvasPixelTest()) {
  177. profileImageUrl = generateInitialsImage(name);
  178. } else {
  179. toast.info(
  180. $i18n.t(
  181. 'Fingerprint spoofing detected: Unable to use initials as avatar. Defaulting to default profile image.'
  182. ),
  183. {
  184. duration: 1000 * 10
  185. }
  186. );
  187. }
  188. }}>{$i18n.t('Initials')}</button
  189. >
  190. <button
  191. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-lg py-0.5 opacity-0 group-hover:opacity-100 transition-all"
  192. on:click={async () => {
  193. const url = await getGravatarUrl(localStorage.token, $user?.email);
  194. profileImageUrl = url;
  195. }}>{$i18n.t('Gravatar')}</button
  196. >
  197. </div>
  198. </div>
  199. <div class="flex flex-1 flex-col">
  200. <div class=" flex-1">
  201. <div class="flex flex-col w-full">
  202. <div class=" mb-1 text-xs font-medium">{$i18n.t('Name')}</div>
  203. <div class="flex-1">
  204. <input
  205. class="w-full text-sm dark:text-gray-300 bg-transparent outline-hidden"
  206. type="text"
  207. bind:value={name}
  208. required
  209. placeholder={$i18n.t('Enter your name')}
  210. />
  211. </div>
  212. </div>
  213. <div class="flex flex-col w-full mt-2">
  214. <div class=" mb-1 text-xs font-medium">{$i18n.t('Bio')}</div>
  215. <div class="flex-1">
  216. <Textarea
  217. className="w-full text-sm dark:text-gray-300 bg-transparent outline-hidden"
  218. bind:value={bio}
  219. minSize={100}
  220. placeholder={$i18n.t('Share your background and interests')}
  221. />
  222. </div>
  223. </div>
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. {#if $config?.features?.enable_user_webhooks}
  229. <div class="mt-2">
  230. <div class="flex flex-col w-full">
  231. <div class=" mb-1 text-xs font-medium">{$i18n.t('Notification Webhook')}</div>
  232. <div class="flex-1">
  233. <input
  234. class="w-full text-sm outline-hidden"
  235. type="url"
  236. placeholder={$i18n.t('Enter your webhook URL')}
  237. bind:value={webhookUrl}
  238. required
  239. />
  240. </div>
  241. </div>
  242. </div>
  243. {/if}
  244. <hr class="border-gray-50 dark:border-gray-850 my-4" />
  245. {#if $config?.features.enable_login_form}
  246. <div class="mt-2">
  247. <UpdatePassword />
  248. </div>
  249. {/if}
  250. {#if ($config?.features?.enable_api_key ?? true) || $user?.role === 'admin'}
  251. <div class="flex justify-between items-center text-sm mt-2">
  252. <div class=" font-medium">{$i18n.t('API keys')}</div>
  253. <button
  254. class=" text-xs font-medium text-gray-500"
  255. type="button"
  256. on:click={() => {
  257. showAPIKeys = !showAPIKeys;
  258. }}>{showAPIKeys ? $i18n.t('Hide') : $i18n.t('Show')}</button
  259. >
  260. </div>
  261. {#if showAPIKeys}
  262. <div class="flex flex-col py-2.5">
  263. {#if $user?.role === 'admin'}
  264. <div class="justify-between w-full">
  265. <div class="flex justify-between w-full">
  266. <div class="self-center text-xs font-medium mb-1">{$i18n.t('JWT Token')}</div>
  267. </div>
  268. <div class="flex">
  269. <SensitiveInput value={localStorage.token} readOnly={true} />
  270. <button
  271. class="ml-1.5 px-1.5 py-1 dark:hover:bg-gray-850 transition rounded-lg"
  272. on:click={() => {
  273. copyToClipboard(localStorage.token);
  274. JWTTokenCopied = true;
  275. setTimeout(() => {
  276. JWTTokenCopied = false;
  277. }, 2000);
  278. }}
  279. >
  280. {#if JWTTokenCopied}
  281. <svg
  282. xmlns="http://www.w3.org/2000/svg"
  283. viewBox="0 0 20 20"
  284. fill="currentColor"
  285. class="w-4 h-4"
  286. >
  287. <path
  288. fill-rule="evenodd"
  289. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  290. clip-rule="evenodd"
  291. />
  292. </svg>
  293. {:else}
  294. <svg
  295. xmlns="http://www.w3.org/2000/svg"
  296. viewBox="0 0 16 16"
  297. fill="currentColor"
  298. class="w-4 h-4"
  299. >
  300. <path
  301. fill-rule="evenodd"
  302. d="M11.986 3H12a2 2 0 0 1 2 2v6a2 2 0 0 1-1.5 1.937V7A2.5 2.5 0 0 0 10 4.5H4.063A2 2 0 0 1 6 3h.014A2.25 2.25 0 0 1 8.25 1h1.5a2.25 2.25 0 0 1 2.236 2ZM10.5 4v-.75a.75.75 0 0 0-.75-.75h-1.5a.75.75 0 0 0-.75.75V4h3Z"
  303. clip-rule="evenodd"
  304. />
  305. <path
  306. fill-rule="evenodd"
  307. d="M3 6a1 1 0 0 0-1 1v7a1 1 0 0 0 1 1h7a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1H3Zm1.75 2.5a.75.75 0 0 0 0 1.5h3.5a.75.75 0 0 0 0-1.5h-3.5ZM4 11.75a.75.75 0 0 1 .75-.75h3.5a.75.75 0 0 1 0 1.5h-3.5a.75.75 0 0 1-.75-.75Z"
  308. clip-rule="evenodd"
  309. />
  310. </svg>
  311. {/if}
  312. </button>
  313. </div>
  314. </div>
  315. {/if}
  316. {#if $config?.features?.enable_api_key ?? true}
  317. <div class="justify-between w-full mt-2">
  318. {#if $user?.role === 'admin'}
  319. <div class="flex justify-between w-full">
  320. <div class="self-center text-xs font-medium mb-1">{$i18n.t('API Key')}</div>
  321. </div>
  322. {/if}
  323. <div class="flex">
  324. {#if APIKey}
  325. <SensitiveInput value={APIKey} readOnly={true} />
  326. <button
  327. class="ml-1.5 px-1.5 py-1 dark:hover:bg-gray-850 transition rounded-lg"
  328. on:click={() => {
  329. copyToClipboard(APIKey);
  330. APIKeyCopied = true;
  331. setTimeout(() => {
  332. APIKeyCopied = false;
  333. }, 2000);
  334. }}
  335. >
  336. {#if APIKeyCopied}
  337. <svg
  338. xmlns="http://www.w3.org/2000/svg"
  339. viewBox="0 0 20 20"
  340. fill="currentColor"
  341. class="w-4 h-4"
  342. >
  343. <path
  344. fill-rule="evenodd"
  345. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  346. clip-rule="evenodd"
  347. />
  348. </svg>
  349. {:else}
  350. <svg
  351. xmlns="http://www.w3.org/2000/svg"
  352. viewBox="0 0 16 16"
  353. fill="currentColor"
  354. class="w-4 h-4"
  355. >
  356. <path
  357. fill-rule="evenodd"
  358. d="M11.986 3H12a2 2 0 0 1 2 2v6a2 2 0 0 1-1.5 1.937V7A2.5 2.5 0 0 0 10 4.5H4.063A2 2 0 0 1 6 3h.014A2.25 2.25 0 0 1 8.25 1h1.5a2.25 2.25 0 0 1 2.236 2ZM10.5 4v-.75a.75.75 0 0 0-.75-.75h-1.5a.75.75 0 0 0-.75.75V4h3Z"
  359. clip-rule="evenodd"
  360. />
  361. <path
  362. fill-rule="evenodd"
  363. d="M3 6a1 1 0 0 0-1 1v7a1 1 0 0 0 1 1h7a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1H3Zm1.75 2.5a.75.75 0 0 0 0 1.5h3.5a.75.75 0 0 0 0-1.5h-3.5ZM4 11.75a.75.75 0 0 1 .75-.75h3.5a.75.75 0 0 1 0 1.5h-3.5a.75.75 0 0 1-.75-.75Z"
  364. clip-rule="evenodd"
  365. />
  366. </svg>
  367. {/if}
  368. </button>
  369. <Tooltip content={$i18n.t('Create new key')}>
  370. <button
  371. class=" px-1.5 py-1 dark:hover:bg-gray-850transition rounded-lg"
  372. on:click={() => {
  373. createAPIKeyHandler();
  374. }}
  375. >
  376. <svg
  377. xmlns="http://www.w3.org/2000/svg"
  378. fill="none"
  379. viewBox="0 0 24 24"
  380. stroke-width="2"
  381. stroke="currentColor"
  382. class="size-4"
  383. >
  384. <path
  385. stroke-linecap="round"
  386. stroke-linejoin="round"
  387. d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99"
  388. />
  389. </svg>
  390. </button>
  391. </Tooltip>
  392. {:else}
  393. <button
  394. class="flex gap-1.5 items-center font-medium px-3.5 py-1.5 rounded-lg bg-gray-100/70 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-850 transition"
  395. on:click={() => {
  396. createAPIKeyHandler();
  397. }}
  398. >
  399. <Plus strokeWidth="2" className=" size-3.5" />
  400. {$i18n.t('Create new secret key')}</button
  401. >
  402. {/if}
  403. </div>
  404. </div>
  405. {/if}
  406. </div>
  407. {/if}
  408. {/if}
  409. </div>
  410. <div class="flex justify-end pt-3 text-sm font-medium">
  411. <button
  412. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
  413. on:click={async () => {
  414. const res = await submitHandler();
  415. if (res) {
  416. saveHandler();
  417. }
  418. }}
  419. >
  420. {$i18n.t('Save')}
  421. </button>
  422. </div>
  423. </div>