devOP1
devOP1

Reputation: 477

How to check if user's email is verified?

I am making an application where the user can sign in whether their email is verified or not. However, if their email is not verified, they will only have access to one page and if it is verified they will have access to the entire application (AppView).

So far with the code I have, I am able to create a user and sign them in, giving them access to the full application. But I'm not sure how to show the other view if their email is not verified.

Here is my code for checking if a user is signed in:

func listen () {
    // monitor authentication changes using firebase
    handle = Auth.auth().addStateDidChangeListener { (auth, user) in
        if let user = user {
            // if we have a user, create a new user model
            print("Got user: \(user)")
            self.isLoggedIn = true
            self.session = User(
                uid: user.uid,
                displayName: user.displayName, email: "")
        } else {
            // if we don't have a user, set our session to nil
            self.isLoggedIn = false
            self.session = nil
        }
    }
}

And here is where I choose which view it should show:

struct ContentView: View {


@EnvironmentObject var session: SessionStore

func getUser () {
      session.listen()
  }

var body: some View {
    
    Group {
      
        if (session.session != nil) {
        
        // I need to check here if the user is verified or not
        
        AppView() //full application view
        
      } else {
        
        OnBoardingView()
        
      }
    }.onAppear(perform: getUser)
    
  }

}

Upvotes: 2

Views: 1069

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You can see if the user is verified by checking the User.isEmailVerified property. The main problem you may encounter is that this property is not immediately refreshed after you click the link in the email, as the iOS has no way of knowing that you clicked the link.

The client will automatically pick up the new value for isEmailVerified when it refreshes the token, which automatically happens every hour, when the user signs in explicitly again, or when you explicitly force it to get a new ID token.

Common ways to make this flow run smoothly for your users are to:

  • Force a ID token refresh when the iOS regains focus.
  • Force a Id token refresh periodically, while on a "waiting until you verified your email address" screen.,
  • Show a button to the user "I verified my email address" and then refresh the ID token.

Alternatively you can sign the user out after sending them the email, and then check when they sign in again. While this is simpler to implement, the above flow is more user friendly.

Upvotes: 2

Related Questions