@@ -2,9 +2,11 @@ import { Observable } from "tns-core-modules/data/observable";
2
2
import { alert , prompt } from "tns-core-modules/ui/dialogs" ;
3
3
import { ios as iosUtils } from "tns-core-modules/utils/utils" ;
4
4
import { isIOS } from "tns-core-modules/platform" ;
5
- import { AddEventListenerResult } from "nativescript-plugin-firebase" ;
5
+ import { AddEventListenerResult , User } from "nativescript-plugin-firebase" ;
6
6
import * as fs from "tns-core-modules/file-system" ;
7
+
7
8
const firebase = require ( "nativescript-plugin-firebase" ) ;
9
+ const firebaseWebApi = require ( "nativescript-plugin-firebase/app" ) ;
8
10
9
11
declare const assert : any ;
10
12
@@ -13,6 +15,223 @@ export class HelloWorldModel extends Observable {
13
15
public userEmailOrPhone : string ;
14
16
private userListenerWrapper : AddEventListenerResult ;
15
17
private companiesListenerWrapper : AddEventListenerResult ;
18
+ private onAuthStateChangedHandlerSet = false ;
19
+
20
+
21
+ /***********************************************
22
+ * Web API usage examples
23
+ ***********************************************/
24
+
25
+ private ensureWebOnAuthChangedHandler ( ) : void {
26
+ if ( ! this . onAuthStateChangedHandlerSet ) {
27
+ this . onAuthStateChangedHandlerSet = true ;
28
+ firebaseWebApi . auth ( ) . onAuthStateChanged ( ( user ?: User ) => {
29
+ console . log ( ">> auth state changed: " + user ) ;
30
+ if ( user ) {
31
+ this . set ( "userEmailOrPhone" , user . email ? user . email : ( user . phoneNumber ? user . phoneNumber : "N/A" ) ) ;
32
+ alert ( {
33
+ title : "User signed in" ,
34
+ message : JSON . stringify ( user ) ,
35
+ okButtonText : "Nice!"
36
+ } ) ;
37
+ } else {
38
+ alert ( {
39
+ title : "User signed out" ,
40
+ okButtonText : "Bye!"
41
+ } ) ;
42
+ }
43
+ } ) ;
44
+ }
45
+ }
46
+
47
+ public doWebInit ( ) : void {
48
+ firebaseWebApi . initializeApp ( ) ;
49
+ }
50
+
51
+ public doWebLoginAnonymously ( ) : void {
52
+ this . ensureWebOnAuthChangedHandler ( ) ;
53
+ firebaseWebApi . auth ( ) . signInAnonymously ( )
54
+ . catch ( err => {
55
+ alert ( {
56
+ title : "Login error" ,
57
+ message : JSON . stringify ( err ) ,
58
+ okButtonText : "OK, pity"
59
+ } ) ;
60
+ }
61
+ ) ;
62
+ }
63
+
64
+ public doWebLoginByPassword ( ) : void {
65
+ this . ensureWebOnAuthChangedHandler ( ) ;
66
+ firebaseWebApi . auth ( ) . signInWithEmailAndPassword ( '[email protected] ' , 'firebase' )
67
+ . catch ( err => {
68
+ alert ( {
69
+ title : "Login error" ,
70
+ message : JSON . stringify ( err ) ,
71
+ okButtonText : "OK, pity"
72
+ } ) ;
73
+ }
74
+ ) ;
75
+ }
76
+
77
+ public doWebFetchProvidersForEmail ( ) : void {
78
+ const user = firebaseWebApi . auth ( ) . currentUser ;
79
+ if ( ! user || ! user . email ) {
80
+ alert ( {
81
+ title : "Can't fetch providers" ,
82
+ message : "No user with emailaddress logged in." ,
83
+ okButtonText : "OK, makes sense.."
84
+ } ) ;
85
+ return ;
86
+ }
87
+
88
+ firebaseWebApi . auth ( ) . fetchProvidersForEmail ( user . email ) . then (
89
+ result => {
90
+ alert ( {
91
+ title : `Providers for ${ user . email } ` ,
92
+ message : JSON . stringify ( result ) , // likely to be ["password"]
93
+ okButtonText : "Thanks!"
94
+ } ) ;
95
+ } ,
96
+ errorMessage => {
97
+ alert ( {
98
+ title : "Fetch Providers for Email error" ,
99
+ message : errorMessage ,
100
+ okButtonText : "OK, pity.."
101
+ } ) ;
102
+ }
103
+ ) ;
104
+ }
105
+
106
+ public doWebLogout ( ) : void {
107
+ firebaseWebApi . auth ( ) . signOut ( )
108
+ . then ( ( ) => {
109
+ this . set ( "userEmailOrPhone" , null ) ;
110
+ alert ( {
111
+ title : "Logout OK" ,
112
+ okButtonText : "OK, bye!"
113
+ } ) ;
114
+ } )
115
+ . catch ( error => {
116
+ alert ( {
117
+ title : "Logout error" ,
118
+ message : JSON . stringify ( error ) ,
119
+ okButtonText : "Hmmkay"
120
+ } ) ;
121
+ }
122
+ ) ;
123
+ }
124
+
125
+ public doWebCreateUser ( ) : void {
126
+ firebaseWebApi . auth ( ) . createUserWithEmailAndPassword ( '[email protected] ' , 'firebase' )
127
+ . then ( result => {
128
+ alert ( {
129
+ title : "User created" ,
130
+ message : JSON . stringify ( result ) ,
131
+ okButtonText : "Nice!"
132
+ } ) ;
133
+ } )
134
+ . catch (
135
+ error => {
136
+ alert ( {
137
+ title : "No user created" ,
138
+ message : JSON . stringify ( error ) ,
139
+ okButtonText : "OK, got it"
140
+ } ) ;
141
+ }
142
+ ) ;
143
+ }
144
+
145
+ public doWebGetCurrentUser ( ) : void {
146
+ const user = firebaseWebApi . auth ( ) . currentUser ;
147
+ if ( user ) {
148
+ alert ( {
149
+ title : "Current user" ,
150
+ message : JSON . stringify ( user ) ,
151
+ okButtonText : "Nice!"
152
+ } ) ;
153
+ } else {
154
+ alert ( {
155
+ title : "No current user" ,
156
+ okButtonText : "OK, thanks"
157
+ } ) ;
158
+ }
159
+ }
160
+
161
+ public doWebAddValueEventListenerForCompanies ( ) : void {
162
+ const path = "/companies" ;
163
+ const onValueEvent = result => {
164
+ if ( result . error ) {
165
+ alert ( {
166
+ title : "Listener error" ,
167
+ message : result . error ,
168
+ okButtonText : "Darn!"
169
+ } ) ;
170
+ } else {
171
+ this . set ( "path" , path ) ;
172
+ this . set ( "key" , result . key ) ;
173
+ this . set ( "value" , JSON . stringify ( result . val ( ) ) ) ;
174
+ }
175
+ } ;
176
+
177
+ firebaseWebApi . database ( ) . ref ( path ) . on ( "value" , onValueEvent ) ;
178
+ }
179
+
180
+ public doWebRemoveValueEventListenersForCompanies ( ) : void {
181
+ const path = "/companies" ;
182
+ firebaseWebApi . database ( ) . ref ( path ) . off ( "value" ) ;
183
+ }
184
+
185
+ public doWebGetValueForCompanies ( ) : void {
186
+ const path = "/companies" ;
187
+ firebaseWebApi . database ( ) . ref ( path )
188
+ . once ( "value" )
189
+ . then ( result => {
190
+ this . set ( "path" , path ) ;
191
+ this . set ( "key" , result . key ) ;
192
+ this . set ( "value" , JSON . stringify ( result . val ( ) ) ) ;
193
+ } )
194
+ . catch ( error => console . log ( "doWebGetValueForCompanies error: " + error ) ) ;
195
+ }
196
+
197
+ public doWebStoreCompaniesBySetValue ( ) : void {
198
+ firebaseWebApi . database ( ) . ref ( "/companies" )
199
+ . set ( [
200
+ {
201
+ name : 'Telerik (web)' ,
202
+ country : 'Bulgaria' ,
203
+ since : 2000 ,
204
+ updateTs : firebase . ServerValue . TIMESTAMP
205
+ } ,
206
+ {
207
+ name : 'Google (web)' ,
208
+ country : 'USA' ,
209
+ since : 1900 ,
210
+ updateTs : firebase . ServerValue . TIMESTAMP
211
+ }
212
+ ]
213
+ )
214
+ . then ( ( ) => console . log ( "firebase.setValue done" ) )
215
+ . catch ( error => console . log ( "firebase.setValue error: " + error ) ) ;
216
+ }
217
+
218
+ public doWebRemoveCompanies ( ) : void {
219
+ firebaseWebApi . database ( ) . ref ( "/companies" ) . remove ( )
220
+ . then ( ( ) => console . log ( "firebase.remove done" ) )
221
+ . catch ( ( err ) => console . log ( "firebase.remove error: " + err ) ) ;
222
+ }
223
+
224
+ public doWebQueryBulgarianCompanies ( ) : void {
225
+ const path = "/companies" ;
226
+ const child = "name" ;
227
+ firebaseWebApi . database ( ) . ref ( path ) . orderByChild ( child ) ;
228
+ }
229
+
230
+
231
+
232
+ /***********************************************
233
+ * Native API usage examples
234
+ ***********************************************/
16
235
17
236
public doInit ( ) : void {
18
237
firebase . init ( {
@@ -724,6 +943,21 @@ export class HelloWorldModel extends Observable {
724
943
) ;
725
944
}
726
945
946
+ public doGetValueForCompanies ( ) : void {
947
+ firebase . getValue ( '/companies' ) . then (
948
+ result => {
949
+ alert ( {
950
+ title : "Value retrieved" ,
951
+ message : JSON . stringify ( result ) ,
952
+ okButtonText : "OK"
953
+ } ) ;
954
+ } ,
955
+ error => {
956
+ console . log ( "doGetValueForCompanies error: " + error ) ;
957
+ }
958
+ ) ;
959
+ }
960
+
727
961
public doUserStoreByPush ( ) : void {
728
962
firebase . push (
729
963
'/users' ,
0 commit comments