mirror of
				https://github.com/immich-app/immich.git
				synced 2025-10-31 10:37:11 -04:00 
			
		
		
		
	refactor: auth login event (#17934)
This commit is contained in:
		
							parent
							
								
									64e738f79d
								
							
						
					
					
						commit
						f64e6f5dc3
					
				| @ -853,6 +853,7 @@ | |||||||
|     "failed_to_keep_this_delete_others": "Failed to keep this asset and delete the other assets", |     "failed_to_keep_this_delete_others": "Failed to keep this asset and delete the other assets", | ||||||
|     "failed_to_load_asset": "Failed to load asset", |     "failed_to_load_asset": "Failed to load asset", | ||||||
|     "failed_to_load_assets": "Failed to load assets", |     "failed_to_load_assets": "Failed to load assets", | ||||||
|  |     "failed_to_load_notifications": "Failed to load notifications", | ||||||
|     "failed_to_load_people": "Failed to load people", |     "failed_to_load_people": "Failed to load people", | ||||||
|     "failed_to_remove_product_key": "Failed to remove product key", |     "failed_to_remove_product_key": "Failed to remove product key", | ||||||
|     "failed_to_stack_assets": "Failed to stack assets", |     "failed_to_stack_assets": "Failed to stack assets", | ||||||
|  | |||||||
| @ -1,3 +1,5 @@ | |||||||
|  | import type { LoginResponseDto } from '@immich/sdk'; | ||||||
|  | 
 | ||||||
| type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void; | type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void; | ||||||
| 
 | 
 | ||||||
| class EventManager<EventMap extends Record<string, unknown[]>> { | class EventManager<EventMap extends Record<string, unknown[]>> { | ||||||
| @ -50,6 +52,7 @@ class EventManager<EventMap extends Record<string, unknown[]>> { | |||||||
| 
 | 
 | ||||||
| export const eventManager = new EventManager<{ | export const eventManager = new EventManager<{ | ||||||
|   'user.login': []; |   'user.login': []; | ||||||
|  |   'auth.login': [LoginResponseDto]; | ||||||
|   'auth.logout': []; |   'auth.logout': []; | ||||||
|   'language.change': [{ name: string; code: string; rtl?: boolean }]; |   'language.change': [{ name: string; code: string; rtl?: boolean }]; | ||||||
| }>(); | }>(); | ||||||
|  | |||||||
| @ -1,24 +1,27 @@ | |||||||
| import { eventManager } from '$lib/managers/event-manager.svelte'; | import { eventManager } from '$lib/managers/event-manager.svelte'; | ||||||
|  | import { handlePromiseError } from '$lib/utils'; | ||||||
|  | import { handleError } from '$lib/utils/handle-error'; | ||||||
| import { getNotifications, updateNotification, updateNotifications, type NotificationDto } from '@immich/sdk'; | import { getNotifications, updateNotification, updateNotifications, type NotificationDto } from '@immich/sdk'; | ||||||
|  | import { t } from 'svelte-i18n'; | ||||||
|  | import { get } from 'svelte/store'; | ||||||
| 
 | 
 | ||||||
| class NotificationStore { | class NotificationStore { | ||||||
|   notifications = $state<NotificationDto[]>([]); |   notifications = $state<NotificationDto[]>([]); | ||||||
| 
 | 
 | ||||||
|   constructor() { |   constructor() { | ||||||
|     // TODO replace this with an `auth.login` event
 |     eventManager.on('auth.login', () => handlePromiseError(this.refresh())); | ||||||
|     this.refresh().catch(() => {}); |  | ||||||
| 
 |  | ||||||
|     eventManager.on('auth.logout', () => this.clear()); |     eventManager.on('auth.logout', () => this.clear()); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   get hasUnread() { |   async refresh() { | ||||||
|     return this.notifications.length > 0; |     try { | ||||||
|  |       this.notifications = await getNotifications({ unread: true }); | ||||||
|  |     } catch (error) { | ||||||
|  |       const translate = get(t); | ||||||
|  |       handleError(error, translate('errors.failed_to_load_notifications')); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   refresh = async () => { |  | ||||||
|     this.notifications = await getNotifications({ unread: true }); |  | ||||||
|   }; |  | ||||||
| 
 |  | ||||||
|   markAsRead = async (id: string) => { |   markAsRead = async (id: string) => { | ||||||
|     this.notifications = this.notifications.filter((notification) => notification.id !== id); |     this.notifications = this.notifications.filter((notification) => notification.id !== id); | ||||||
|     await updateNotification({ id, notificationUpdateDto: { readAt: new Date().toISOString() } }); |     await updateNotification({ id, notificationUpdateDto: { readAt: new Date().toISOString() } }); | ||||||
|  | |||||||
| @ -2,15 +2,15 @@ | |||||||
|   import { goto } from '$app/navigation'; |   import { goto } from '$app/navigation'; | ||||||
|   import AuthPageLayout from '$lib/components/layouts/AuthPageLayout.svelte'; |   import AuthPageLayout from '$lib/components/layouts/AuthPageLayout.svelte'; | ||||||
|   import { AppRoute } from '$lib/constants'; |   import { AppRoute } from '$lib/constants'; | ||||||
|  |   import { eventManager } from '$lib/managers/event-manager.svelte'; | ||||||
|   import { featureFlags, serverConfig } from '$lib/stores/server-config.store'; |   import { featureFlags, serverConfig } from '$lib/stores/server-config.store'; | ||||||
|   import { oauth } from '$lib/utils'; |   import { oauth } from '$lib/utils'; | ||||||
|   import { getServerErrorMessage, handleError } from '$lib/utils/handle-error'; |   import { getServerErrorMessage, handleError } from '$lib/utils/handle-error'; | ||||||
|   import { login } from '@immich/sdk'; |   import { login, type LoginResponseDto } from '@immich/sdk'; | ||||||
|   import { Alert, Button, Field, Input, PasswordInput, Stack } from '@immich/ui'; |   import { Alert, Button, Field, Input, PasswordInput, Stack } from '@immich/ui'; | ||||||
|   import { onMount } from 'svelte'; |   import { onMount } from 'svelte'; | ||||||
|   import { t } from 'svelte-i18n'; |   import { t } from 'svelte-i18n'; | ||||||
|   import type { PageData } from './$types'; |   import type { PageData } from './$types'; | ||||||
|   import { notificationManager } from '$lib/stores/notification-manager.svelte'; |  | ||||||
| 
 | 
 | ||||||
|   interface Props { |   interface Props { | ||||||
|     data: PageData; |     data: PageData; | ||||||
| @ -25,10 +25,11 @@ | |||||||
|   let loading = $state(false); |   let loading = $state(false); | ||||||
|   let oauthLoading = $state(true); |   let oauthLoading = $state(true); | ||||||
| 
 | 
 | ||||||
|   const onSuccess = async () => { |   const onSuccess = async (user: LoginResponseDto) => { | ||||||
|     await notificationManager.refresh(); |  | ||||||
|     await goto(AppRoute.PHOTOS, { invalidateAll: true }); |     await goto(AppRoute.PHOTOS, { invalidateAll: true }); | ||||||
|  |     eventManager.emit('auth.login', user); | ||||||
|   }; |   }; | ||||||
|  | 
 | ||||||
|   const onFirstLogin = async () => await goto(AppRoute.AUTH_CHANGE_PASSWORD); |   const onFirstLogin = async () => await goto(AppRoute.AUTH_CHANGE_PASSWORD); | ||||||
|   const onOnboarding = async () => await goto(AppRoute.AUTH_ONBOARDING); |   const onOnboarding = async () => await goto(AppRoute.AUTH_ONBOARDING); | ||||||
| 
 | 
 | ||||||
| @ -40,8 +41,8 @@ | |||||||
| 
 | 
 | ||||||
|     if (oauth.isCallback(globalThis.location)) { |     if (oauth.isCallback(globalThis.location)) { | ||||||
|       try { |       try { | ||||||
|         await oauth.login(globalThis.location); |         const user = await oauth.login(globalThis.location); | ||||||
|         await onSuccess(); |         await onSuccess(user); | ||||||
|         return; |         return; | ||||||
|       } catch (error) { |       } catch (error) { | ||||||
|         console.error('Error [login-form] [oauth.callback]', error); |         console.error('Error [login-form] [oauth.callback]', error); | ||||||
| @ -78,7 +79,7 @@ | |||||||
|         await onFirstLogin(); |         await onFirstLogin(); | ||||||
|         return; |         return; | ||||||
|       } |       } | ||||||
|       await onSuccess(); |       await onSuccess(user); | ||||||
|       return; |       return; | ||||||
|     } catch (error) { |     } catch (error) { | ||||||
|       errorMessage = getServerErrorMessage(error) || $t('errors.incorrect_email_or_password'); |       errorMessage = getServerErrorMessage(error) || $t('errors.incorrect_email_or_password'); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user