我正在使用Firebase SimpleLogin来启用电子邮件/密码验证。创建用户和后续登录都正常工作。但是,无论我离开应用程序多久(即使只有几秒钟),当我返回时用户从未登录,例如...
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...
始终返回一个空用户。
我没有通过API注销用户。同时我在Firebase控制台中将用户登录天数设置为21天。
我看到JS文档中提到了一个记住我参数,但是我在Android/Java中找不到等效的内容。
想知道我是否在文档中遗漏了什么或者对于Android来说这是不可能的?
谢谢您的帮助,Neil。
编辑:添加了代码示例。
用户创建......
public void registerUserForChat(final MyApplication application, String email, String password) {
Firebase ref = new Firebase(FIREBASE_URL);
SimpleLogin authClient = new SimpleLogin(ref);
authClient.createUser(email, password, new SimpleLoginAuthenticatedHandler() {
@Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
Log.e(TAG, "Error attempting to create new Firebase User: " + error);
}
else {
Log.d(TAG, "User successfully registered for Firebase");
application.setLoggedIntoChat(true);
}
}
});
}
用户登录....
public void loginUserForChat(final MyApplication application, String email, String password) {
Log.d(TAG, "Attempting to login Firebase user...");
Firebase ref = new Firebase(FirebaseService.FIREBASE_URL);
final SimpleLogin authClient = new SimpleLogin(ref);
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler() {
@Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if (error != null) {
Log.d(TAG, "error performing check: " + error);
} else if (user == null) {
Log.d(TAG, "no user logged in. Will login...");
authClient.loginWithEmail(email, password, new SimpleLoginAuthenticatedHandler() {
@Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
if(com.firebase.simplelogin.enums.Error.UserDoesNotExist == error) {
Log.e(TAG, "UserDoesNotExist!");
} else {
Log.e(TAG, "Error attempting to login Firebase User: " + error);
}
}
else {
Log.d(TAG, "User successfully logged into Firebase");
application.setLoggedIntoChat(true);
}
}
});
} else {
Log.d(TAG, "user is logged in");
}
}
});
}
因此,loginUserForChat方法首先检查是否有已登录的用户,如果没有,则执行登录。请注意,每次我启动应用程序时,我看到的日志是....
- 尝试登录 Firebase 用户...
- 未登录用户。将进行登录...
- 用户成功登录 Firebase
如果我退出应用程序,即使只是几秒钟,然后返回 - 我会看到相同的日志。
我注意到的一件事是,对于checkAuthStatus的调用不需要任何用户凭据 - 我假设它只是检查是否有任何本地已登录的用户?
非常感谢。
SimpleLogin(com.firebase.client.Firebase ref, android.content.Context context)
。我有一种预感,这将解决您的问题。查看new SimpleLogin(),需要上下文来自动持久化/加载会话,并且我们应该在示例中显示这一点。 - Rob DiMarco