الهدف
بعد هالدرس تعرف ليش localStorage غلط على الـ mobile، تخزّن auth tokens بأمان في Keychain/Keystore، وتضيف Face ID / fingerprint login لتطبيقك.
ليش هذا الحين؟
الـ auth على الـ mobile مختلف عن الـ web بطريقة غير واضحة: localStorage ينحذف لما المستخدم يمسح الـ app data أو يحدّث iOS. الـ cookies ما تشتغل بنفس الطريقة في WebView. والمستخدم يتوقع يفتح التطبيق بـ Face ID — مو username و password كل مرة.
الفكرة
الـ Storage على الـ Mobile:
| Storage | iOS | Android | مناسب لـ |
|---|---|---|---|
| localStorage | ينمسح أحيانا | ينمسح أحيانا | لا للـ tokens |
@capacitor/preferences |
UserDefaults | SharedPreferences | Settings بسيطة |
| Keychain (iOS) / Keystore (Android) | مشفّر بـ hardware | مشفّر بـ hardware | Tokens، passwords |
القاعدة الأساسية: أي شي حساس (auth token، API key، كلمة مرور) — يروح في Keychain/Keystore.
Biometric Auth: Face ID و Touch ID على iOS، Fingerprint و Face Unlock على Android. الـ biometric authentication ما تخزّن بياناتك الحيوية — بس تثبت إن الشخص اللي قدام الجهاز هو صاحبه. بعد التحقق، تطلّع الـ token المحفوظ في Keychain.
Better Auth + Mobile: الـ Backend ما يتغير — نفس API، نفس sessions. الفرق بس في كيف تحفظ الـ session token على الجهاز.