feat: Implement initial with admin and mobile clients, authentication, data models, and lead generation scripts.
This commit is contained in:
@@ -8,6 +8,7 @@ import { NEWS_KATEGORIE_LABELS } from '@innungsapp/shared/types'
|
||||
import { useNewsList } from '@/hooks/useNews'
|
||||
import { useTermineListe } from '@/hooks/useTermine'
|
||||
import { useNewsReadStore } from '@/store/news.store'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
|
||||
// Helper to truncate text
|
||||
function getNewsExcerpt(value: string) {
|
||||
@@ -23,16 +24,18 @@ export default function HomeScreen() {
|
||||
const { data: newsItems = [] } = useNewsList()
|
||||
const { data: termine = [] } = useTermineListe(true)
|
||||
const readIds = useNewsReadStore((s) => s.readIds)
|
||||
const { data: me } = trpc.members.me.useQuery()
|
||||
const userName = me?.name ?? ''
|
||||
|
||||
const latestNews = newsItems.slice(0, 2)
|
||||
const upcomingEvents = termine.slice(0, 3)
|
||||
const unreadCount = newsItems.filter((item) => !(item.isRead || readIds.has(item.id))).length
|
||||
|
||||
const QUICK_ACTIONS = [
|
||||
{ label: 'Mitglieder', icon: 'people', color: '#003B7E', bg: '#E0F2FE', route: '/(app)/members' },
|
||||
{ label: 'Termine', icon: 'calendar', color: '#B45309', bg: '#FEF3C7', route: '/(app)/termine' },
|
||||
{ label: 'Stellen', icon: 'briefcase', color: '#059669', bg: '#D1FAE5', route: '/(app)/stellen' },
|
||||
{ label: 'Profil', icon: 'person', color: '#4F46E5', bg: '#E0E7FF', route: '/(app)/profil' },
|
||||
{ label: 'Mitglieder', icon: 'people-circle', color: '#2563EB', bg: '#DBEAFE', route: '/(app)/members' },
|
||||
{ label: 'Termine', icon: 'alarm', color: '#EA580C', bg: '#FFEDD5', route: '/(app)/termine' },
|
||||
{ label: 'Stellen', icon: 'construct', color: '#0F766E', bg: '#CCFBF1', route: '/(app)/stellen' },
|
||||
{ label: 'Aktuelles', icon: 'megaphone', color: '#BE185D', bg: '#FCE7F3', route: '/(app)/news' },
|
||||
]
|
||||
|
||||
return (
|
||||
@@ -53,7 +56,7 @@ export default function HomeScreen() {
|
||||
</View>
|
||||
<View>
|
||||
<Text style={styles.greeting}>Willkommen zurück,</Text>
|
||||
<Text style={styles.username}>Demo Admin</Text>
|
||||
<Text style={styles.username}>{userName}</Text>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
@@ -461,4 +464,3 @@ const styles = StyleSheet.create({
|
||||
fontWeight: '500',
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { View, Text, ScrollView, TouchableOpacity, Alert, StyleSheet } from 'rea
|
||||
import { SafeAreaView } from 'react-native-safe-area-context'
|
||||
import { Ionicons } from '@expo/vector-icons'
|
||||
import { useAuth } from '@/hooks/useAuth'
|
||||
import { MOCK_MEMBER_ME } from '@/lib/mock-data'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
|
||||
type Item = {
|
||||
label: string
|
||||
@@ -20,9 +20,10 @@ const MENU_ITEMS: Item[] = [
|
||||
|
||||
export default function ProfilScreen() {
|
||||
const { signOut } = useAuth()
|
||||
const member = MOCK_MEMBER_ME
|
||||
const { data: me } = trpc.members.me.useQuery()
|
||||
const name = me?.name ?? ''
|
||||
|
||||
const initials = member.name
|
||||
const initials = name
|
||||
.split(' ')
|
||||
.slice(0, 2)
|
||||
.map((chunk) => chunk[0]?.toUpperCase() ?? '')
|
||||
@@ -42,7 +43,7 @@ export default function ProfilScreen() {
|
||||
<Ionicons name="settings-outline" size={15} color="#64748B" />
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
<Text style={styles.name}>{member.name}</Text>
|
||||
<Text style={styles.name}>{name}</Text>
|
||||
<Text style={styles.role}>Innungsgeschaeftsfuehrer</Text>
|
||||
<View style={styles.badgesRow}>
|
||||
<View style={styles.statusBadge}>
|
||||
|
||||
@@ -7,29 +7,40 @@ import { useRouter } from 'expo-router'
|
||||
import { SafeAreaView } from 'react-native-safe-area-context'
|
||||
import { Ionicons } from '@expo/vector-icons'
|
||||
import { authClient } from '@/lib/auth-client'
|
||||
import { useAuthStore } from '@/store/auth.store'
|
||||
|
||||
export default function LoginScreen() {
|
||||
const router = useRouter()
|
||||
const setSession = useAuthStore((s) => s.setSession)
|
||||
const [email, setEmail] = useState('')
|
||||
const [password, setPassword] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
|
||||
const canSubmit = email.trim().length > 0 && !loading
|
||||
const canSubmit = email.trim().length > 0 && password.length > 0 && !loading
|
||||
|
||||
async function handleSendLink() {
|
||||
if (!email.trim()) return
|
||||
async function handleLogin() {
|
||||
if (!canSubmit) return
|
||||
setLoading(true)
|
||||
setError('')
|
||||
const result = await authClient.signIn.magicLink({
|
||||
|
||||
const result = await authClient.signIn.email({
|
||||
email: email.trim().toLowerCase(),
|
||||
callbackURL: '/home',
|
||||
password,
|
||||
})
|
||||
setLoading(false)
|
||||
|
||||
if (result.error) {
|
||||
setError(result.error.message ?? 'Ein Fehler ist aufgetreten.')
|
||||
} else {
|
||||
router.push({ pathname: '/(auth)/check-email', params: { email } })
|
||||
setError(result.error.message ?? 'E-Mail oder Passwort falsch.')
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
const token = (result.data as any)?.session?.token
|
||||
const user = (result.data as any)?.user
|
||||
await setSession(user ? { user } : null, token)
|
||||
|
||||
setLoading(false)
|
||||
router.replace('/(app)/home' as never)
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -60,7 +71,21 @@ export default function LoginScreen() {
|
||||
autoCorrect={false}
|
||||
value={email}
|
||||
onChangeText={setEmail}
|
||||
onSubmitEditing={handleSendLink}
|
||||
returnKeyType="next"
|
||||
/>
|
||||
</View>
|
||||
|
||||
<Text style={[styles.inputLabel, { marginTop: 4 }]}>Passwort</Text>
|
||||
<View style={styles.inputWrap}>
|
||||
<Ionicons name="lock-closed-outline" size={18} color="#94A3B8" />
|
||||
<TextInput
|
||||
style={styles.input}
|
||||
placeholder="••••••••"
|
||||
placeholderTextColor="#94A3B8"
|
||||
secureTextEntry
|
||||
value={password}
|
||||
onChangeText={setPassword}
|
||||
onSubmitEditing={handleLogin}
|
||||
returnKeyType="go"
|
||||
/>
|
||||
</View>
|
||||
@@ -72,7 +97,7 @@ export default function LoginScreen() {
|
||||
) : null}
|
||||
|
||||
<TouchableOpacity
|
||||
onPress={handleSendLink}
|
||||
onPress={handleLogin}
|
||||
disabled={!canSubmit}
|
||||
style={[styles.submitBtn, !canSubmit && styles.submitBtnDisabled]}
|
||||
activeOpacity={0.85}
|
||||
@@ -81,7 +106,7 @@ export default function LoginScreen() {
|
||||
<ActivityIndicator color="#FFFFFF" />
|
||||
) : (
|
||||
<View style={styles.submitContent}>
|
||||
<Text style={styles.submitLabel}>Login-Link senden</Text>
|
||||
<Text style={styles.submitLabel}>Anmelden</Text>
|
||||
<Ionicons name="arrow-forward" size={16} color="#FFFFFF" />
|
||||
</View>
|
||||
)}
|
||||
@@ -89,7 +114,7 @@ export default function LoginScreen() {
|
||||
</View>
|
||||
|
||||
<Text style={styles.hint}>
|
||||
Noch kein Zugang? Kontaktieren Sie Ihre Innungsgeschaeftsstelle.
|
||||
Noch kein Zugang? Kontaktieren Sie Ihre Innungsgeschäftsstelle.
|
||||
</Text>
|
||||
</View>
|
||||
</KeyboardAvoidingView>
|
||||
@@ -98,109 +123,38 @@ export default function LoginScreen() {
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
safeArea: {
|
||||
flex: 1,
|
||||
backgroundColor: '#FFFFFF',
|
||||
},
|
||||
keyboardView: {
|
||||
flex: 1,
|
||||
},
|
||||
content: {
|
||||
flex: 1,
|
||||
justifyContent: 'center',
|
||||
paddingHorizontal: 24,
|
||||
},
|
||||
logoSection: {
|
||||
alignItems: 'center',
|
||||
marginBottom: 40,
|
||||
},
|
||||
safeArea: { flex: 1, backgroundColor: '#FFFFFF' },
|
||||
keyboardView: { flex: 1 },
|
||||
content: { flex: 1, justifyContent: 'center', paddingHorizontal: 24 },
|
||||
logoSection: { alignItems: 'center', marginBottom: 40 },
|
||||
logoBox: {
|
||||
width: 64,
|
||||
height: 64,
|
||||
backgroundColor: '#003B7E',
|
||||
borderRadius: 18,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
marginBottom: 16,
|
||||
},
|
||||
logoLetter: {
|
||||
color: '#FFFFFF',
|
||||
fontSize: 30,
|
||||
fontWeight: '900',
|
||||
},
|
||||
appName: {
|
||||
fontSize: 30,
|
||||
fontWeight: '800',
|
||||
color: '#0F172A',
|
||||
letterSpacing: -0.6,
|
||||
marginBottom: 4,
|
||||
},
|
||||
tagline: {
|
||||
fontSize: 14,
|
||||
color: '#64748B',
|
||||
textAlign: 'center',
|
||||
},
|
||||
form: {
|
||||
gap: 12,
|
||||
},
|
||||
inputLabel: {
|
||||
fontSize: 14,
|
||||
fontWeight: '700',
|
||||
color: '#334155',
|
||||
width: 64, height: 64, backgroundColor: '#003B7E',
|
||||
borderRadius: 18, alignItems: 'center', justifyContent: 'center', marginBottom: 16,
|
||||
},
|
||||
logoLetter: { color: '#FFFFFF', fontSize: 30, fontWeight: '900' },
|
||||
appName: { fontSize: 30, fontWeight: '800', color: '#0F172A', letterSpacing: -0.6, marginBottom: 4 },
|
||||
tagline: { fontSize: 14, color: '#64748B', textAlign: 'center' },
|
||||
form: { gap: 8 },
|
||||
inputLabel: { fontSize: 14, fontWeight: '700', color: '#334155' },
|
||||
inputWrap: {
|
||||
flexDirection: 'row',
|
||||
alignItems: 'center',
|
||||
backgroundColor: '#F8FAFC',
|
||||
borderRadius: 14,
|
||||
borderWidth: 1,
|
||||
borderColor: '#E2E8F0',
|
||||
paddingHorizontal: 12,
|
||||
gap: 8,
|
||||
},
|
||||
input: {
|
||||
flex: 1,
|
||||
paddingVertical: 13,
|
||||
color: '#0F172A',
|
||||
fontSize: 15,
|
||||
flexDirection: 'row', alignItems: 'center',
|
||||
backgroundColor: '#F8FAFC', borderRadius: 14,
|
||||
borderWidth: 1, borderColor: '#E2E8F0',
|
||||
paddingHorizontal: 12, gap: 8,
|
||||
},
|
||||
input: { flex: 1, paddingVertical: 13, color: '#0F172A', fontSize: 15 },
|
||||
errorBox: {
|
||||
backgroundColor: '#FEF2F2',
|
||||
borderWidth: 1,
|
||||
borderColor: '#FECACA',
|
||||
borderRadius: 12,
|
||||
paddingHorizontal: 14,
|
||||
paddingVertical: 10,
|
||||
},
|
||||
errorText: {
|
||||
color: '#B91C1C',
|
||||
fontSize: 13,
|
||||
backgroundColor: '#FEF2F2', borderWidth: 1,
|
||||
borderColor: '#FECACA', borderRadius: 12,
|
||||
paddingHorizontal: 14, paddingVertical: 10,
|
||||
},
|
||||
errorText: { color: '#B91C1C', fontSize: 13 },
|
||||
submitBtn: {
|
||||
backgroundColor: '#003B7E',
|
||||
borderRadius: 14,
|
||||
paddingVertical: 14,
|
||||
alignItems: 'center',
|
||||
marginTop: 4,
|
||||
},
|
||||
submitBtnDisabled: {
|
||||
backgroundColor: '#CBD5E1',
|
||||
},
|
||||
submitContent: {
|
||||
flexDirection: 'row',
|
||||
alignItems: 'center',
|
||||
gap: 6,
|
||||
},
|
||||
submitLabel: {
|
||||
color: '#FFFFFF',
|
||||
fontWeight: '700',
|
||||
fontSize: 15,
|
||||
},
|
||||
hint: {
|
||||
marginTop: 24,
|
||||
textAlign: 'center',
|
||||
color: '#64748B',
|
||||
fontSize: 13,
|
||||
lineHeight: 18,
|
||||
backgroundColor: '#003B7E', borderRadius: 14,
|
||||
paddingVertical: 14, alignItems: 'center', marginTop: 8,
|
||||
},
|
||||
submitBtnDisabled: { backgroundColor: '#CBD5E1' },
|
||||
submitContent: { flexDirection: 'row', alignItems: 'center', gap: 6 },
|
||||
submitLabel: { color: '#FFFFFF', fontWeight: '700', fontSize: 15 },
|
||||
hint: { marginTop: 24, textAlign: 'center', color: '#64748B', fontSize: 13, lineHeight: 18 },
|
||||
})
|
||||
|
||||
@@ -2,6 +2,7 @@ import '../global.css'
|
||||
import { useEffect } from 'react'
|
||||
import { Stack, SplashScreen } from 'expo-router'
|
||||
import { useAuthStore } from '@/store/auth.store'
|
||||
import { TRPCProvider } from '@/lib/trpc'
|
||||
|
||||
SplashScreen.preventAutoHideAsync()
|
||||
|
||||
@@ -16,11 +17,13 @@ export default function RootLayout() {
|
||||
if (!isInitialized) return null
|
||||
|
||||
return (
|
||||
<Stack screenOptions={{ headerShown: false }}>
|
||||
<Stack.Screen name="index" />
|
||||
<Stack.Screen name="(auth)" options={{ animation: 'fade' }} />
|
||||
<Stack.Screen name="(app)" options={{ animation: 'fade' }} />
|
||||
<Stack.Screen name="stellen-public/index" />
|
||||
</Stack>
|
||||
<TRPCProvider>
|
||||
<Stack screenOptions={{ headerShown: false }}>
|
||||
<Stack.Screen name="index" />
|
||||
<Stack.Screen name="(auth)" options={{ animation: 'fade' }} />
|
||||
<Stack.Screen name="(app)" options={{ animation: 'fade' }} />
|
||||
<Stack.Screen name="stellen-public/index" />
|
||||
</Stack>
|
||||
</TRPCProvider>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -12,10 +12,7 @@ export function useAuth() {
|
||||
|
||||
return {
|
||||
session,
|
||||
orgId: 'org-1',
|
||||
role: 'member' as const,
|
||||
isAuthenticated: true, // Mock: immer eingeloggt
|
||||
isAdmin: false,
|
||||
isAuthenticated: !!session,
|
||||
signOut: handleSignOut,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,25 @@
|
||||
import { MOCK_MEMBERS } from '@/lib/mock-data'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
import { useMembersFilterStore } from '@/store/members.store'
|
||||
|
||||
export function useMembersList() {
|
||||
const search = useMembersFilterStore((s) => s.search)
|
||||
const nurAusbildungsbetriebe = useMembersFilterStore((s) => s.nurAusbildungsbetriebe)
|
||||
|
||||
let data = MOCK_MEMBERS.filter((m) => m.status === 'aktiv')
|
||||
const { data, isLoading, refetch, isFetching } = trpc.members.list.useQuery({
|
||||
search: search || undefined,
|
||||
status: 'aktiv',
|
||||
ausbildungsbetrieb: nurAusbildungsbetriebe || undefined,
|
||||
})
|
||||
|
||||
if (search) {
|
||||
const q = search.toLowerCase()
|
||||
data = data.filter(
|
||||
(m) =>
|
||||
m.name.toLowerCase().includes(q) ||
|
||||
m.betrieb.toLowerCase().includes(q) ||
|
||||
m.ort.toLowerCase().includes(q) ||
|
||||
m.sparte.toLowerCase().includes(q)
|
||||
)
|
||||
return {
|
||||
data: data ?? [],
|
||||
isLoading,
|
||||
refetch,
|
||||
isRefetching: isFetching,
|
||||
}
|
||||
|
||||
if (nurAusbildungsbetriebe) {
|
||||
data = data.filter((m) => m.istAusbildungsbetrieb)
|
||||
}
|
||||
|
||||
return { data, isLoading: false, refetch: () => {}, isRefetching: false }
|
||||
}
|
||||
|
||||
export function useMemberDetail(id: string) {
|
||||
const data = MOCK_MEMBERS.find((m) => m.id === id) ?? null
|
||||
return { data, isLoading: false }
|
||||
const { data, isLoading } = trpc.members.byId.useQuery({ id })
|
||||
return { data: data ?? null, isLoading }
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import { useState } from 'react'
|
||||
import { MOCK_NEWS } from '@/lib/mock-data'
|
||||
import { useNewsReadStore } from '@/store/news.store'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
|
||||
export function useNewsList(kategorie?: string) {
|
||||
const localReadIds = useNewsReadStore((s) => s.readIds)
|
||||
const filtered = kategorie
|
||||
? MOCK_NEWS.filter((n) => n.kategorie === kategorie)
|
||||
: MOCK_NEWS
|
||||
const { data, isLoading, refetch, isFetching } = trpc.news.list.useQuery({
|
||||
kategorie: kategorie || undefined,
|
||||
})
|
||||
|
||||
const data = filtered.map((n) => ({
|
||||
...n,
|
||||
isRead: n.isRead || localReadIds.has(n.id),
|
||||
}))
|
||||
|
||||
return { data, isLoading: false, refetch: () => {}, isRefetching: false }
|
||||
return {
|
||||
data: data ?? [],
|
||||
isLoading,
|
||||
refetch,
|
||||
isRefetching: isFetching,
|
||||
}
|
||||
}
|
||||
|
||||
export function useNewsDetail(id: string) {
|
||||
const markRead = useNewsReadStore((s) => s.markRead)
|
||||
const news = MOCK_NEWS.find((n) => n.id === id) ?? null
|
||||
const utils = trpc.useUtils()
|
||||
const { data, isLoading } = trpc.news.byId.useQuery({ id })
|
||||
|
||||
const markReadMutation = trpc.news.markRead.useMutation()
|
||||
|
||||
function onOpen() {
|
||||
markRead(id)
|
||||
markReadMutation.mutate({ newsId: id })
|
||||
utils.news.list.invalidate()
|
||||
}
|
||||
|
||||
return { data: news, isLoading: false, onOpen }
|
||||
return { data: data ?? null, isLoading, onOpen }
|
||||
}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
import { MOCK_STELLEN } from '@/lib/mock-data'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
|
||||
export function useStellenListe(opts?: { sparte?: string; lehrjahr?: string }) {
|
||||
let data = MOCK_STELLEN.filter((s) => s.aktiv)
|
||||
if (opts?.sparte) data = data.filter((s) => s.sparte === opts.sparte)
|
||||
if (opts?.lehrjahr) data = data.filter((s) => s.lehrjahr === opts.lehrjahr)
|
||||
return { data, isLoading: false, refetch: () => {}, isRefetching: false }
|
||||
const { data, isLoading, refetch, isFetching } = trpc.stellen.listPublic.useQuery({
|
||||
sparte: opts?.sparte,
|
||||
lehrjahr: opts?.lehrjahr,
|
||||
})
|
||||
|
||||
return {
|
||||
data: data ?? [],
|
||||
isLoading,
|
||||
refetch,
|
||||
isRefetching: isFetching,
|
||||
}
|
||||
}
|
||||
|
||||
export function useStelleDetail(id: string) {
|
||||
const data = MOCK_STELLEN.find((s) => s.id === id) ?? null
|
||||
return { data, isLoading: false }
|
||||
const { data, isLoading } = trpc.stellen.byId.useQuery({ id })
|
||||
return { data: data ?? null, isLoading }
|
||||
}
|
||||
|
||||
@@ -1,33 +1,32 @@
|
||||
import { useState } from 'react'
|
||||
import { MOCK_TERMINE } from '@/lib/mock-data'
|
||||
import { trpc } from '@/lib/trpc'
|
||||
|
||||
export function useTermineListe(upcoming = true) {
|
||||
const now = new Date()
|
||||
const data = MOCK_TERMINE.filter((t) =>
|
||||
upcoming ? t.datum >= now : t.datum < now
|
||||
).sort((a, b) =>
|
||||
upcoming ? a.datum.getTime() - b.datum.getTime() : b.datum.getTime() - a.datum.getTime()
|
||||
)
|
||||
return { data, isLoading: false, refetch: () => {}, isRefetching: false }
|
||||
const { data, isLoading, refetch, isFetching } = trpc.termine.list.useQuery({ upcoming })
|
||||
|
||||
return {
|
||||
data: data ?? [],
|
||||
isLoading,
|
||||
refetch,
|
||||
isRefetching: isFetching,
|
||||
}
|
||||
}
|
||||
|
||||
export function useTerminDetail(id: string) {
|
||||
const data = MOCK_TERMINE.find((t) => t.id === id) ?? null
|
||||
return { data, isLoading: false }
|
||||
const { data, isLoading } = trpc.termine.byId.useQuery({ id })
|
||||
return { data: data ?? null, isLoading }
|
||||
}
|
||||
|
||||
export function useToggleAnmeldung() {
|
||||
const [isPending, setIsPending] = useState(false)
|
||||
const utils = trpc.useUtils()
|
||||
const mutation = trpc.termine.toggleAnmeldung.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.termine.list.invalidate()
|
||||
utils.termine.byId.invalidate()
|
||||
},
|
||||
})
|
||||
|
||||
function mutate({ terminId }: { terminId: string }) {
|
||||
setIsPending(true)
|
||||
const termin = MOCK_TERMINE.find((t) => t.id === terminId)
|
||||
if (termin) {
|
||||
termin.isAngemeldet = !termin.isAngemeldet
|
||||
termin.teilnehmerAnzahl += termin.isAngemeldet ? 1 : -1
|
||||
}
|
||||
setTimeout(() => setIsPending(false), 300)
|
||||
return {
|
||||
mutate: ({ terminId }: { terminId: string }) => mutation.mutate({ terminId }),
|
||||
isPending: mutation.isPending,
|
||||
}
|
||||
|
||||
return { mutate, isPending }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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 ??
|
||||
@@ -10,4 +11,15 @@ const apiUrl =
|
||||
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 })
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { create } from 'zustand'
|
||||
import { MOCK_MEMBER_ME } from '@/lib/mock-data'
|
||||
import { authClient } from '@/lib/auth-client'
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
|
||||
interface Session {
|
||||
user: { id: string; email: string; name: string }
|
||||
@@ -9,26 +10,48 @@ interface AuthState {
|
||||
session: Session | null
|
||||
isInitialized: boolean
|
||||
initialize: () => Promise<void>
|
||||
setSession: (session: Session | null, token?: string) => Promise<void>
|
||||
signOut: () => Promise<void>
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>((set) => ({
|
||||
// Mock: direkt eingeloggt
|
||||
session: {
|
||||
user: {
|
||||
id: MOCK_MEMBER_ME.userId!,
|
||||
email: MOCK_MEMBER_ME.email,
|
||||
name: MOCK_MEMBER_ME.name,
|
||||
},
|
||||
},
|
||||
isInitialized: true,
|
||||
session: null,
|
||||
isInitialized: false,
|
||||
|
||||
initialize: async () => {
|
||||
// Mock: nichts zu tun
|
||||
set({ isInitialized: true })
|
||||
try {
|
||||
// Check if we have a stored token and validate it
|
||||
const token = await AsyncStorage.getItem('better-auth-session')
|
||||
if (!token) {
|
||||
set({ session: null, isInitialized: true })
|
||||
return
|
||||
}
|
||||
// authClient now sends the token via cookie header (see auth-client.ts)
|
||||
const result = await authClient.getSession()
|
||||
if (result?.data?.user) {
|
||||
set({
|
||||
session: { user: result.data.user },
|
||||
isInitialized: true,
|
||||
})
|
||||
} else {
|
||||
await AsyncStorage.removeItem('better-auth-session')
|
||||
set({ session: null, isInitialized: true })
|
||||
}
|
||||
} catch {
|
||||
set({ session: null, isInitialized: true })
|
||||
}
|
||||
},
|
||||
|
||||
setSession: async (session, token) => {
|
||||
if (token) {
|
||||
await AsyncStorage.setItem('better-auth-session', token)
|
||||
}
|
||||
set({ session })
|
||||
},
|
||||
|
||||
signOut: async () => {
|
||||
await authClient.signOut()
|
||||
await AsyncStorage.removeItem('better-auth-session')
|
||||
set({ session: null })
|
||||
},
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user