@@ -2,7 +2,7 @@ import { Observable, EventData, Page, fromObject } from '@nativescript/core';
22import { DemoSharedFirebaseAuth } from '@demo/shared' ;
33import { firebase , Firebase } from '@nativescript/firebase-core' ;
44import '@nativescript/firebase-auth' ;
5- import { Auth , User , OAuthProvider } from '@nativescript/firebase-auth' ;
5+ import { Auth , User , OAuthProvider , PhoneAuthProvider } from '@nativescript/firebase-auth' ;
66export 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 ( )
0 commit comments