push
This commit is contained in:
44
innungsapp/apps/mobile/lib/api-url.ts
Normal file
44
innungsapp/apps/mobile/lib/api-url.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import Constants from 'expo-constants'
|
||||
import { Platform } from 'react-native'
|
||||
|
||||
function stripTrailingSlash(url: string): string {
|
||||
return url.replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
function extractHost(hostUri?: string | null): string | null {
|
||||
if (!hostUri) return null
|
||||
const [host] = hostUri.split(':')
|
||||
return host || null
|
||||
}
|
||||
|
||||
function resolveExpoHost(): string | null {
|
||||
const fromExpoConfig = (Constants.expoConfig as any)?.hostUri as string | undefined
|
||||
const fromManifest2 = (Constants as any)?.manifest2?.extra?.expoClient?.hostUri as
|
||||
| string
|
||||
| undefined
|
||||
const fromManifest = (Constants as any)?.manifest?.debuggerHost as string | undefined
|
||||
|
||||
return extractHost(fromExpoConfig ?? fromManifest2 ?? fromManifest ?? null)
|
||||
}
|
||||
|
||||
export function getApiBaseUrl(): string {
|
||||
const configuredApiUrl =
|
||||
process.env.EXPO_PUBLIC_API_URL ??
|
||||
((Constants.expoConfig?.extra as { apiUrl?: string } | undefined)?.apiUrl ?? undefined)
|
||||
|
||||
if (configuredApiUrl && !configuredApiUrl.includes('localhost')) {
|
||||
return stripTrailingSlash(configuredApiUrl)
|
||||
}
|
||||
|
||||
const expoHost = resolveExpoHost()
|
||||
if (expoHost) {
|
||||
return `http://${expoHost}:3000`
|
||||
}
|
||||
|
||||
if (configuredApiUrl) {
|
||||
return stripTrailingSlash(configuredApiUrl)
|
||||
}
|
||||
|
||||
const fallbackHost = Platform.OS === 'android' ? '10.0.2.2' : '127.0.0.1'
|
||||
return `http://${fallbackHost}:3000`
|
||||
}
|
||||
@@ -1,16 +1,12 @@
|
||||
import { createAuthClient } from 'better-auth/react'
|
||||
import { magicLinkClient } from 'better-auth/client/plugins'
|
||||
import Constants from 'expo-constants'
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import { getApiBaseUrl } from './api-url'
|
||||
|
||||
const apiUrl =
|
||||
Constants.expoConfig?.extra?.apiUrl ??
|
||||
process.env.EXPO_PUBLIC_API_URL ??
|
||||
'http://localhost:3000'
|
||||
const apiUrl = getApiBaseUrl()
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
baseURL: apiUrl,
|
||||
plugins: [magicLinkClient()],
|
||||
plugins: [],
|
||||
fetchOptions: {
|
||||
customFetchImpl: async (url, options) => {
|
||||
const token = await AsyncStorage.getItem('better-auth-session')
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { Platform } from 'react-native'
|
||||
import { trpc } from './trpc'
|
||||
import { queryClient } from './trpc'
|
||||
import { getApiBaseUrl } from './api-url'
|
||||
|
||||
Notifications.setNotificationHandler({
|
||||
handleNotification: async () => ({
|
||||
@@ -30,11 +29,8 @@ export async function setupPushNotifications() {
|
||||
projectId: process.env.EXPO_PUBLIC_PROJECT_ID,
|
||||
})
|
||||
|
||||
// Store push token on the server
|
||||
// We call the tRPC mutation to save the token
|
||||
const caller = trpc.createClient as never
|
||||
// Simple fetch to avoid circular deps:
|
||||
const apiUrl = process.env.EXPO_PUBLIC_API_URL ?? 'http://localhost:3000'
|
||||
// Store push token on the server.
|
||||
const apiUrl = getApiBaseUrl()
|
||||
await fetch(`${apiUrl}/api/push-token`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
|
||||
9
innungsapp/apps/mobile/lib/org-config.ts
Normal file
9
innungsapp/apps/mobile/lib/org-config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import Constants from 'expo-constants'
|
||||
|
||||
export function getOrgSlug(): string {
|
||||
return (
|
||||
process.env.EXPO_PUBLIC_ORG_SLUG ??
|
||||
(Constants.expoConfig?.extra as { orgSlug?: string } | undefined)?.orgSlug ??
|
||||
'tischler'
|
||||
)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import superjson from 'superjson'
|
||||
import { createElement, type ReactNode } from 'react'
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import type { AppRouter } from '@innungsapp/admin'
|
||||
import { getApiBaseUrl } from './api-url'
|
||||
|
||||
export const trpc = createTRPCReact<AppRouter>()
|
||||
|
||||
@@ -17,14 +18,10 @@ export const queryClient = new QueryClient({
|
||||
},
|
||||
})
|
||||
|
||||
function getApiUrl() {
|
||||
return process.env.EXPO_PUBLIC_API_URL ?? 'http://localhost:3000'
|
||||
}
|
||||
|
||||
const trpcClient = trpc.createClient({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: `${getApiUrl()}/api/trpc`,
|
||||
url: `${getApiBaseUrl()}/api/trpc`,
|
||||
transformer: superjson,
|
||||
async headers() {
|
||||
// Include session cookie for auth
|
||||
|
||||
Reference in New Issue
Block a user