与许多开发人员一样,我在本周公布之后将我的后端迁移到Parse,我无法弄清楚如何检查当前是否有用户登录。我正在使用电子邮件和密码验证。
这是我的代码不起作用:
public class ApplicationStart extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
Firebase ref = new Firebase(Passwords.FB_LINK);
AuthData currentUser = ref.getAuth();
if (currentUser == null) {
// Take user to log in screen
Intent intent = new Intent(this, MyIntro.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else {
// User already logged in
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
TwitterAuthConfig authConfig = new TwitterAuthConfig(Passwords.TWITTER_KEY, Passwords.TWITTER_SECRET);
Fabric.with(this, new TwitterCore(authConfig), new Digits());
}
}
答案 0 :(得分:2)
所以我在发布这个问题后不久就设法解决了这个问题。这是有效的代码。我只需要换一行:
Firebase ref = new Firebase(Passwords.FB_LINK).child("users");
完整代码:
public class ApplicationStart extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
Firebase ref = new Firebase(Passwords.FB_LINK).child("users");
AuthData currentUser = ref.getAuth();
if (currentUser == null) {
// Take user to log in screen
Intent intent = new Intent(this, MyIntro.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else {
// User already logged in
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
TwitterAuthConfig authConfig = new TwitterAuthConfig(Passwords.TWITTER_KEY, Passwords.TWITTER_SECRET);
Fabric.with(this, new TwitterCore(authConfig), new Digits());
}
}
答案 1 :(得分:1)
声明一个实例(类变量)
private FirebaseAuth mAuth;
在onCreate()
初始化mAuth
mAuth = FirebaseAuth.getInstance();
在onStart()
中检查用户是否已登录并检查null
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
// No user is signed in
} else {
// User logged in
}
}
这可以通过一个班轮完成:
if(FirebaseAuth.getInstance().getCurrentUser()) {
//User is logged in
}
答案 2 :(得分:0)
private FirebaseAuth firebaseAuth;
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, UserActivty.class));
finish();
}
选中此项,如果您已经登录,它将直接将您发送到主页。