Account.svelte 14 KB

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