Skip to content

Instantly share code, notes, and snippets.

View EhsanHadid's full-sized avatar

Ehsan Hadid EhsanHadid

View GitHub Profile
@EhsanHadid
EhsanHadid / register.tsx
Last active February 23, 2023 09:30
register the user info and link account
const user = useUser();
const registerFormHandler = async ({ fullname, password }) => {
if (user && user.phoneNumber) {
const email = convertPhoneToEmail(user.phoneNumber);
const credential = EmailAuthProvider.credential(email, password);
const userCred = await linkWithCredential(user, credential);
await updateProfile(userCred.user, {
displayName: fullname,
});
@EhsanHadid
EhsanHadid / code-verification.tsx
Last active February 23, 2023 09:43
verify phone number
const verifyCode = ({ code }) => {
const credential = PhoneAuthProvider.credential(verificationId, code);
signInWithCredential(auth, credential).then((userCredential) => {
console.log(userCredential);
});
};
@EhsanHadid
EhsanHadid / phone-util.ts
Created February 23, 2023 09:00
phone util
export const convertPhoneToEmail = (phone: string) => {
// replacing special characters( +,-, (, ), and #. ) to alphabet chars
console.log(phone);
const cleanedPhone = phone
.replace("(", "A")
.replace(")", "B")
.replace(",", "C")
.replace("#", "D")
.replace(" ", "E");
@EhsanHadid
EhsanHadid / phone-verification.tsx
Last active February 23, 2023 09:04
sendVerificationCode
const sendVerificationCode = async () => {
setLoading(true);
try {
const email = convertPhoneToEmail(phoneNumber);
const providers = await fetchSignInMethodsForEmail(auth, email);
if (providers.length == 0) {
const confirmationResult = await signInWithPhoneNumber(
auth,
phoneNumber,
@EhsanHadid
EhsanHadid / phone-verification.tsx
Last active February 23, 2023 08:07
Firebase Phone Authentication plus Password Integration with React
import { useState } from "react";
import { Button, Form } from "antd";
import { RecaptchaVerifier, signInWithPhoneNumber } from "firebase/auth";
import { auth } from "../../firebase";
import { PhoneNumberInput } from "./phone-input";
export default function PhoneVerification({ codeSent }) {
const [phoneNumber, setPhoneNumber] = useState<string>("");
const [loading, setLoading] = useState(false);
const [isPhoneEmpty, setIsPhoneEmpty] = useState<boolean>(true);