I am trying to build a React Native app and firebase authentication. I am getting the Error:
RecaptchaVerifier is only supported in a browser HTTP/HTTPS environment with DOM support
auth
.signInWithPhoneNumber(number, appVerifier)) // <- this is where captcha is added
.then((result) => {
this.setState({ confirm: result });
this.setState({ buttonText: "Verify OTP" });
this.setState({ isLoading: false });
})
.catch((err) => {
console.log(`Error in phone auth: ${err}`);
});
The version which I am using of firebase is "react-native-firebase": "^5.6.0"
When I remove second parameter it gives an error which states two parameter required, found one
Are you sure you are using react-native-firebase phone auth with 5.X.X version. You can skip the appverifier from the method and instead use the following method from react-native-firebase. It will automatically open a window to verify the app and recaptha.
firebase.auth().signInWithPhoneNumber(phoneNumber)
.then(confirmResult => // save confirm result to use with the manual verification code)
.catch(error => /error);
Related
I'm trying to fetch a list of all the users in my web app, but I keep receiving this error:
"TypeError: utils_firebase_WEBPACK_IMPORTED_MODULE_2_.auth.listUsers is not a function"
I copied exactly from Firebase documentation, and below is my code.
auth prints out "AuthImpl {app: FirebaseAppImpl, heartbeatServiceProvider: Provider, config: {…}, currentUser: null, emulatorConfig: null, …}", so I know that auth exists.
import { useEffect } from "react";
import { auth } from "../utils/firebase";
function users() {
const listAllUsers = (nextPageToken) => {
console.log(auth);
auth
.listUsers(1000, nextPageToken)
.then((listUsersResult) => {
listUsersResult.users.forEach((userRecord) => {
console.log("user", userRecord);
});
if (listUsersResult.pageToken) {
listAllUsers(listUsersResult.pageToken);
}
})
.catch((error) => {
console.log("Error listing users:", error);
});
};
useEffect(() => {
listAllUsers();
}, []);
return <div>users</div>;
}
export default users;
Can someone help me with this? Thanks!
I tried to look at the documentation from Firebase, but with no luck
listUsers() is a method from the Admin SDK and not from the JS SDK. The page you copied the code from documents the Admin SDK methods for the Authentication Service.
There isn't any corresponding method in the JS SDK because, for security reasons, it is not possible to let a user directly listing all users of a Firebase project from a front-end.
If you want to list all users of your Firebase project from your front-end you can write a Callable Cloud Function that uses the listUsers() Admin SDK's method. It's then up to you to verify who can call this Cloud Function.
I'm new to React native. Today I wanted to create a login and sign up page linked to firebase. I followed this tutorial -https://www.youtube.com/watch?v=TkuQAjnaSbM&t=110s - word for word and found it very successful.
I installed the expo application on my ios phone and I get the builds from there. The only difference between us and the tutorial in the video is that it uses an android emulator and I get build on the expo application on my real phone. But the problem is that I cannot add new users to the firebase via the script. I also share the code. I would appreciate if you help.
handleSignUp=() => {
firebase
.auth()
.createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(userCredentinals => {
return userCredentinals.user.updateProfile({
displayName: this.state.name
});
})
.catch(error => this.setState({ errorMessage: error.messsage}));
};
somehow I can't connect with firebase. but I'm sure I put the config code block in the right place. Maybe I am not authorized to write my own phone to the database I use as an emulator?
<TouchableOpacity style={styles.button}>
<Text style={{color:"#FFF", fontWeight: "500"}}
onPress={this.handleSignUp}
onPress={() => this.props.navigation.navigate("Home")}
>Kayıt Ol</Text>
</TouchableOpacity>
Please update your function like this.
const handleSignUp = () => {
console.log('Function is called');
firebase
.auth()
.createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(userCredentinals => {
console.log('Success');
return userCredentinals.user.updateProfile({
displayName: this.state.name
});
})
.catch(error => {
console.log(error);
this.setState({ errorMessage: error.messsage});
});
};
Then please update your question with what you get displayed in the console. Then I will be able to help you to solve this problem.
I want to detect whether the user has just registered via SSO or simply logged in.
The documentation wasn't helpful.
I'm using React + ES6. Here's my current auth method:
authWithGoogle = () => {
this.props.setIsLoggingInState(true);
firebaseApp
.auth()
.getRedirectResult()
.catch(() => console.error('something went wrong with Google SSO'));
firebaseApp
.auth()
.signInWithRedirect(googleProvider)
.catch(() => console.error('something went wrong with Google SSO'));
};
To be honest, the code above doesn't seem right... (but it works)
You can do the following:
firebaseApp.auth().getRedirectResult().then(function(userCredential) {
// True if the user is new, false if existing.
console.log(userCredential.additionalUserInfo.isNewUser);
}).catch(function(error) {
// Error occurred.
});
I am developing an application, with an feature of Google Login through Firebase. I am trying to login via Google with the help of an library, known as react-native-google-signin. It is well known library in the field of ReactNative for Google Login.
My problem is not with this library, but the problem is that while I am using react-native-google-signin library with firebase to login via google. Firebase User is not staying persistence, I mean to say that when I am opening app after close FirebaseUser is getting null. Below the code I am using to login via firebase,
GoogleSignin.signIn().then(data => {
const credentials = firebase.auth.GoogleAuthProvider.credential(data.idToken, data.accessToken);
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then() => {
return firebase.auth().signInWithCredential(credentials);
}).catch(error => {
console.log('Error', error);
})
}).then(user => {
console.log('user', firebase.auth().currentUser);
}).catch(error => {
console.log('Error', error);
})
I also checked Firebase Docs, tried by using setPersistence() method but still I am getting null user after open app again.
You can try this
when you first time open your app you get user null, but after login one time then reopen your app and you will get previously logged in user in your console
async _setupGoogleSignin() {
try {
await GoogleSignin.hasPlayServices({ autoResolve: true });
await GoogleSignin.configure({
webClientId: 'YOUR WEBCLIENTID',
offlineAccess: false
});
const user = await GoogleSignin.currentUserAsync();
console.log("user",user); // HERE YOU GET LOGGED IN USER IN YOUR CONSOLE FIRST TIME IT WILL BE NULL BUT AFTER YOU GET PREVIOUSLY LOGGED IN USER
this.setState({user});
}
catch(err) {
console.log("Play services error", err.code, err.message);
}}
then
_signIn() {
GoogleSignin.signIn()
.then((user) => {
console.log(user);
this.setState({user: user});
const credential = firebase.auth.GoogleAuthProvider.credential(user.idToken, user.accessToken);
// console.log(credential);
return firebase.auth().signInAndRetrieveDataWithCredential(credential);
})
.catch((err) => {
console.log('WRONG SIGNIN', err);
})
.done();}
it is worked for me...
hope it will help you...
I'm using AngularFire2 (2.0.0-beta.2) incombination with angular2 (2.0.0-rc.4). Using Angularfire I can programatically create a user (email/password) with
angularFire.auth.createUser({email : this.email, password; this.password})
That part works as expected. Subsequently, I would like to update either the email address or password. I've examined the AngularFire source and there doesn't seem to be a mechanism to do this. Am I correct in this assessment? And if I'm correct, should I expect to see a mechanism in upcoming releases or should I just use the native Firebase mechanisms?
You are looking to use $firebaseAuth(). Just inject it in your controller and go with
$firebaseAuth().$updateEmail("email#email.com");
$firebaseAuth().$updatePassword("newpass123");
I think my answer will be helpful, Password can be changed by firebase-admin using cloud function you will just have to pass email and new password from the client-side(Angular, ionic, ...)
Cloud function:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const cors = require('cors')({ origin: true });
export const resetUserPassword = functions.https.onRequest( async (req, res) => {
return cors( req, res, async () => {
const email = req.body.data.email;
const password = req.body.data.password;
admin.auth().getUserByEmail(email)
.then((getUserRecord) => {
admin.auth().updateUser(getUserRecord.uid, {
email,
password
})
.then(function(userRecord) {
res.send({
status: 200,
data: userRecord
})
})
.catch(function(error) {
res.send({
status: 500,
data: error
})
});
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
})
});
Client side code(IONIC):
import { AngularFireFunctions } from '#angular/fire/functions';
export class AuthService {
constructor(
private functions: AngularFireFunctions
) {}
resetUserPassword() {
const userNewCredential = {email: 'user-email', password: 'your-new-password'};
this.functions.httpsCallable('resetUserPassword')
(userNewCredential).toPromise().then((updatedUser) => {
console.log(updatedUser);
}).catch((error) => console.log(error));
}
I'm going to try an answer my own question. I think that the AngularFire2 public API's are missing some functionality related to firebase authentication. For example I don't think the present version of AngularFire (2.0.0-beta2) has the ability to update the email address or password, or send the password reset email. I think the solution to this present shortcoming is to get the native firebase objet and just use the native firebase methods to resolve. However, I haven't been able to figure out how to get access to the native FireBase object using AngularFire2. I've posted this question to see if anyone can help me do that.