javascript - Firebase auth async await - Stack Overflow

I'm new to using async await and I'm trying a Auth createUserWithEmailAndPassword in firebase

I'm new to using async await and I'm trying a Auth createUserWithEmailAndPassword in firebase.

signUp

exports.signup = async (req, res) => {
    const { email, password, confirmPassword, handle } = req.body

    const newUser = {
        email,
        password,
        confirmPassword,
        handle
    }

    try {
        const response = await firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password)
        const token = response.getIdToken()
        console.log('THIS IS THE RESPONSE', token)
        // return token
        return res.status(200).json({
            message: 'User Successfully Added!',
            token: token
        })

    } catch (err) {
        if (err.code === 'auth/email-already-in-use') {
            return res.status(400).json({
                message: 'Email already taken!'
            })
        } else {
            return res.status(500).json({
                message: 'Something went wrong, Please try again later'
            })
        }
    }
}

My problem is this is actually creating an account but always returning a status of 500 Something went wrong, Please try again later

EDIT:

console.log(err) gives the following output:

TypeError: response.getIdToken is not a function

I'll try to look into it.

I'm new to using async await and I'm trying a Auth createUserWithEmailAndPassword in firebase.

signUp

exports.signup = async (req, res) => {
    const { email, password, confirmPassword, handle } = req.body

    const newUser = {
        email,
        password,
        confirmPassword,
        handle
    }

    try {
        const response = await firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password)
        const token = response.getIdToken()
        console.log('THIS IS THE RESPONSE', token)
        // return token
        return res.status(200).json({
            message: 'User Successfully Added!',
            token: token
        })

    } catch (err) {
        if (err.code === 'auth/email-already-in-use') {
            return res.status(400).json({
                message: 'Email already taken!'
            })
        } else {
            return res.status(500).json({
                message: 'Something went wrong, Please try again later'
            })
        }
    }
}

My problem is this is actually creating an account but always returning a status of 500 Something went wrong, Please try again later

EDIT:

console.log(err) gives the following output:

TypeError: response.getIdToken is not a function

I'll try to look into it.

Share Improve this question edited Dec 25, 2022 at 2:04 Melchia 24.4k23 gold badges108 silver badges129 bronze badges asked Nov 11, 2019 at 0:44 code.cyclingcode.cycling 1,2742 gold badges16 silver badges33 bronze badges 3
  • So what's exactly in err? – zerkms Commented Nov 11, 2019 at 0:46
  • @zerkms I edited my question it looks like my getIdToken() is throwing an error – code.cycling Commented Nov 11, 2019 at 0:49
  • It's not getIdToken throws, but invoking response.getIdToken() which is not a function. Where is response.getIdToken defined? – zerkms Commented Nov 11, 2019 at 0:52
Add a ment  | 

1 Answer 1

Reset to default 6

createUserWithEmailAndPassword returns Promise< UserCredential > And getIdToken is a method of user (Documentation)

const response = await firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password);
const token = await response.user.getIdToken(); // getIdToken is a method of user
console.log('THIS IS THE RESPONSE', token);
// return token
return res.status(200).json({
    message: 'User Successfully Added!',
    token: token
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744220208a4563745.html

相关推荐

  • javascript - Firebase auth async await - Stack Overflow

    I'm new to using async await and I'm trying a Auth createUserWithEmailAndPassword in firebase

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信