Reputation: 28
I am using Firebase tools like Authentication, Functions & Firestore etc. I’ve made an application with React Native, which has a login. Now I want to implement some logic to obligate the user to change their password. The auth of the user is linked with an user document in Firestore through their uid.
I’ve tried to create a Boolean in the user document. The Boolean called changePassword. When I want user to change their password underwater I change the password to a random string and set this Boolean to true. When the user is logged in their password got changed he automatically logs out. When trying to login he user get a message/snackbar asking if they want to change their password with a button to send the mail to change it.
This all works fine, but now I wanted to make a function in my firebase functions to check if the password got changed. So I could set the Boolean changePassword to false.
What are best practices to because the documentations says the auth only can be created and deleted. https://firebase.google.com/docs/auth/extend-with-functions
Main.tsx
if (user && user.emailVerified) {
const doc = await firestore.collection("users").doc(user.uid).get();
User.setCurrentUser(doc);
setAuthenticated(true);
…
} else {
setAuthenticated(false);
}
Login.tsx
const loginPress = async () => {
const query = firebase.firestore().collection("users")
.where('email', ' == ', email.toLowerCase())
const snapshot = await query.get();
const formatted = snapshot.docs.map((doc: any) => doc.data());
const user = formatted[0];
if (user?.changePassword) {
props.navigation.navigate('ForgotPassword', { email: email, changePassword: true });
} else {
firebase.auth().signInWithEmailAndPassword(email, password).then((result) => {
...
}).catch ((error) => {
let errorMessage = error.message;
...
});
}
Keyboard.dismiss();
}
Triggers/onUpdate.ts
In the functions I wanted something like beneath, but it is not possible:
exports.passwordGotChanged = functions.auth.user().onUpdate((snapshot: any) => {
const before = snapshot.before.data();
const after = snapshot.after.data();
if (JSON.stringify(before.password) !== JSON.stringify(after.password)) {
return functions.firestore.document('/users/{after.uid}').set({ changePassword: false }, { merge: true });
} else {
return null
}
});
Maybe this is a duplicate of How to trigger a cloud function in Firebase on password change? but the provided solution won’t work for me and maybe there are new solutions
Upvotes: 0
Views: 443
Reputation: 317712
It's not possible to trigger a function automatically when the user's profile is updated or password is changed. There is no onUpdate trigger.
If you want to do something on your backend when the client updates their profile or changes their password, your client app will have to invoke some sort of endpoint directly. If you're using Cloud Function, you typically do this with an HTTP or callable function. There are no provided solutions - this is something you'll have to build for yourself to meet your specific use case.
Upvotes: 2