wip: connect firebase token to apollo headers

This commit is contained in:
Loewy
2024-02-05 22:50:24 -08:00
parent 01f1f94dd6
commit 6bfd0621ad
6 changed files with 114 additions and 69 deletions

48
auth/firebase-auth.tsx Normal file
View File

@@ -0,0 +1,48 @@
import auth, { FirebaseAuthTypes } from '@react-native-firebase/auth';
import { Alert } from 'react-native';
export const signInWithPhoneNumber = async (phoneNumber: string): Promise<FirebaseAuthTypes.ConfirmationResult | undefined> => {
if (!phoneNumber) {
Alert.alert("Please enter a valid phone number with a country code");
return;
}
try {
const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
return confirmation;
} catch (err) {
console.warn(err);
Alert.alert("There was an error. Make sure you are using a country code (ex: +1 for US)");
}
};
export const confirmCode = async (confirm: FirebaseAuthTypes.ConfirmationResult, code: string): Promise<void> => {
try {
await confirm.confirm(code);
} catch {
Alert.alert("Invalid code, please try again.");
}
};
export const onAuthStateChanged = (callback: (user: FirebaseAuthTypes.User | null) => void) => {
return auth().onAuthStateChanged(callback);
};
export const getCurrentUserToken = async (): Promise<string | null> => {
const user = auth().currentUser;
if (user) {
return await user.getIdToken();
}
return null;
};
export const signOut = async (): Promise<void> => {
try {
auth().signOut()
// tie in to AppNav
} catch (err) {
console.error(err)
// toggle appnav state regardless
// Handle sign out error - have to look into best way to do this - asyncstorage?
}
}

15
auth/index.ts Normal file
View File

@@ -0,0 +1,15 @@
import {
signInWithPhoneNumber,
confirmCode,
onAuthStateChanged,
getCurrentUserToken,
signOut
} from './firebase-auth'
export {
signInWithPhoneNumber,
confirmCode,
onAuthStateChanged,
getCurrentUserToken,
signOut
}