Firebase-React Native Authentication linkWithCredential Error

I am developing a React Native mobile app in which I want to use Firebase’s phone authentication as well as Email Password authentication. For this purpose, after the user signs up using their mobile phone by verifying OTP, they have to enter their email and create a password. Then I try to link these credentials to the already logged in user but I am getting the following error:

Account linking error [Error: [auth/invalid-credential] The supplied auth credential is malformed, has expired or is not currently supported.]

import React from 'react'
import { useState } from 'react';
import { View, Text, TouchableOpacity, Pressable, StyleSheet, Image, TextInput } from 'react-native'
import { useNavigation } from '@react-navigation/native';
import { linkWithCredential, EmailAuthProvider } from "firebase/auth";
import auth from "@react-native-firebase/auth";


const SignupCreatePassword = ({ route }) => {
    const [password, setPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    const [errorMessage, setErrorMessage] = useState('');
    const [secureText, setSecureText] = useState(true)
    const { userEmail } = route.params

    const validatePassword = (password: string): boolean => {
        const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*d).{12,}$/;
        return passwordRegex.test(password);
    };

    const handleContinue = async () => {
        if (!validatePassword(password)) {
            setErrorMessage(
                'Password must be at least 12 characters long and include uppercase, lowercase, and numbers.'
            );
            return;
        }
        if (password !== confirmPassword) {
            setErrorMessage('Passwords do not match.');
            return;
        }
        setErrorMessage('');
        // Proceed to the next screen
        console.log('Password is valid and matches.');
        const credential = EmailAuthProvider.credential(userEmail, password);
        console.log("These are the credentials", credential)
        await auth().currentUser?.linkWithCredential(auth().currentUser, credential)
        .then((usercred) => {
            const user = usercred.user;
            console.log("Account linking success", user);
        }).catch((error) => {
            console.log("Account linking error", error);
        });
        
    };
    

  return (
    <View style={style.container}>
        
    {/*Back button and Sign In button */}
    <View style={style.topContainer} >
        <TouchableOpacity style={style.arrowButton}>
            <Image source={require('../../../assets/icons/arrow-left.png')}></Image>
        </TouchableOpacity>
    </View>

    {/* Main heading and subtext */}
    <View className='gap-2'>
        <Text className='font-inter-semibold' style={style.textTwo}>Create Password</Text>
        <Text className='font-inter-regular leading-[26px]' style={style.textThree}>Create a strong password for your future logins.</Text>
    </View>

    {/* Input field and button */}
    <View  className='gap-10 w-full items-center justify-center'>
        <View className='gap-5'>
        <View style={style.inputContainer}>
        <Image className='ml-5 -mt-1' source={require('../../../assets/icons/lock-icon.png')}></Image>
        <TextInput secureTextEntry={secureText} className='font-inter-regular'
        value={password}
        onChangeText={setPassword}
        style={style.inputField} 
        placeholder="Enter Password"
        />
        </View>

        <View style={style.inputContainer}>
        <Image className='ml-5 -mt-1' source={require('../../../assets/icons/lock-icon.png')}></Image>
        <TextInput secureTextEntry={secureText} className='font-inter-regular'
        value={confirmPassword}
        onChangeText={setConfirmPassword}
        style={style.inputField} 
        placeholder="Confirm Password"
        />
        <Pressable
         onPress={() => setSecureText(!secureText)}
            >
        <Image
        className="mr-6 -mt-1"
        source={secureText 
            ? require('../../../assets/icons/eyeslash-icon.png') 
            : require('../../../assets/icons/eye-icon.png')}
        />
        </Pressable>
        </View>
        </View>
        {errorMessage ? <Text>{errorMessage}</Text> : null}
        <TouchableOpacity style={style.inputButton} onPress={handleContinue}>
        <Text className='font-inter-semibold' style={style.textFour}>Continue</Text>
        </TouchableOpacity>
    </View>
    </View>
  )
}

const style = StyleSheet.create({
    container : {
        flex: 1,
        backgroundColor: 'rgba(45, 43, 46, 1)',
        padding: 20,
        gap: 40,

    },
    topContainer: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',        
    },
    arrowButton : {
        alignItems: 'center',
        justifyContent: 'center',
        height: 40,
        width: 40,
        backgroundColor: 'rgba(234, 235, 240, 1)',
        borderRadius: 100
    },
    textOne :{
        color : 'rgba(110, 156, 58, 1)',
        fontSize: 18,
        textDecorationLine: 'underline'
    },
    textTwo :{
        color : 'rgba(255, 255, 255, 1)',
        fontSize: 32,
    },
    textThree :{
        color : 'rgba(255, 255, 255, 0.8)',
        fontSize: 18,
    },
    textFour :{
        color : 'rgba(64, 70, 63, 1)',
        fontSize: 15,
    },
    inputField :{
        flex: 1,
        width: 270,
        height: 56,
        backgroundColor: 'rgba(255, 255, 255, 1)',
        borderRadius: 100,
        paddingHorizontal: 18,
        fontSize: 16,
    },
    inputContainer :{
        width: 370,
        height: 56,
        backgroundColor: 'rgba(255, 255, 255, 1)',
        borderRadius: 100,
        alignItems: 'center',
        justifyContent: 'center',
        flexDirection: 'row',
    },
    inputButton :{
        width: 370,
        height: 56,
        backgroundColor: 'rgba(255, 255, 255, 1)',
        borderRadius: 100,
        alignItems: 'center',
        justifyContent: 'center',
    }

});

export default SignupCreatePassword;

I am using these firebase docs
I am using these firebase docs

New contributor

Mian Abdulrehman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật