Files
cally/hooks/firebase/useLoginWithQrCode.ts
Milan Paunovic 70db8bdc0b New calendar
2024-12-15 16:29:34 +01:00

25 lines
972 B
TypeScript

import {useMutation} from "@tanstack/react-query";
import functions, {FirebaseFunctionsTypes} from '@react-native-firebase/functions';
import auth from "@react-native-firebase/auth";
import {useAuthContext} from "@/contexts/AuthContext";
export const useLoginWithQrCode = () => {
const {setRedirectOverride} = useAuthContext()
return useMutation({
mutationKey: ["loginWithQrCode"],
mutationFn: async ({userId}: { userId: string }) => {
try {
setRedirectOverride(true)
const res = await functions().httpsCallable("generateCustomToken")({userId}) as FirebaseFunctionsTypes.HttpsCallableResult<{
token: string
}>
console.log(res)
await auth().signInWithCustomToken(res?.data?.token)
} catch (e) {
console.error(e)
throw Error("Failed to login with QR code.")
}
},
});
}