Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 96c7383

Browse files
Janamoukevmoo
authored andcommitted
Add phone support and upgraded JS API to v4.4
1 parent 03649c0 commit 96c7383

File tree

17 files changed

+532
-36
lines changed

17 files changed

+532
-36
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
## 4.1.0
22

3+
* Upgraded to Firebase JS API `4.2.0`.
34
* Added `toJson` to `DataSnapshot` and `Query`.
5+
* Added more tests for V4 API.
6+
* `Auth`:
7+
* Implemented `PhoneAuthProvider` and `RecaptchaVerifier`.
8+
* `User`:
9+
* Added `phoneNumber` property to the `UserInfo`.
10+
* Added `linkWithPhoneNumber`, `updatePhoneNumber` and `reauthenticateWithPhoneNumber` methods.
11+
* New example demonstrating `PhoneAuthProvider` functionality in `example/auth_phone`.
412

513
## 4.0.0
614

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ You must include the original Firebase JavaScript source into your `.html` file
3737
to be able to use the library.
3838

3939
```html
40-
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
40+
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
4141
```
4242

4343
### Use it
@@ -133,6 +133,7 @@ The following providers need to be enabled in Firebase console,
133133

134134
* E-mail/password
135135
* Anonymous
136+
* Phone
136137

137138
### Database tests and example
138139

example/auth/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h2>Register</h2>
3333
<button id="verify_email" style="display: none">Verify email</button>
3434
<a href="#" id="logout_btn" style="display: none">Log out</a>
3535
</div>
36-
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
36+
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
3737
<script src="index.dart" type="application/dart"></script>
3838
<script src="packages/browser/dart.js"></script>
3939
</body>

example/auth_phone/index.dart

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}

example/auth_phone/index.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Phone auth demo</title>
6+
<style type="text/css">
7+
.error {
8+
color: red;
9+
}
10+
#auth_info td {
11+
padding: 1px;
12+
}
13+
#auth_info td.header {
14+
font-weight: bold;
15+
text-align: right;
16+
}
17+
</style>
18+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
19+
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
20+
</head>
21+
<body>
22+
<div class="container">
23+
<h1>Phone auth demo</h1>
24+
<p class="error"></p>
25+
<h2>Register with phone number</h2>
26+
<form id="register_form" style="display: none">
27+
<label for="phone">Your phone number in E.164 format (e.g. +16505550101).</label><br>
28+
<input type="text" placeholder="Your phone number" id="phone">
29+
<div id="recaptcha-container"></div>
30+
<input type="submit" value="Register me" id="register">
31+
</form>
32+
<form id="verification_form" style="display: none">
33+
<h3>Verification code</h3>
34+
<p></p>
35+
<input type="text" placeholder="Your verification code" id="code">
36+
<input type="submit" value="Verify me">
37+
</form>
38+
<table id="auth_info" style="display: none">
39+
</table>
40+
<a href="#" id="logout_btn" style="display: none">Log out</a>
41+
</div>
42+
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
43+
<script src="index.dart" type="application/dart"></script>
44+
<script src="packages/browser/dart.js"></script>
45+
</body>
46+
</html>

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<h1>Examples</h1>
1212
<ul>
1313
<li><a href="auth/index.html">auth</a></li>
14+
<li><a href="auth_phone/index.html">phone auth</a></li>
1415
<li><a href="realtime_database/index.html">database</a></li>
1516
<li><a href="storage/index.html">storage</a></li>
1617
</ul>

example/realtime_database/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h1>Messages demo</h1>
4646
<input type="submit" value="Send" id="submit" disabled>
4747
</form>
4848
</div>
49-
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
49+
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
5050
<script src="index.dart" type="application/dart"></script>
5151
<script src="packages/browser/dart.js"></script>
5252
</body>

example/storage/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1>Upload image demo</h1>
1414
</form>
1515
<p id="message"></p>
1616
</div>
17-
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
17+
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
1818
<script src="index.dart" type="application/dart"></script>
1919
<script src="packages/browser/dart.js"></script>
2020
</body>

0 commit comments

Comments
 (0)