2022-05-21 02:23:55 -05:00
|
|
|
<script lang="ts">
|
2023-07-01 00:50:47 -04:00
|
|
|
import { goto } from '$app/navigation';
|
|
|
|
import LoadingSpinner from '$lib/components/shared-components/loading-spinner.svelte';
|
|
|
|
import { AppRoute } from '$lib/constants';
|
2023-09-08 22:51:46 -04:00
|
|
|
import { featureFlags, serverConfig } from '$lib/stores/server-config.store';
|
2024-02-14 06:38:57 -08:00
|
|
|
import { oauth } from '$lib/utils';
|
2023-07-15 00:03:56 -04:00
|
|
|
import { getServerErrorMessage, handleError } from '$lib/utils/handle-error';
|
2024-02-13 17:07:37 -05:00
|
|
|
import { getServerConfig, login } from '@immich/sdk';
|
2024-02-18 10:47:13 -08:00
|
|
|
import { onMount } from 'svelte';
|
2023-07-01 00:50:47 -04:00
|
|
|
import { fade } from 'svelte/transition';
|
|
|
|
import Button from '../elements/buttons/button.svelte';
|
2024-02-24 21:28:56 +01:00
|
|
|
import PasswordField from '../shared-components/password-field.svelte';
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2024-02-18 10:47:13 -08:00
|
|
|
export let onSuccess: () => unknown | Promise<unknown>;
|
|
|
|
export let onFirstLogin: () => unknown | Promise<unknown>;
|
|
|
|
export let onOnboarding: () => unknown | Promise<unknown>;
|
|
|
|
|
2023-07-15 00:03:56 -04:00
|
|
|
let errorMessage: string;
|
2023-07-01 00:50:47 -04:00
|
|
|
let email = '';
|
|
|
|
let password = '';
|
2023-09-01 07:08:42 -04:00
|
|
|
let oauthError = '';
|
2023-07-01 00:50:47 -04:00
|
|
|
let loading = false;
|
|
|
|
let oauthLoading = true;
|
|
|
|
|
|
|
|
onMount(async () => {
|
2023-09-01 07:08:42 -04:00
|
|
|
if (!$featureFlags.oauth) {
|
|
|
|
oauthLoading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
if (oauth.isCallback(window.location)) {
|
|
|
|
try {
|
|
|
|
await oauth.login(window.location);
|
2024-02-18 10:47:13 -08:00
|
|
|
await onSuccess();
|
2023-07-01 00:50:47 -04:00
|
|
|
return;
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error [login-form] [oauth.callback]', error);
|
2024-02-29 11:22:39 -05:00
|
|
|
oauthError = getServerErrorMessage(error) || 'Unable to complete OAuth login';
|
2023-07-01 00:50:47 -04:00
|
|
|
oauthLoading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-09-01 07:08:42 -04:00
|
|
|
if ($featureFlags.oauthAutoLaunch && !oauth.isAutoLaunchDisabled(window.location)) {
|
2023-07-01 00:50:47 -04:00
|
|
|
await goto(`${AppRoute.AUTH_LOGIN}?autoLaunch=0`, { replaceState: true });
|
2023-09-01 07:08:42 -04:00
|
|
|
await oauth.authorize(window.location);
|
2023-07-01 00:50:47 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-02-27 08:37:37 -08:00
|
|
|
handleError(error, 'Unable to connect!');
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
oauthLoading = false;
|
|
|
|
});
|
|
|
|
|
2024-02-13 17:07:37 -05:00
|
|
|
const handleLogin = async () => {
|
2023-07-01 00:50:47 -04:00
|
|
|
try {
|
2023-07-15 00:03:56 -04:00
|
|
|
errorMessage = '';
|
2023-07-01 00:50:47 -04:00
|
|
|
loading = true;
|
|
|
|
|
2024-02-13 17:07:37 -05:00
|
|
|
const user = await login({ loginCredentialDto: { email, password } });
|
|
|
|
const serverConfig = await getServerConfig();
|
2024-01-03 23:28:32 -06:00
|
|
|
|
|
|
|
if (user.isAdmin && !serverConfig.isOnboarded) {
|
2024-02-18 10:47:13 -08:00
|
|
|
await onOnboarding();
|
2024-01-03 23:28:32 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.isAdmin && user.shouldChangePassword) {
|
2024-02-18 10:47:13 -08:00
|
|
|
await onFirstLogin();
|
2023-07-01 00:50:47 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-02-18 10:47:13 -08:00
|
|
|
await onSuccess();
|
2023-07-01 00:50:47 -04:00
|
|
|
return;
|
2023-07-15 00:03:56 -04:00
|
|
|
} catch (error) {
|
2024-02-29 11:22:39 -05:00
|
|
|
errorMessage = getServerErrorMessage(error) || 'Incorrect email or password';
|
2023-07-01 00:50:47 -04:00
|
|
|
loading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2023-09-01 07:08:42 -04:00
|
|
|
|
|
|
|
const handleOAuthLogin = async () => {
|
|
|
|
oauthLoading = true;
|
|
|
|
oauthError = '';
|
2024-02-02 06:27:54 +01:00
|
|
|
const success = await oauth.authorize(window.location);
|
|
|
|
if (!success) {
|
|
|
|
oauthLoading = false;
|
|
|
|
oauthError = 'Unable to login with OAuth';
|
|
|
|
}
|
2023-09-01 07:08:42 -04:00
|
|
|
};
|
2022-05-21 02:23:55 -05:00
|
|
|
</script>
|
|
|
|
|
2023-09-01 07:08:42 -04:00
|
|
|
{#if !oauthLoading && $featureFlags.passwordLogin}
|
2024-02-13 17:07:37 -05:00
|
|
|
<form on:submit|preventDefault={handleLogin} class="mt-5 flex flex-col gap-5">
|
2023-07-15 00:03:56 -04:00
|
|
|
{#if errorMessage}
|
2023-07-01 00:50:47 -04:00
|
|
|
<p class="text-red-400" transition:fade>
|
2023-07-15 00:03:56 -04:00
|
|
|
{errorMessage}
|
2023-07-01 00:50:47 -04:00
|
|
|
</p>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<div class="flex flex-col gap-2">
|
|
|
|
<label class="immich-form-label" for="email">Email</label>
|
|
|
|
<input
|
|
|
|
class="immich-form-input"
|
|
|
|
id="email"
|
|
|
|
name="email"
|
|
|
|
type="email"
|
|
|
|
autocomplete="email"
|
|
|
|
bind:value={email}
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="flex flex-col gap-2">
|
|
|
|
<label class="immich-form-label" for="password">Password</label>
|
2024-02-24 21:28:56 +01:00
|
|
|
<PasswordField id="password" bind:password autocomplete="current-password" />
|
2023-07-01 00:50:47 -04:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="my-5 flex w-full">
|
2023-09-01 07:08:42 -04:00
|
|
|
<Button type="submit" size="lg" fullwidth disabled={loading}>
|
2023-07-01 00:50:47 -04:00
|
|
|
{#if loading}
|
|
|
|
<span class="h-6">
|
|
|
|
<LoadingSpinner />
|
|
|
|
</span>
|
|
|
|
{:else}
|
|
|
|
Login
|
|
|
|
{/if}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2023-03-15 22:38:29 +01:00
|
|
|
{/if}
|
2022-07-11 05:31:17 +02:00
|
|
|
|
2023-09-01 07:08:42 -04:00
|
|
|
{#if $featureFlags.oauth}
|
|
|
|
{#if $featureFlags.passwordLogin}
|
2023-07-18 13:19:39 -05:00
|
|
|
<div class="inline-flex w-full items-center justify-center">
|
|
|
|
<hr class="my-4 h-px w-3/4 border-0 bg-gray-200 dark:bg-gray-600" />
|
2023-07-01 00:50:47 -04:00
|
|
|
<span
|
2023-07-18 13:19:39 -05:00
|
|
|
class="absolute left-1/2 -translate-x-1/2 bg-white px-3 font-medium text-gray-900 dark:bg-immich-dark-gray dark:text-white"
|
2023-07-01 00:50:47 -04:00
|
|
|
>
|
|
|
|
or
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
<div class="my-5 flex flex-col gap-5">
|
|
|
|
{#if oauthError}
|
2023-09-01 07:08:42 -04:00
|
|
|
<p class="text-center text-red-400" transition:fade>{oauthError}</p>
|
2023-07-01 00:50:47 -04:00
|
|
|
{/if}
|
2023-09-01 07:08:42 -04:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
disabled={loading || oauthLoading}
|
|
|
|
size="lg"
|
|
|
|
fullwidth
|
|
|
|
color={$featureFlags.passwordLogin ? 'secondary' : 'primary'}
|
|
|
|
on:click={handleOAuthLogin}
|
|
|
|
>
|
|
|
|
{#if oauthLoading}
|
|
|
|
<span class="h-6">
|
|
|
|
<LoadingSpinner />
|
|
|
|
</span>
|
|
|
|
{:else}
|
2023-09-08 22:51:46 -04:00
|
|
|
{$serverConfig.oauthButtonText}
|
2023-09-01 07:08:42 -04:00
|
|
|
{/if}
|
|
|
|
</Button>
|
2023-07-01 00:50:47 -04:00
|
|
|
</div>
|
2023-03-15 22:38:29 +01:00
|
|
|
{/if}
|
2023-02-15 18:56:54 +01:00
|
|
|
|
2023-09-01 07:08:42 -04:00
|
|
|
{#if !$featureFlags.passwordLogin && !$featureFlags.oauth}
|
2023-07-18 13:19:39 -05:00
|
|
|
<p class="p-4 text-center dark:text-immich-dark-fg">Login has been disabled.</p>
|
2023-03-15 22:38:29 +01:00
|
|
|
{/if}
|