在学习后,在最新的firebase中进行了发送电子邮件验证,尽管缺少文档,但我想亲自对其进行测试。
使用下面的摘要:
Auth.$onAuthStateChanged(function(firebaseUser) {
if (firebaseUser) {
console.log(firebaseUser);
if (firebaseUser.emailVerified) {
// console.log(firebaseUser.emailVerified);
toastr.success('Email verified');
} else {
toastr.info('Do verify email');
}
}
})
console.log(firebaseUser.emailVerified)
返回false,始终,尽管已启动了发送验证,收到电子邮件并单击。
通过电子邮件登录后,我检查是否已验证用户,如果没有验证,则应发送电子邮件:
Auth.$signInWithEmailAndPassword(email, password)
.then(function(firebaseUser) {
if (!firebaseUser.emailVerified) {
firebaseUser.sendEmailVerification();
console.log('Email verification sent');
}
$state.go('home');
})
在我的 https://console.firebase.google.com/project/my-app-name/authentication/emails
下,一切默认情况下是:
Follow this link to verify your email address.
https://my-app-name.firebaseapp.com/__/auth/handler?mode=<action>&oobCode=<code>
我用来注册的电子邮件会收到验证电子邮件,但是,单击链接无助于将user.emailVerified
更改为true。
这是所有步骤都概述了,还是文档中找不到的另一步?
如Tope在评论中所述,您需要进行firebaseUser.reload()
,以便更改Firebaseuser的身份验证状态。
我只是采取了相同的步骤:
auth.currentUser.emailVerified
false
auth.currentUser.sendEmailVerification()
- 等待电子邮件
- 单击电子邮件中的链接
- 签字
- 再次登录
,然后
auth.currentUser.emailVerified
true
注意步骤3和4:我需要再次登录,在我的应用中可见emailVerified
的新值之前。
这个答案似乎也很重要,但是我只在编写上述内容后才发现它:未发送firebase确认电子邮件
以非常简约的方式,这就是我最终得到的:
angular.module('theApp')
.controller('emailVerifyController', ['$scope', '$stateParams', 'currentAuth', 'DatabaseRef',
function($scope, $stateParams, currentAuth, DatabaseRef) {
// where currentAuth and DatabaseRef is what they sound like
$scope.doVerify = function() {
firebase.auth()
.applyActionCode($stateParams.oobCode)
.then(function(data) {
// change emailVerified for logged in User
// you can update a DatabaseRef endpoint in here
// whatever!
toastr.success('Verification happened', 'Success!');
})
.catch(function(error) {
$scope.error = error.message;
toastr.error(error.message, error.reason, { timeOut: 0 });
})
};
}
])
然后在模板中,类似的东西:
<a ng-click="doVerify()" class="button large">Verify my Account</a>
尽管applyActionCode
尚未包装为Angularfire,但您仍然可以掉落到Angularjs中SDK的Vanilla Javascript,此外,为什么不!
我在firebase中共享更多详细信息以进行电子邮件验证:
https://blog.khophi.co/email-verification-firebase-3-0-sdk/
我们刚刚经历了同一问题,发现解决方案是使用user.reload()
功能重新加载用户,并且还将观察者从onAuthStateChanged
更改为onIdTokenChanged
。
这是因为您正在使用新的emailVerifed
属性更新Firebase令牌,并且onIdTokenChanged
正在侦听对ID令牌的更改,并使用正确的值更新用户对象。
代码片段:
export function* register_user(action) {
try {
//Call api which registers user and sets email verified to true
let register_result = yield call(Register_API.create_registration, action.data)
if (register_result && register_result.status >= 200 && register_result.status < 300) {
let user = firebase.auth().currentUser
//Force user to reload so we can trigger the onIdTokenChanged listener
return user.reload()
}
} catch (e) {
console.error(e)
}
}
firebase.auth().onIdTokenChanged(function (user) {
if (user && user.uid) {
if (user.emailVerified) {
//Stuff you want to do
}
}
})
在我的情况下,currentUser.emailVerified
似乎切换到true
,但并非总是如此。不知道为什么。在大多数情况下,它将切换到带有铬的true
,但并非总是如此。它不与firefox。
应用reload()
方法似乎可以解决该问题。
i在加载包含OobCode的URL之后,在传奇中具有以下内容:
const currentUser = firebase.auth().currentUser;
if(currentUser){
currentUser.reload();
}
我们只需要在用户单击用户确认链接后重新加载用户的状态即可。尤其是如果有人使用Firebase UI登录流程,可能会注意到用户注册和登录同时执行。对于这种情况,需要通过登录登录对用户进行重新验证,这不是一个令人愉快的体验。但是,Firebase提供了重新加载用户的选项。将其放入异步 - wait函数将确保在执行下一个命令之前对状态进行刷新。
async function confirmEmailVerified(){
await auth.currentUser.reload();
var isEmailVerified = auth.currentUser.emailVerified;
if (isEmailVerified) {
console.log("Email verified");
}else{
console.log("Email not verified, please verify or click on resend to get the verification email!");
}
}