eletrotupi / tcc/ frontend/context/authRouting.ts11443325051f8ebf8d5bdcba4d184e4c95d1f887
1006 B | 35 lines Raw
1
import { type Href } from 'expo-router';
2
import { User } from '@/lib/api';
3

4
export type AuthStatus =
5
  | 'loading'
6
  | 'signedOut'
7
  | 'awaitingActivation'
8
  | 'active';
9

10
// The single source of truth for "where does this account belong". Everything
11
// that routes on auth state reads this instead of re-deriving it from
12
// `user.active` and ad-hoc booleans.
13
export function deriveStatus(isLoading: boolean, user: User | null): AuthStatus {
14
  if (isLoading) return 'loading';
15
  if (!user) return 'signedOut';
16
  if (!user.active) return 'awaitingActivation';
17
  return 'active';
18
}
19

20
// `null` means "still loading — show a spinner, do not navigate".
21
export function routeForStatus(
22
  status: AuthStatus,
23
  user: User | null,
24
): Href | null {
25
  switch (status) {
26
    case 'loading':
27
      return null;
28
    case 'signedOut':
29
      return '/auth/login';
30
    case 'awaitingActivation':
31
      return '/auth/activate';
32
    case 'active':
33
      return user?.onboardingSeenAt ? '/(tabs)/new' : '/onboarding';
34
  }
35
}