I am using firebase
phone auth in react native app with this docs, I tested it in some different devices. sometimes the phone auth
works and sometimes it throws this error
firebase phone auth Error: Invalid token. at nativeToJSError
I have gone through the docs of firebase and try to understand this error. Here is my code snippet:
confirmPhone = async(phoneNumber) => {
const phoneWithAreaCode = phoneNumber.replace(/^0+/, '+972');
return new Promise((res, rej) => {
firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
.on('state_changed', async(phoneAuthSnapshot) => {
switch (phoneAuthSnapshot.state) {
case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
res(phoneAuthSnapshot)
break
case firebase.auth.PhoneAuthState.CODE_SENT:
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
res(phoneAuthSnapshot)
break
case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT:
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
UserStore.setErrorCodeAuthentication('SMS code has expired')
res(phoneAuthSnapshot)
case firebase.auth.PhoneAuthState.ERROR:
console.log(phoneAuthSnapshot.error.code)
if (phoneAuthSnapshot.error) {
this.showMessageErrorByCode(phoneAuthSnapshot.error.code)
}
rej(phoneAuthSnapshot)
break
}
})
})
}
confirmCode = async(verificationId, code, phoneAuthSnapshot) => {
console.log(verificationId, code);
try {
const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code)
UserStore.setCodeInput(code)
UserStore.setUserCredentials(credential)
AppStore.setAlreadyVerifiedAuto(true)
await this.authenticate(credential)
return credential
} catch (e) {
console.log(e)
this.showMessageErrorByCode(e.error.code)
// throw new Error(e)
}
}
showMessageErrorByCode(errorcode) {
switch (errorcode) {
case 'auth/invalid-phone-number':
UserStore.setErrorCodeAuthentication('Please enter valid phone number');
break;
case 'auth/unknown':
{
UserStore.setErrorCodeAuthentication('User blocked by firebase');
break;
}
case 'auth/session-expired':
{
UserStore.setErrorCodeAuthentication('SMS code has expired');
break;
}
case 'auth/invalid-verification-code':
{
UserStore.setErrorCodeAuthentication('Code verification not correct');
break;
}
default:
UserStore.setErrorCodeAuthentication('other error');
}
}
authenticate = async(credential) => {
await firebase.auth().signInAndRetrieveDataWithCredential(credential)
}
//...
I am using firebase
phone auth in react native app with this docs, I tested it in some different devices. sometimes the phone auth
works and sometimes it throws this error
firebase phone auth Error: Invalid token. at nativeToJSError
I have gone through the docs of firebase and try to understand this error. Here is my code snippet:
confirmPhone = async(phoneNumber) => {
const phoneWithAreaCode = phoneNumber.replace(/^0+/, '+972');
return new Promise((res, rej) => {
firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
.on('state_changed', async(phoneAuthSnapshot) => {
switch (phoneAuthSnapshot.state) {
case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
res(phoneAuthSnapshot)
break
case firebase.auth.PhoneAuthState.CODE_SENT:
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
res(phoneAuthSnapshot)
break
case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT:
UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
UserStore.setErrorCodeAuthentication('SMS code has expired')
res(phoneAuthSnapshot)
case firebase.auth.PhoneAuthState.ERROR:
console.log(phoneAuthSnapshot.error.code)
if (phoneAuthSnapshot.error) {
this.showMessageErrorByCode(phoneAuthSnapshot.error.code)
}
rej(phoneAuthSnapshot)
break
}
})
})
}
confirmCode = async(verificationId, code, phoneAuthSnapshot) => {
console.log(verificationId, code);
try {
const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code)
UserStore.setCodeInput(code)
UserStore.setUserCredentials(credential)
AppStore.setAlreadyVerifiedAuto(true)
await this.authenticate(credential)
return credential
} catch (e) {
console.log(e)
this.showMessageErrorByCode(e.error.code)
// throw new Error(e)
}
}
showMessageErrorByCode(errorcode) {
switch (errorcode) {
case 'auth/invalid-phone-number':
UserStore.setErrorCodeAuthentication('Please enter valid phone number');
break;
case 'auth/unknown':
{
UserStore.setErrorCodeAuthentication('User blocked by firebase');
break;
}
case 'auth/session-expired':
{
UserStore.setErrorCodeAuthentication('SMS code has expired');
break;
}
case 'auth/invalid-verification-code':
{
UserStore.setErrorCodeAuthentication('Code verification not correct');
break;
}
default:
UserStore.setErrorCodeAuthentication('other error');
}
}
authenticate = async(credential) => {
await firebase.auth().signInAndRetrieveDataWithCredential(credential)
}
//...
Share
Improve this question
edited May 15, 2020 at 12:18
SMAKSS
10.5k3 gold badges24 silver badges37 bronze badges
asked May 17, 2018 at 11:39
ManspofManspof
35727 gold badges94 silver badges182 bronze badges
4
- 3 Have you had a look at this: stackoverflow./questions/45091583/… – Vasile Florin Vilsan Commented May 29, 2018 at 9:37
- Did you ever resolve this issue? Seeing the exact same error. – bmovement Commented Nov 1, 2018 at 0:50
- check this - stackoverflow./a/54800057/4295614 – Arun Kumar P Commented Feb 21, 2019 at 5:54
- 2 Possible duplicate of "Invalid Token" when trying to authenticate phone number using firebase – oktapodia Commented Oct 29, 2019 at 0:00
2 Answers
Reset to default 2Check the following:
- Correct bundle Id
- Correct Google-Info.plist
- Correct aps-environment value
- Upload the APNS authentication key (p8) instead of certificates
Hope you find it helpful.
I think this can happen if the bundle id of the app trying to access firebase doesn't match the bundle id registered in the firebase console.