           text: $email, placeholder: "Email", symbolName: "person.circle.fill"
         )
 
-        CustomStyledButton(title: "Send Sign In Link") {
-          sendSignInLink()
-        }
-        .disabled(email.isEmpty)
+        CustomStyledButton(title: "Send Sign In Link", action: sendSignInLink)
+          .disabled(email.isEmpty)
 
         Spacer()
       }
       .padding()
       .navigationBarTitle("Passwordless Login")
     }
-    .onOpenURL { (url) in
+    .onOpenURL { url in
       let link = url.absoluteString
       if Auth.auth().isSignIn(withEmailLink: link) {
-        passwordlessSignIn(email: email, link: link) { user, error in
-          isPresentingSheet = user?.isEmailVerified ?? false
+        passwordlessSignIn(email: email, link: link) { result in
+          switch result {
+          case let .success(user):
+            isPresentingSheet = user?.isEmailVerified ?? false
+          case .failure:
+            isPresentingSheet = false
+          }
         }
       }
     }
     .sheet(isPresented: $isPresentingSheet) {
-      SuccessView(email: $email)
+      SuccessView(email: email)
     }
   }
 


  var body: some View {
    Button(action: action) {
      Spacer()
      Text(title)
        .padding()
        .accentColor(.white)
      Spacer()
    }
    .background(Color.orange)
    .cornerRadius(16.0)
  }

}

/// Displayed when a user successfuly logs in.
struct SuccessView: View {
  @Binding var email: String
  
  var body: some View {
    /// The first view in this `ZStack` is a `Color` view that expands
    /// to set the background color of the `SucessView`.
    ZStack {
      Color.orange
        .edgesIgnoringSafeArea(.all)
        
      VStack(alignment: .leading) {
        Group {
          Text("Welcome")
            .font(.largeTitle)
            .fontWeight(.semibold)
          
          Text(email.lowercased())
            .font(.title3)
            .fontWeight(.bold)
            .multilineTextAlignment(.leading)
        }
        .padding(.leading)
        
        Image(systemName: "checkmark.circle")
          .resizable()
          .aspectRatio(contentMode: .fit)

}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

// MARK: Example Auth View Modifier

extension View {
  func handlePasswordlessLogin(forEmail email: String,
                               completion: @escaping (Result<User?, Error>) -> Void) -> some View {
    onOpenURL { url in
      let link = url.absoluteString
      if Auth.auth().isSignIn(withEmailLink: link) {
        Auth.auth().signIn(withEmail: email, link: link) { result, error in
          if let error = error {
            print("ⓧ Authentication error: \(error.localizedDescription).")
            completion(.failure(error))
          } else {
            print("✔ Authentication was successful.")
            completion(.success(Auth.auth().currentUser))
          }
        }
      }
    }
  }
}
