【发布时间】:2020-12-22 18:26:37
【问题描述】:
问题
我正在使用 Firebase 工具,例如 Authentication、Functions 和 Firestore 等。我使用 React Native 制作了一个应用程序,该应用程序具有登录功能。现在我想实现一些逻辑来强制用户更改密码。用户的身份验证通过其 uid 与 Firestore 中的用户文档相关联。
我尝试在用户文档中创建一个布尔值。布尔值称为changePassword。当我希望用户在水下更改密码时,我将密码更改为随机字符串并将此布尔值设置为 true。当用户登录时,他们的密码已更改,他会自动注销。尝试登录时,用户会收到一条消息/小吃栏,询问他们是否要更改密码,并使用按钮发送邮件以更改密码。
这一切都很好,但现在我想在我的 firebase 函数中创建一个函数来检查密码是否被更改。所以我可以将 Boolean changePassword 设置为 false。
什么是最佳实践,因为文档说只能创建和删除身份验证。 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();
}
Firebase 函数
触发器/onUpdate.ts
在函数中我想要类似下面的东西,但这是不可能的:
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
}
});
附言
也许这是 How to trigger a cloud function in Firebase on password change? 的副本,但提供的解决方案对我不起作用,也许有新的解决方案
【问题讨论】:
标签: firebase react-native google-cloud-firestore firebase-authentication google-cloud-functions