使用Firebase + Android设计登录/注册逻辑的问题 [英] Having Problems Designing Sign-In / Sign-Up Logic with Firebase + Android

查看:179
本文介绍了使用Firebase + Android设计登录/注册逻辑的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢Android /编程...我正在设计一个使用Firebase作为后台的应用程序(尽管我所遇到的任何应用程序会出现这样的问题,这些应用程序会尝试立即根据远程设备进行决策数据)。



我觉得有更聪明的方式来做我想做的事。



要求是(我正在考虑问题的方式):




  • 设计新用户登录并注册(两个单独的东西)的应用程序,但应用程序不会允许他们正确使用该应用程序,除非它们已经通过Firebase数据库控制台被人员授权(手动)。现有/认可的用户将绕过这些阶段。



所以...为了解决这个问题,我以为:


  1. 当他们第一次打开应用程序时,如果他们没有登录,请提供一个登录活动。


  2. 如果成功登录后发现他们尚未注册,则提交注册活动。


  3. 如果在成功注册后,发现它们尚未被管理员批准,然后退出应用程序并提醒一个对话框,解释他们必须等待更长时间才能批准。


听起来很简单,所以我们可以像这样一个MainActivity onResume()方法:

  @Override 
protected void onResume(){
super.onResume();

//如果没有登录,请转到signIn /创建帐户活动

//(我们登录或从signin.java返回)

//如果没有登录/注册,那么我们去登录/注册活动

//(我们注册或从register.java返回)

//是用户管理员批准的吗?否则,我们退出。


}

但是当我们尝试实施这些支票。



如何检查用户是否已经注册?我想,我们可以直接查询Firebase数据库,例如:




  • 已经注册了,我们检查了Firebase DB的JSON树中的一个分支(key),该树分支符合已登录的userID,然后我们知道他们已经注册(因为注册活动已经创建了这个记录)


  • 要检查是否经过管理员批准,我们将检查一个字段,例如:

     '\firebase\user_id\isAllowed'= true。 




默认情况下,该字段将设置为false并且只有当管理员手动设置数据库中的值时,才设置为true。



