@@ -2,7 +2,7 @@ import { Observable, EventData, Page, fromObject } from '@nativescript/core';
2
2
import { DemoSharedFirebaseAuth } from '@demo/shared' ;
3
3
import { firebase , Firebase } from '@nativescript/firebase-core' ;
4
4
import '@nativescript/firebase-auth' ;
5
- import { Auth , User , OAuthProvider } from '@nativescript/firebase-auth' ;
5
+ import { Auth , User , OAuthProvider , PhoneAuthProvider } from '@nativescript/firebase-auth' ;
6
6
export function navigatingTo ( args : EventData ) {
7
7
const page = < Page > args . object ;
8
8
page . bindingContext = new DemoModel ( ) ;
@@ -12,6 +12,9 @@ export class DemoModel extends DemoSharedFirebaseAuth {
12
12
email : string ;
13
13
password : string ;
14
14
user : User ;
15
+ phoneNumber : string ;
16
+ code : string ;
17
+ verificationId : string ;
15
18
constructor ( ) {
16
19
super ( ) ;
17
20
@@ -22,6 +25,45 @@ export class DemoModel extends DemoSharedFirebaseAuth {
22
25
} ) ;
23
26
}
24
27
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
+
25
67
createUser ( ) {
26
68
firebase ( )
27
69
. auth ( )
0 commit comments