30 lines
701 B
TypeScript
30 lines
701 B
TypeScript
import { redirect } from '@tanstack/react-router'
|
|
|
|
import type { AppLanguage } from '@/i18n'
|
|
import { getPreferredLanguage } from '@/i18n'
|
|
import { useAuthStore } from '@/store/auth-store'
|
|
|
|
import { initializeAuthSession, isAuthenticated } from './auth-session'
|
|
|
|
interface RequireAuthenticatedSessionOptions {
|
|
fallbackLanguage?: AppLanguage
|
|
}
|
|
|
|
export async function requireAuthenticatedSession(
|
|
options: RequireAuthenticatedSessionOptions = {},
|
|
) {
|
|
await initializeAuthSession()
|
|
|
|
if (isAuthenticated()) {
|
|
return useAuthStore.getState()
|
|
}
|
|
|
|
throw redirect({
|
|
to: '/$lang',
|
|
params: {
|
|
lang: options.fallbackLanguage ?? getPreferredLanguage(),
|
|
},
|
|
replace: true,
|
|
})
|
|
}
|