但是这些远程检查将需要时间才能执行...并且结果被传递通过回调方法...所以我们如何暂停onresume方法,以便它等待结果继续之前? (我们可以在回调中嵌套回调: Firstcheck.onsuccess(){[Secondcheck.onsuccess(){[ThirdCheck.onsuccess(){// SUCCESS!}]}}} ...但这似乎是一个坏主意。



好的...所以为什么不使用共享首选项,而且注册活动保存一个user_progress变量如1,这意味着他们已经完成了注册?这样,用户的进度将被存储在本地,并且可以在 onresume()逻辑中立即检查。



但是,这会创建一个新的问题,那就是用户如何重新安装应用程序呢?然后共享的首选项被擦除,现在它们会相信他们没有注册...并且错误地向他们提供注册活动。



我有个别的解决方案来解决这些个人问题,但是老鼠难以遵循代码的结果是什么。 ..



有没有人遇到过这样的问题?我在想什么我错了?我觉得有一个根本不正确的方式,我正在接近它...像程序对象面向对象...我想知道是否有人可以解决。

解决方案

嗯,因为没有人回答,我还在努力,我最好的解决方案是:



在SignINActivity中,当用户成功登录时,我将从共享首选项中的Firebase数据库中保存其datanapshot(配置文件)的副本。这样,当他们返回到MainActivity中的onResume()时,它可以使用此快照来确定所有需要的条件,而不涉及回调。接下来,在signUPActivity中,我将在完成本地快照时更新。



我已经用一个新的启动器类ActionDecider替换了MainActivity。 >

ActionDecider.onResume():

  @Override 
protected void onResume(){
super.onResume();

// TODO:做安全网检查这里


//(4)条件:如何告诉:做什么
//
// 1)未登录:auth == null:load SignINActivity
// 2)已登录但未注册:snapshot == null:load SignUPActivity
// 3)注册,但未批准:/ userid / isAllowed == false:退出对话框
// 4)注册并获得批准:/ userid / isAllowed == true:继续正常



//如果没有登录,请转到登录/创建帐户活动

//用户被发送到登录他们登录(或创建帐户)的用户,并保存
//它的数据库树的数据集合,以便在返回这里之前共享prefs


//(用户登录)

// CHECK:是否被用户批准? (检查快照 - 这是先检查,因为在
//大多数情况下,他们将被批准,这避免了不必要的额外检查)

//是? [exit]并加载mainActivity。 []

//否? CHECK:是用户注册了吗?

//否?加载signUP-Activity(登录活动时更新数据完成时的数据)

//是? [退出]对话框

}

最佳解决方案?我对此表示怀疑。但是,随时让我知道一个更好的方法。


I'm relatively new with Android/programming... I'm trying to design an app that uses Firebase as the backend (although the problems I am having would exist for any app that tries to make immediate decisions based on remote data).

I feel like there is a smarter way to do what I'm trying to do.

The requirements are (the way I'm thinking about problem):

  • Design an app where new users sign in and register (two separate things), but the app will not allow them to use the app properly unless they have been expicitly (manually) authorized by a human via the Firebase database console. Existing/approved users will bypass these stages.

So... to solve this, I have thought:

  1. When they first open the app, if they are not signed in, present them with a SignIn Activity.

  2. If, upon successful sign-in, it is found they have not yet registered, then present a registration Activity.

  3. If upon successful registration, it is found that they are not yet admin-approved, then exit the app with an alert dialog explaining they must wait longer for approval.

Sounds simple... so we could just have a MainActivity onResume() method like this:

@Override
protected void onResume(){
    super.onResume();

    // if not signed in, go to signIn/Create account activity

    // (either we are signed in or we return from signin.java)

    // if not signedUP/registered, then we go to signUP/register activity

    // (either we are signed up or we return from register.java)

    //  is user admin-approved?  if not, then we exit.


}

But the problems arise when we try to implement these "checks."

How do you check whether a user has already registered? I thought, well, we can just query the Firebase database... for example:

  • To find out if a user has already registered, we check for a branch (key) in the JSON tree of the Firebase DB that matches the signed in userID, then we know they have registered (because the registration activity would have created this record already)

  • To check whether they are admin-approved, we would check for a field such as:

    '\firebase\user_id\isAllowed' = true. 
    

This field would be set to false by default and set to true only if an admin manually sets the value in the database.

But these sort of remote checks will take time to execute... and the results are delivered via callback methods... so how can we "pause" the onresume method so that it waits for the result before continuing?? (we could 'nest' callbacks within callbacks: Firstcheck.onsuccess() { [Secondcheck.onsuccess() { [Thirdcheck.onsuccess() { // SUCCESS! }] } ] } ... but this seems like a bad idea.

Alright... so why not use shared preferences instead, and have the registration activity save a "user_progress" variable such as 1, meaning that they have completed the registration? This way the user's progress would be stored locally and could be immediately checked in the onresume() logic.

But then this creates a new problem... that is what if the user re-installs the app? then the shared preferences are wiped and it would now believe that they are not registered... and mistakenly present them with the registration activity again.

I have individual solutions for these individual problems, but what results is a rat's nest of hard to follow code...

Has anyone else been stuck with this kind of problem? Where am I going wrong in my thinking? I feel like there is a fundamentally incorrect way that I'm approaching it... like procedural vs object oriented... and I'm wondering if anyone can address that.

解决方案

Well... since nobody has answered yet, I've still been working on this, and my best solution is:

In the SignINActivity, when the user is successfully signed in, I will save a copy of their datasnapshot (profile) from the Firebase database in Shared Preferences. That way, when they return to the onResume() in MainActivity, it can use this snapshot to determine all the conditions I need without callbacks being involved. Subsequently, in signUPActivity, I will update the local snapshot when that is completed, also.

I have replaced 'MainActivity' with a new launcher class, ActionDecider.

ActionDecider.onResume() :

@Override
protected void onResume(){
    super.onResume();

    // TODO: do safetynet check here


    // (4) CONDITIONS :  HOW TO TELL :  WHAT TO DO
    // 
    // 1)not signed in : auth == null : load SignINActivity
    // 2)signed in, but not registered : snapshot == null : load SignUPActivity
    // 3)registered, but not approved : /userid/isAllowed == false : exit with dialog
    // 4)registered, and approved : /userid/isAllowed == true : continue as normal



    // if not signed in, go to signIn/Create account activity

        // user is sent to signIN which signs them in (or creates account), and saves a
        // datasnapshot of their db tree to shared prefs before returning here


    // (user is signed in)

    // CHECK: is user approved? (check the snapshot -- this is checked first because in 
    // most cases they will be approved, and this avoids an unnecessary extra check)

        // yes?  [exit] and load mainActivity. []

        // no?  CHECK:  is user signed up?

                // no?  load signUP-Activity   (signUP activity UPDATES datasnapshot when complete)

                // yes?  [exit] with dialog 

}

Best solution? I doubt it. But... feel free to let me know a better approach.

这篇关于使用Firebase + Android设计登录/注册逻辑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