|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | + |
| 3 | +import firebase |
| 4 | +import FirebaseCore |
| 5 | +import FirebaseAuth |
| 6 | +import SwiftWin32 |
| 7 | + |
| 8 | +private final class FireBaseLogLevelPickerHandler { |
| 9 | + private let levels = ["Default", "Verbose", "Debug", "Info", "Warning", "Error", "Assert"] |
| 10 | +} |
| 11 | + |
| 12 | +extension FireBaseLogLevelPickerHandler: PickerViewDataSource { |
| 13 | + public func numberOfComponents(in pickerView: PickerView) -> Int { |
| 14 | + 1 |
| 15 | + } |
| 16 | + |
| 17 | + public func pickerView(_ pickerView: PickerView, |
| 18 | + numberOfRowsInComponent component: Int) -> Int { |
| 19 | + self.levels.count |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +extension FireBaseLogLevelPickerHandler: PickerViewDelegate { |
| 24 | + public func pickerView(_ pickerView: PickerView, titleForRow row: Int, |
| 25 | + forComponent component: Int) -> String? { |
| 26 | + self.levels[row] |
| 27 | + } |
| 28 | + |
| 29 | + public func pickerView(_ pickerView: PickerView, didSelectRow row: Int, |
| 30 | + inComponent component: Int) { |
| 31 | + guard row > 0 else { return } |
| 32 | + FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel(rawValue: CInt(row - 1))) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// MARK: - FireBaseUIViewController |
| 37 | + |
| 38 | +internal final class FireBaseUIViewController: ViewController { |
| 39 | + fileprivate let firebaseLogHandler = FireBaseLogLevelPickerHandler() |
| 40 | + |
| 41 | + var cboLogLevel = PickerView(frame: Rect(x: 136, y: 8, width: 256, height: 24)) |
| 42 | + var txtEmail = TextField(frame: Rect(x: 136, y: 46, width: 512, height: 24)) |
| 43 | + var txtPassword = TextField(frame: Rect(x: 136, y: 78, width: 512, height: 24)) |
| 44 | + var btnSignIn = Button(frame: Rect(x: 8, y: 116, width: 120, height: 32), |
| 45 | + title: "Sign In") |
| 46 | + var btnToken = Button(frame: Rect(x: 8, y: 180, width: 120, height: 32), |
| 47 | + title: "Get Token") |
| 48 | + var chkRefresh = Switch(frame: Rect(x: 8, y: 212, width: 648, height: 32), |
| 49 | + title: "Force Token Refresh") |
| 50 | + var txtToken: TextView = TextView(frame: Rect(x: 8, y: 244, width: 640, height: 48)) |
| 51 | + |
| 52 | + var btnCreate = Button(frame: Rect(x: 8, y: 324, width: 120, height: 32), |
| 53 | + title: "Create User") |
| 54 | + var btnVerify = Button(frame: Rect(x: 132, y: 324, width: 120, height: 32), |
| 55 | + title: "Verify Email") |
| 56 | + var btnReset = Button(frame: Rect(x: 256, y: 324, width: 156, height: 32), |
| 57 | + title: "Reset Password") |
| 58 | + |
| 59 | + override func viewDidLoad() { |
| 60 | + super.viewDidLoad() |
| 61 | + self.title = "FireBase UI" |
| 62 | + configureView() |
| 63 | + |
| 64 | + if let user = Auth.auth().currentUser { |
| 65 | + txtEmail.text = user.email |
| 66 | + try? Auth.auth().signOut() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private func configureView() { |
| 71 | + let lblLogLevel = Label(frame: Rect(x: 8, y: 8, width: 128, height: 24), |
| 72 | + title: "Log Level:") |
| 73 | + self.view?.addSubview(lblLogLevel) |
| 74 | + |
| 75 | + cboLogLevel.dataSource = firebaseLogHandler |
| 76 | + cboLogLevel.delegate = firebaseLogHandler |
| 77 | + self.view?.addSubview(cboLogLevel) |
| 78 | + cboLogLevel.reloadAllComponents() |
| 79 | + cboLogLevel.selectRow(0, inComponent: 0, animated: false) |
| 80 | + |
| 81 | + let lblEmail = Label(frame: Rect(x: 8, y: 46, width: 128, height: 20), |
| 82 | + title: "Email Address:") |
| 83 | + self.view?.addSubview(lblEmail) |
| 84 | + self.view?.addSubview(txtEmail) |
| 85 | + |
| 86 | + let lblPassword = Label(frame: Rect(x: 8, y: 78, width: 128, height: 20), |
| 87 | + title: "Password:") |
| 88 | + self.view?.addSubview(lblPassword) |
| 89 | + |
| 90 | + txtPassword.isSecureTextEntry = true |
| 91 | + self.view?.addSubview(txtPassword) |
| 92 | + |
| 93 | + btnSignIn.addTarget(self, action: FireBaseUIViewController.signIn, |
| 94 | + for: .primaryActionTriggered) |
| 95 | + self.view?.addSubview(btnSignIn) |
| 96 | + |
| 97 | + btnToken.addTarget(self, action: FireBaseUIViewController.getToken, |
| 98 | + for: .primaryActionTriggered) |
| 99 | + self.view?.addSubview(btnToken) |
| 100 | + |
| 101 | + self.view?.addSubview(chkRefresh) |
| 102 | + |
| 103 | + txtToken.editable = false |
| 104 | + txtToken.font = .systemFont(ofSize: 9) |
| 105 | + self.view?.addSubview(txtToken) |
| 106 | + |
| 107 | + btnCreate.addTarget(self, action: FireBaseUIViewController.createUser, |
| 108 | + for: .primaryActionTriggered) |
| 109 | + self.view?.addSubview(btnCreate) |
| 110 | + |
| 111 | + btnVerify.addTarget(self, action: FireBaseUIViewController.verifyEmail, |
| 112 | + for: .primaryActionTriggered) |
| 113 | + self.view?.addSubview(btnVerify) |
| 114 | + |
| 115 | + btnReset.addTarget(self, action: FireBaseUIViewController.resetPassword, |
| 116 | + for: .primaryActionTriggered) |
| 117 | + self.view?.addSubview(btnReset) |
| 118 | + } |
| 119 | + |
| 120 | + private func signIn() { |
| 121 | + guard let email = txtEmail.text, let password = txtPassword.text else { |
| 122 | + print("email and password are required") |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + Task { |
| 127 | + do { |
| 128 | + _ = try await Auth.auth().signIn(withEmail: email, password: password) |
| 129 | + } catch { |
| 130 | + print("Error signing in: \(error.localizedDescription)") |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private func createUser() { |
| 136 | + guard let email = txtEmail.text, let password = txtPassword.text else { |
| 137 | + print("email and password are required") |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + Task { |
| 142 | + do { |
| 143 | + _ = try await Auth.auth().createUser(withEmail: email, password: password) |
| 144 | + } catch { |
| 145 | + print("Error signing in: \(error.localizedDescription)") |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private func getToken() { |
| 151 | + Task { |
| 152 | + guard var user = Auth.auth().currentUser else { |
| 153 | + print("user not logged in") |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + if chkRefresh.isOn { |
| 158 | + do { |
| 159 | + let result = try await user.getIDTokenResult(forcingRefresh: true) |
| 160 | + txtToken.text = result.token |
| 161 | + } catch { |
| 162 | + print("Error refreshing token: \(error.localizedDescription)") |
| 163 | + } |
| 164 | + } else { |
| 165 | + do { |
| 166 | + txtToken.text = try await user.getIDToken() |
| 167 | + } catch { |
| 168 | + print("Error refreshing token: \(error.localizedDescription)") |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private func verifyEmail() { |
| 175 | + Task { |
| 176 | + guard var user = Auth.auth().currentUser else { |
| 177 | + print("user not logged in") |
| 178 | + return |
| 179 | + } |
| 180 | + |
| 181 | + try await user.sendEmailVerification() |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private func resetPassword() { |
| 186 | + guard let email = txtEmail.text else { |
| 187 | + print("email is required") |
| 188 | + return |
| 189 | + } |
| 190 | + |
| 191 | + Task { |
| 192 | + do { |
| 193 | + _ = try await Auth.auth().sendPasswordReset(withEmail: email) |
| 194 | + } catch { |
| 195 | + print("Error sending password reset: \(error.localizedDescription)") |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments