import * as Linking from 'expo-linking'; import * as WebBrowser from 'expo-web-browser'; import { SPOTIFY_LINK_PAGES } from '@musicshare/shared'; import { api } from './api'; export type SignInResult = 'connected' | 'cancelled'; export class SignInError extends Error {} /** * Sign in on the service's own site in an auth browser and come back through * the app's deep link. */ export async function signInWithService(provider: 'plex' | 'lastfm'): Promise { // In Expo Go this is an exp:// URL, in builds musicshare:// const returnTo = Linking.createURL('settings'); const { url } = await api.authorizeUrl(provider, { client: 'mobile', returnTo }); const result = await WebBrowser.openAuthSessionAsync(url, returnTo); if (result.type !== 'success') return 'cancelled'; const error = Linking.parse(result.url).queryParams?.error; if (typeof error === 'string') throw new SignInError(error); return 'connected'; } /** Where users approve "Spotify Scrobbling" (Last.fm) or the Spotify link (ListenBrainz). */ export const openSpotifyLinkPage = (on: keyof typeof SPOTIFY_LINK_PAGES = 'lastfm') => WebBrowser.openBrowserAsync(SPOTIFY_LINK_PAGES[on]);