Skip to content

Commit 9c989f6

Browse files
committed
chore: add phone auth demo
1 parent a625a0d commit 9c989f6

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

apps/demo/src/plugin-demos/firebase-auth.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Observable, EventData, Page, fromObject } from '@nativescript/core';
22
import { DemoSharedFirebaseAuth } from '@demo/shared';
33
import { firebase, Firebase } from '@nativescript/firebase-core';
44
import '@nativescript/firebase-auth';
5-
import { Auth, User, OAuthProvider } from '@nativescript/firebase-auth';
5+
import { Auth, User, OAuthProvider, PhoneAuthProvider } from '@nativescript/firebase-auth';
66
export function navigatingTo(args: EventData) {
77
const page = <Page>args.object;
88
page.bindingContext = new DemoModel();
@@ -12,6 +12,9 @@ export class DemoModel extends DemoSharedFirebaseAuth {
1212
email: string;
1313
password: string;
1414
user: User;
15+
phoneNumber: string;
16+
code: string;
17+
verificationId: string;
1518
constructor() {
1619
super();
1720

@@ -22,6 +25,45 @@ export class DemoModel extends DemoSharedFirebaseAuth {
2225
});
2326
}
2427

28+
async linkPhone() {
29+
if (!firebase().auth().currentUser) {
30+
console.info('Login to link phone');
31+
return;
32+
}
33+
try {
34+
const cred = PhoneAuthProvider.provider().credential(this.verificationId, this.code);
35+
const linkedCred = await firebase()
36+
.auth()
37+
.currentUser
38+
.linkWithCredential(cred);
39+
console.log('verificationId', linkedCred);
40+
} catch (e) {
41+
console.log('linkPhone error:', e);
42+
}
43+
}
44+
45+
async getVerificationCode() {
46+
try {
47+
this.verificationId = await PhoneAuthProvider.provider().verifyPhoneNumber(this.phoneNumber);
48+
} catch (e) {
49+
console.log('getVerificationCode error:', e);
50+
}
51+
}
52+
53+
async loginWithPhone() {
54+
try {
55+
const cred = PhoneAuthProvider.provider().credential(this.verificationId, this.code);
56+
const value = await firebase()
57+
.auth()
58+
.signInWithCredential(cred);
59+
console.log('verificationId', this.verificationId);
60+
console.log('loginUser', value);
61+
this._setCurrentUser(value.user);
62+
} catch (e) {
63+
console.log('linkPhone error:', e);
64+
}
65+
}
66+
2567
createUser() {
2668
firebase()
2769
.auth()

apps/demo/src/plugin-demos/firebase-auth.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
<StackLayout>
99
<TextField hint="Enter email" text="{{ email }}" keyboardType="email" autocapitalizationType="none"/>
1010
<TextField hint="Enter password" text="{{ password }}" secure="true"/>
11+
<TextField hint="Enter Phone Number" text="{{ phoneNumber }}"/>
12+
<TextField hint="Enter Verification Code" text="{{ code }}"/>
13+
<Button text="Get Phone Verification Code" tap="{{ getVerificationCode }}" class="btn btn-primary"/>
1114
<Button text="Create User" tap="{{ createUser }}" class="btn btn-primary"/>
1215
<Button text="Login User" tap="{{ loginUser }}" class="btn btn-primary"/>
16+
<Button text="Login User With Phone" tap="{{ loginWithPhone }}" class="btn btn-primary"/>
17+
<Button text="Link User Phone" tap="{{ linkPhone }}" class="btn btn-primary"/>
1318
<Button text="Get Current User" tap="{{ getCurrentUser }}" class="btn btn-primary"/>
1419
<Button text="Logout User" tap="{{ logOutUser }}" class="btn btn-primary"/>
1520
<StackLayout>

0 commit comments

Comments
 (0)