stadtwerke/innungsapp/apps/mobile/lib/auth-client.ts

26 lines
844 B
TypeScript

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'
const apiUrl =
Constants.expoConfig?.extra?.apiUrl ??
process.env.EXPO_PUBLIC_API_URL ??
'http://localhost:3000'
export const authClient = createAuthClient({
baseURL: apiUrl,
plugins: [magicLinkClient()],
fetchOptions: {
customFetchImpl: async (url, options) => {
const token = await AsyncStorage.getItem('better-auth-session')
const headers = new Headers((options?.headers as HeadersInit) ?? {})
headers.set('origin', apiUrl)
if (token) {
headers.set('cookie', `better-auth.session_token=${token}`)
}
return fetch(url, { ...options, headers })
},
},
})