feat(mobile): FirebaseAuthProvider (e-post/lösen) bakom AuthProvider-gränssnittet (AUTH-4)

This commit is contained in:
Sven (AAMOS AI)
2026-08-15 20:39:29 +07:00
parent 8f61ef1592
commit 8352c3a0ce
3 changed files with 642 additions and 26 deletions
+1
View File
@@ -32,6 +32,7 @@
"expo-splash-screen": "~31.0.13",
"expo-status-bar": "~3.0.9",
"expo-system-ui": "~6.0.9",
"firebase": "^12.17.1",
"i18next": "^26.3.6",
"react": "19.1.0",
"react-dom": "19.1.0",
@@ -0,0 +1,88 @@
import { initializeApp, getApps, getApp } from "firebase/app";
import {
initializeAuth,
getAuth,
onAuthStateChanged as fbOnAuthStateChanged,
signInWithEmailAndPassword,
createUserWithEmailAndPassword,
signOut as fbSignOut,
type Auth,
type User,
type Persistence,
} from "firebase/auth";
import * as fbAuth from "firebase/auth";
import AsyncStorage from "@react-native-async-storage/async-storage";
import type { AuthProvider, AuthUser, Unsubscribe } from "./types";
/**
* ENDA mobilfilen som importerar Firebase. App-koden använder bara
* AuthProvider-gränssnittet. Byt till Zitadel => byt bara denna fil.
* Publik klient-config (identifierare, ingen hemlighet).
*/
const firebaseConfig = {
apiKey: "AIzaSyAH2F-aZRvZXQtGD7X8cF9Zp3yXUhEl3sM",
authDomain: "cibello-c2ff3.firebaseapp.com",
projectId: "cibello-c2ff3",
storageBucket: "cibello-c2ff3.firebasestorage.app",
messagingSenderId: "1034102905039",
appId: "1:1034102905039:web:12e31120989655f34affba",
};
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
// getReactNativePersistence finns i runtime men saknas ibland i typerna
// (firebase-js-sdk#9316) hämtas därför via en typsäker modul-cast.
const getReactNativePersistence = (
fbAuth as unknown as { getReactNativePersistence: (storage: unknown) => Persistence }
).getReactNativePersistence;
// AsyncStorage-persistens så inloggningen överlever app-omstart.
let auth: Auth;
try {
auth = initializeAuth(app, { persistence: getReactNativePersistence(AsyncStorage) });
} catch {
// initializeAuth kastar om den redan körts (Fast Refresh) återanvänd instansen.
auth = getAuth(app);
}
function toAuthUser(u: User | null): AuthUser | null {
if (!u) return null;
return {
uid: u.uid,
email: u.email,
emailVerified: u.emailVerified,
displayName: u.displayName,
providerId: u.providerData[0]?.providerId ?? null,
};
}
export const firebaseAuthProvider: AuthProvider = {
async signUpWithEmail(email, password) {
const cred = await createUserWithEmailAndPassword(auth, email, password);
return toAuthUser(cred.user)!;
},
async signInWithEmail(email, password) {
const cred = await signInWithEmailAndPassword(auth, email, password);
return toAuthUser(cred.user)!;
},
async signInWithGoogle(): Promise<AuthUser> {
throw new Error("Google-inloggning läggs till i AUTH-6.");
},
async signInWithApple(): Promise<AuthUser> {
throw new Error("Apple-inloggning läggs till i AUTH-6.");
},
async signOut() {
await fbSignOut(auth);
},
currentUser() {
return toAuthUser(auth.currentUser);
},
onAuthStateChanged(callback): Unsubscribe {
return fbOnAuthStateChanged(auth, (u) => callback(toAuthUser(u)));
},
async getIdToken(forceRefresh = false) {
const u = auth.currentUser;
if (!u) return null;
return u.getIdToken(forceRefresh);
},
};