chahinebrini 01d515d137 feat(rebreak-native): persistent FaceID-sign-in + iOS-grouped UI + Outlook guard + sparkline cooldowns
Auth / FaceID — eingeloggt bleiben funktioniert jetzt:
- AppLock-Init idempotent: late re-init durch router.replace-Re-Mount behält
  locked-State (fixt Endlosschleife: unlock → re-mount → init reset → lock)
- LockScreen-Auto-Prompt nur wenn AppState=active (verhindert silent FaceID-
  Fail wenn LockScreen während background-Event mountet — User sah dann nur
  Fallback-Button)
- index.tsx: wenn Session schon in AsyncStorage liegt → router.replace zu /(app),
  Landing wird übersprungen; early-return nach allen Hooks (Rules of Hooks)
- WebBrowser.dismissAuthSession vor openAuthSessionAsync (verhindert
  "Another web browser is already open" nach abgebrochenen OAuth-Flows)

UI — iOS-Grouped-Look auf Settings + Profile:
- Neue Theme-Tokens groupedBg (#F2F2F7 / #000) + card (#fff / #1c1c1e),
  identisch zu Apples systemGroupedBackground / secondarySystemGroupedBackground
- settings.tsx + profile/index.tsx + profile/[userId].tsx: Page-BG → groupedBg
- StreakSection / UrgeStatsCard / DemographicsAccordion / StatsBar /
  ApprovedDomainsList: Card-BG colors.surface → colors.card

Mail-Connect — Outlook-Tile entschärft:
- Microsoft hat App-Passwords für consumer-Outlook (.com/hotmail/live/msn) im
  September 2024 abgeschaltet, der bisherige Guide-Flow ist seit ~8 Monaten
  wirkungslos → AUTHENTICATIONFAILED
- Tile bleibt sichtbar mit opacity 0.45, "Kommt bald"-Sub-Label, disabled=true
- Provider-Typ um disabled? + disabledLabelKey? erweitert (wiederverwendbar)
- Backend-OAuth-Plan unter backend/docs/mail-outlook-oauth-plan.md (mo)
  → Generisches AuthMethod-Framework (app_password | oauth) geplant

Profile — Cooldown-Verlauf als Sparkline statt Endlos-Liste:
- 8 Wochen-Buckets, Bar-Höhe nach Frequenz (cap 5/Woche), leere Wochen als
  2px-Flatlines
- Sub-Label: "{n} Cooldowns in 8 Wochen · Ø 1 pro {avg} Wochen · zuletzt {date}"
- Neutral formuliert (Sucht-/Stigma-Sensibilität: Cooldown = Schutz-Pause,
  kein Rückfall)
- useProfileData.ts liefert rawStartedAt (ISO) zusätzlich zum formatierten Wert
- i18n-Keys unter profile.cooldown.* in DE + EN

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 16:15:54 +02:00

119 lines
4.6 KiB
TypeScript

import { create } from 'zustand';
import AsyncStorage from '@react-native-async-storage/async-storage';
// expo-local-authentication ist ein NATIVE-Modul. In einem Dev-Client, der noch
// VOR dem Hinzufügen dieser Dependency gebaut wurde, fehlt der native Teil →
// `require()` wirft "Cannot find native module 'ExpoLocalAuthentication'" und
// würde die ganze App beim Start crashen (appLock wird im RootLayout importiert).
// Daher: defensiv laden. Fehlt das Modul → App-Sperre einfach „nicht verfügbar",
// der Rest der App läuft normal weiter. In jedem echten Build (EAS / frischer
// `expo prebuild`) ist das Modul drin und alles funktioniert.
type LocalAuthModule = typeof import('expo-local-authentication');
let LocalAuthentication: LocalAuthModule | null = null;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, global-require
LocalAuthentication = require('expo-local-authentication') as LocalAuthModule;
} catch {
LocalAuthentication = null;
}
/**
* App-Sperre (Face ID / Touch ID).
*
* Liegt OBEN auf der bereits authentifizierten Supabase-Session — re-auth beim
* Öffnen, kein Login-Ersatz. Sucht-/Stigma-Schutz: wer dein entsperrtes iPhone
* nimmt, kann Rebreak nicht öffnen.
*
* - `enabled` → User-Präferenz, persistiert (AsyncStorage)
* - `locked` → in-memory: ist die App gerade hinter der Sperre?
* - `available`→ Gerät kann biometrisch ODER per Geräte-Passcode auth'en
*
* Lock-Timing: sofort beim Backgrounden (siehe AppLockGate / AppState-Listener).
*/
const STORAGE_KEY = '@rebreak/app-lock-enabled';
/** Reason-String im System-Prompt von Face ID / Touch ID. Wird i18n überschrieben
* wo wir `t()` haben — dieser Default greift nur falls der Aufrufer keinen liefert. */
const DEFAULT_PROMPT = 'Rebreak entsperren';
type AppLockState = {
/** persistierte User-Präferenz */
enabled: boolean;
/** in-memory: App ist gerade gesperrt */
locked: boolean;
/** Gerät unterstützt Biometrie/Passcode-Auth (sonst Toggle ausgrauen) */
available: boolean;
/** init() durch */
ready: boolean;
init: () => Promise<void>;
setEnabled: (enabled: boolean) => Promise<void>;
/** sperren (App geht in den Hintergrund) */
lock: () => void;
/** Face-ID/Touch-ID/Passcode-Prompt — bei Erfolg entsperrt. Gibt success zurück. */
authenticate: (promptMessage?: string) => Promise<boolean>;
};
export const useAppLockStore = create<AppLockState>((set, get) => ({
enabled: false,
locked: false,
available: false,
ready: false,
init: async () => {
// Idempotenz: nur beim allerersten init() den locked-Default setzen. Spätere
// init()-Calls (z.B. wenn RootLayoutInner durch router.replace re-mountet)
// dürfen den aktuellen locked-Zustand NICHT zurücksetzen — sonst entsteht
// eine Endlosschleife: unlock → re-mount → init() → locked=true wieder.
const alreadyReady = get().ready;
if (!LocalAuthentication) {
// Native-Modul fehlt (alter Dev-Client) → Sperre nicht verfügbar, App läuft weiter.
set({ enabled: false, available: false, locked: false, ready: true });
return;
}
const [storedRaw, hasHardware, isEnrolled] = await Promise.all([
AsyncStorage.getItem(STORAGE_KEY),
LocalAuthentication.hasHardwareAsync(),
LocalAuthentication.isEnrolledAsync(),
]);
// available = Gerät hat Biometrie-Hardware UND mind. eine Methode eingerichtet
// (Face/Touch ODER Geräte-Passcode — isEnrolledAsync deckt beides ab).
const available = hasHardware && isEnrolled;
const enabled = storedRaw === 'true' && available;
set({
enabled,
available,
// Cold-Start: locked=enabled (kein Flash von App-Inhalt vor LockScreen).
// Re-Init: aktuellen locked-Stand erhalten — sonst Loop.
locked: alreadyReady ? get().locked : enabled,
ready: true,
});
},
setEnabled: async (enabled) => {
await AsyncStorage.setItem(STORAGE_KEY, enabled ? 'true' : 'false');
set({ enabled, locked: false });
},
lock: () => {
if (get().enabled) set({ locked: true });
},
authenticate: async (promptMessage) => {
if (!LocalAuthentication) return false;
const result = await LocalAuthentication.authenticateAsync({
promptMessage: promptMessage ?? DEFAULT_PROMPT,
// Geräte-Passcode als Fallback erlauben (Face ID schlägt 3x fehl → Passcode).
// Wichtig, damit man sich nicht aus der App aussperrt.
disableDeviceFallback: false,
cancelLabel: undefined,
});
if (result.success) {
set({ locked: false });
return true;
}
return false;
},
}));