我正在尝试在我的scala-js代码中编写一个javascriptfaçade,以利用Google的Firebase身份验证JavaScript框架。在对Firebase进行身份验证时(初始化之后),将获得Firebase用户或带有描述问题的字符串错误代码的异常。具体来说,我正在尝试使用https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword中所述的电子邮件和密码登录。提供正确的电子邮件和密码会向我返回一个有效的Firebase用户,我会像这样“使用façade”:
@JSName("firebase.User")
@js.native
trait FirebaseUser extends FirebaseUserInfo {
var emailVerified : Boolean = js.native
var isAnonynous: Boolean = js.native
var providerData: js.Object = js.native
var refreshToken: String = js.native
}
object FirebaseUser {
def apply(emailVerified : Boolean, isAnonynous: Boolean, providerData: js.Object, refreshToken: String): FirebaseUser = {
js.Dynamic.literal(emailVerified = emailVerified, isAnonynous = isAnonynous, providerData = providerData, refreshToken = refreshToken).asInstanceOf[FirebaseUser]
}
}
包含'signInWithEmailAndPassword'方法的Authfaçade如下:
@JSName("firebase.auth.Auth")
@js.native
trait FirebaseAuth extends js.Object {
var app: FirebaseApp = js.native
var currentUser: FirebaseUser = js.native
def signInWithEmailAndPassword(email: String, password: String): Promise[FirebaseUser] = js.native
def signOut: Promise[Void] = js.native
def onAuthStateChanged(nextOrObserver: FirebaseUser => Unit, opt_error: FirebaseAuthError => Unit = null, opt_completed: Unit => Unit = null)
}
object FirebaseAuth {
def apply(app: FirebaseApp, currentUser: FirebaseUser): FirebaseAuth = {
js.Dynamic.literal(app = app, currentUser = currentUser).asInstanceOf[FirebaseAuth]
}
}
我调用它的代码如下所示:
// since 'signInWithEmailAndPassword()' returns promise, convert to future
val f1 = firebase.auth().signInWithEmailAndPassword(S.user.email, S.user.password).toFuture
f1 onComplete {
case Success(fbu) => {
fbuser = fbu
scope.modState(_.copy(loginState = isLoggedIn)).runNow()
}
case Failure(ex) => println("Error signing-in to firebase, error="+ex.getMessage)
case _ => println("Something else happened when signing-in...")
}
成功案例工作正常,但如果预期null
包含错误原因,则失败案例会返回String
。我究竟做错了什么?我怀疑它与Firebase有自己的Firebase.promise类型和使用内置的scalajs scala.scalajs.js.Promise类型这一事实有关 - 我不得不为Firebase创建一个外观。承诺类型。
我正在使用sbt版本0.13.8和scala-js插件版本0.6.7