|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:html'; |
| 3 | + |
| 4 | +import 'package:firebase/firebase.dart' as fb; |
| 5 | +import 'package:firebase/src/assets/assets.dart'; |
| 6 | + |
| 7 | +main() async { |
| 8 | + //Use for firebase package development only |
| 9 | + await config(); |
| 10 | + |
| 11 | + try { |
| 12 | + fb.initializeApp( |
| 13 | + apiKey: apiKey, |
| 14 | + authDomain: authDomain, |
| 15 | + databaseURL: databaseUrl, |
| 16 | + storageBucket: storageBucket); |
| 17 | + |
| 18 | + new PhoneAuthApp(); |
| 19 | + } on fb.FirebaseJsNotLoadedException catch (e) { |
| 20 | + print(e); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class PhoneAuthApp { |
| 25 | + final fb.Auth auth; |
| 26 | + final FormElement registerForm, verificationForm; |
| 27 | + final InputElement phone, code; |
| 28 | + final AnchorElement logout; |
| 29 | + final TableElement authInfo; |
| 30 | + final ParagraphElement error; |
| 31 | + |
| 32 | + fb.RecaptchaVerifier verifier; |
| 33 | + fb.ConfirmationResult confirmationResult; |
| 34 | + |
| 35 | + PhoneAuthApp() |
| 36 | + : this.auth = fb.auth(), |
| 37 | + this.logout = querySelector("#logout_btn"), |
| 38 | + this.error = querySelector(".error"), |
| 39 | + this.authInfo = querySelector("#auth_info"), |
| 40 | + this.phone = querySelector("#phone"), |
| 41 | + this.code = querySelector("#code"), |
| 42 | + this.registerForm = querySelector("#register_form"), |
| 43 | + this.verificationForm = querySelector("#verification_form") { |
| 44 | + logout.onClick.listen((e) { |
| 45 | + e.preventDefault(); |
| 46 | + auth.signOut(); |
| 47 | + _resetVerifier(); |
| 48 | + }); |
| 49 | + |
| 50 | + this.registerForm.onSubmit.listen((e) { |
| 51 | + e.preventDefault(); |
| 52 | + var phoneValue = phone.value.trim(); |
| 53 | + _registerUser(phoneValue); |
| 54 | + }); |
| 55 | + |
| 56 | + this.verificationForm.onSubmit.listen((e) { |
| 57 | + e.preventDefault(); |
| 58 | + var codeValue = code.value.trim(); |
| 59 | + _verifyUser(codeValue); |
| 60 | + }); |
| 61 | + |
| 62 | + // After opening |
| 63 | + if (auth.currentUser != null) { |
| 64 | + _setLayout(auth.currentUser); |
| 65 | + } else { |
| 66 | + _initVerifier(); |
| 67 | + } |
| 68 | + |
| 69 | + // When auth state changes |
| 70 | + auth.onAuthStateChanged.listen((e) => _setLayout(e)); |
| 71 | + } |
| 72 | + |
| 73 | + _initVerifier() { |
| 74 | + // This is anonymous recaptcha - size must be defined |
| 75 | + verifier = new fb.RecaptchaVerifier("register", { |
| 76 | + "size": "invisible", |
| 77 | + "callback": (resp) { |
| 78 | + print("reCAPTCHA solved, allow signInWithPhoneNumber."); |
| 79 | + }, |
| 80 | + "expired-callback": () { |
| 81 | + print("Response expired. Ask user to solve reCAPTCHA again."); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + // Use this if you want to use recaptcha widget directly |
| 86 | + //verifier = new fb.RecaptchaVerifier("recaptcha-container")..render(); |
| 87 | + } |
| 88 | + |
| 89 | + _resetVerifier() { |
| 90 | + verifier.clear(); |
| 91 | + _initVerifier(); |
| 92 | + } |
| 93 | + |
| 94 | + _registerUser(String phone) async { |
| 95 | + if (phone.isNotEmpty) { |
| 96 | + try { |
| 97 | + confirmationResult = await auth.signInWithPhoneNumber(phone, verifier); |
| 98 | + verificationForm.style.display = "block"; |
| 99 | + registerForm.style.display = "none"; |
| 100 | + } catch (e) { |
| 101 | + error.text = e.toString(); |
| 102 | + } |
| 103 | + } else { |
| 104 | + error.text = "Please fill correct phone number."; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + _verifyUser(String code) async { |
| 109 | + if (code.isNotEmpty && confirmationResult != null) { |
| 110 | + try { |
| 111 | + await confirmationResult.confirm(code); |
| 112 | + } catch (e) { |
| 113 | + error.text = e.toString(); |
| 114 | + } |
| 115 | + } else { |
| 116 | + error.text = "Please fill correct verification code."; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + void _setLayout(fb.User user) { |
| 121 | + if (user != null) { |
| 122 | + registerForm.style.display = "none"; |
| 123 | + verificationForm.style.display = "none"; |
| 124 | + logout.style.display = "block"; |
| 125 | + phone.value = ""; |
| 126 | + code.value = ""; |
| 127 | + error.text = ""; |
| 128 | + authInfo.style.display = "block"; |
| 129 | + |
| 130 | + var data = <String, dynamic>{ |
| 131 | + "email": user.email, |
| 132 | + "emailVerified": user.emailVerified, |
| 133 | + "isAnonymous": user.isAnonymous, |
| 134 | + "phoneNumber": user.phoneNumber |
| 135 | + }; |
| 136 | + |
| 137 | + data.forEach((k, v) { |
| 138 | + if (v != null) { |
| 139 | + var row = authInfo.addRow(); |
| 140 | + |
| 141 | + row.addCell() |
| 142 | + ..text = k |
| 143 | + ..classes.add("header"); |
| 144 | + row.addCell()..text = "$v"; |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + print("User.toJson:"); |
| 149 | + print(const JsonEncoder.withIndent(' ').convert(user)); |
| 150 | + } else { |
| 151 | + registerForm.style.display = "block"; |
| 152 | + authInfo.style.display = "none"; |
| 153 | + logout.style.display = "none"; |
| 154 | + authInfo.children.clear(); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments