({ credentials, provider, share }: LoginParams)
| 34 | const cookies = useCookies(['license-banner-dismissed', 'license-login-modal-dismissed']); |
| 35 | |
| 36 | export async function login({ credentials, provider, share }: LoginParams): Promise<void> { |
| 37 | const appStore = useAppStore(); |
| 38 | const serverStore = useServerStore(); |
| 39 | |
| 40 | let response: AuthenticationData; |
| 41 | |
| 42 | if (share) { |
| 43 | const { share, password } = credentials; |
| 44 | if (!share) throw new Error('Missing share ID'); |
| 45 | |
| 46 | await sdk.request(authenticateShare(share, password, 'session')); |
| 47 | // To initialize auto-refresh |
| 48 | response = await sdk.refresh(); |
| 49 | } else { |
| 50 | const { email, identifier, password, otp } = credentials; |
| 51 | |
| 52 | if (!password) throw new Error('Missing password'); |
| 53 | |
| 54 | const loginOptions: LoginOptions = { otp, ...(provider !== DEFAULT_AUTH_PROVIDER && { provider }) }; |
| 55 | |
| 56 | if (email) { |
| 57 | response = await sdk.login({ email, password }, loginOptions); |
| 58 | } else if (identifier) { |
| 59 | const login = |
| 60 | <Schema extends object>(): RestCommand<AuthenticationData, Schema> => |
| 61 | () => { |
| 62 | const path = getAuthEndpoint(loginOptions.provider); |
| 63 | const data = { identifier, password, otp, mode: 'session' }; |
| 64 | return { path, method: 'POST', body: JSON.stringify(data) }; |
| 65 | }; |
| 66 | |
| 67 | await sdk.request(login()); |
| 68 | // To initialize auto-refresh |
| 69 | response = await sdk.refresh(); |
| 70 | } else { |
| 71 | throw new Error('Missing email or identifier'); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | appStore.accessTokenExpiry = Date.now() + (response.expires ?? 0); |
| 76 | cookies.remove('license-banner-dismissed', { path: '/' }); |
| 77 | cookies.remove('license-login-modal-dismissed', { path: '/' }); |
| 78 | appStore.authenticated = true; |
| 79 | |
| 80 | // Reload server store to get authenticated data |
| 81 | serverStore.hydrate(); |
| 82 | |
| 83 | await hydrate(); |
| 84 | } |
| 85 | |
| 86 | let idle = false; |
| 87 | let firstRefresh = true; |
nothing calls this directly
no test coverage detected