oauth-settings.svelte 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <script lang="ts">
  2. import {
  3. notificationController,
  4. NotificationType
  5. } from '$lib/components/shared-components/notification/notification';
  6. import { handleError } from '$lib/utils/handle-error';
  7. import { api, SystemConfigOAuthDto } from '@api';
  8. import _ from 'lodash';
  9. import { fade } from 'svelte/transition';
  10. import SettingButtonsRow from '../setting-buttons-row.svelte';
  11. import SettingInputField, { SettingInputFieldType } from '../setting-input-field.svelte';
  12. import SettingSwitch from '../setting-switch.svelte';
  13. export let oauthConfig: SystemConfigOAuthDto;
  14. let savedConfig: SystemConfigOAuthDto;
  15. let defaultConfig: SystemConfigOAuthDto;
  16. const handleToggleOverride = () => {
  17. // click runs before bind
  18. const previouslyEnabled = oauthConfig.mobileOverrideEnabled;
  19. if (!previouslyEnabled && !oauthConfig.mobileRedirectUri) {
  20. oauthConfig.mobileRedirectUri = window.location.origin + '/api/oauth/mobile-redirect';
  21. }
  22. };
  23. async function getConfigs() {
  24. [savedConfig, defaultConfig] = await Promise.all([
  25. api.systemConfigApi.getConfig().then((res) => res.data.oauth),
  26. api.systemConfigApi.getDefaults().then((res) => res.data.oauth)
  27. ]);
  28. }
  29. async function reset() {
  30. const { data: resetConfig } = await api.systemConfigApi.getConfig();
  31. oauthConfig = { ...resetConfig.oauth };
  32. savedConfig = { ...resetConfig.oauth };
  33. notificationController.show({
  34. message: 'Reset OAuth settings to the last saved settings',
  35. type: NotificationType.Info
  36. });
  37. }
  38. async function saveSetting() {
  39. try {
  40. const { data: currentConfig } = await api.systemConfigApi.getConfig();
  41. if (!oauthConfig.mobileOverrideEnabled) {
  42. oauthConfig.mobileRedirectUri = '';
  43. }
  44. const result = await api.systemConfigApi.updateConfig({
  45. ...currentConfig,
  46. oauth: oauthConfig
  47. });
  48. oauthConfig = { ...result.data.oauth };
  49. savedConfig = { ...result.data.oauth };
  50. notificationController.show({
  51. message: 'OAuth settings saved',
  52. type: NotificationType.Info
  53. });
  54. } catch (error) {
  55. handleError(error, 'Unable to save OAuth settings');
  56. }
  57. }
  58. async function resetToDefault() {
  59. const { data: defaultConfig } = await api.systemConfigApi.getDefaults();
  60. oauthConfig = { ...defaultConfig.oauth };
  61. notificationController.show({
  62. message: 'Reset OAuth settings to default',
  63. type: NotificationType.Info
  64. });
  65. }
  66. </script>
  67. <div class="mt-2">
  68. {#await getConfigs() then}
  69. <div in:fade={{ duration: 500 }}>
  70. <form autocomplete="off" on:submit|preventDefault class="flex flex-col mx-4 gap-4 py-4">
  71. <p class="text-sm dark:text-immich-dark-fg">
  72. For more details about this feature, refer to the <a
  73. href="http://immich.app/docs/features/oauth#mobile-redirect-uri"
  74. class="underline"
  75. target="_blank"
  76. rel="noreferrer">docs</a
  77. >.
  78. </p>
  79. <SettingSwitch title="Enable" bind:checked={oauthConfig.enabled} />
  80. <hr />
  81. <SettingInputField
  82. inputType={SettingInputFieldType.TEXT}
  83. label="ISSUER URL"
  84. bind:value={oauthConfig.issuerUrl}
  85. required={true}
  86. disabled={!oauthConfig.enabled}
  87. isEdited={!(oauthConfig.issuerUrl == savedConfig.issuerUrl)}
  88. />
  89. <SettingInputField
  90. inputType={SettingInputFieldType.TEXT}
  91. label="CLIENT ID"
  92. bind:value={oauthConfig.clientId}
  93. required={true}
  94. disabled={!oauthConfig.enabled}
  95. isEdited={!(oauthConfig.clientId == savedConfig.clientId)}
  96. />
  97. <SettingInputField
  98. inputType={SettingInputFieldType.TEXT}
  99. label="CLIENT SECRET"
  100. bind:value={oauthConfig.clientSecret}
  101. required={true}
  102. disabled={!oauthConfig.enabled}
  103. isEdited={!(oauthConfig.clientSecret == savedConfig.clientSecret)}
  104. />
  105. <SettingInputField
  106. inputType={SettingInputFieldType.TEXT}
  107. label="SCOPE"
  108. bind:value={oauthConfig.scope}
  109. required={true}
  110. disabled={!oauthConfig.enabled}
  111. isEdited={!(oauthConfig.scope == savedConfig.scope)}
  112. />
  113. <SettingInputField
  114. inputType={SettingInputFieldType.TEXT}
  115. label="BUTTON TEXT"
  116. bind:value={oauthConfig.buttonText}
  117. required={false}
  118. disabled={!oauthConfig.enabled}
  119. isEdited={!(oauthConfig.buttonText == savedConfig.buttonText)}
  120. />
  121. <SettingSwitch
  122. title="AUTO REGISTER"
  123. subtitle="Automatically register new users after signing in with OAuth"
  124. bind:checked={oauthConfig.autoRegister}
  125. disabled={!oauthConfig.enabled}
  126. />
  127. <SettingSwitch
  128. title="MOBILE REDIRECT URI OVERRIDE"
  129. subtitle="Enable when `app.immich:/` is an invalid redirect URI."
  130. disabled={!oauthConfig.enabled}
  131. on:click={() => handleToggleOverride()}
  132. bind:checked={oauthConfig.mobileOverrideEnabled}
  133. />
  134. {#if oauthConfig.mobileOverrideEnabled}
  135. <SettingInputField
  136. inputType={SettingInputFieldType.TEXT}
  137. label="MOBILE REDIRECT URI"
  138. bind:value={oauthConfig.mobileRedirectUri}
  139. required={true}
  140. disabled={!oauthConfig.enabled}
  141. isEdited={!(oauthConfig.mobileRedirectUri == savedConfig.mobileRedirectUri)}
  142. />
  143. {/if}
  144. <SettingButtonsRow
  145. on:reset={reset}
  146. on:save={saveSetting}
  147. on:reset-to-default={resetToDefault}
  148. showResetToDefault={!_.isEqual(savedConfig, defaultConfig)}
  149. />
  150. </form>
  151. </div>
  152. {/await}
  153. </div>