Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit c583119

Browse files
Get push key avoiding .then() #624
1 parent 953d944 commit c583119

File tree

8 files changed

+416
-77
lines changed

8 files changed

+416
-77
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
[Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

66

7+
## 5.1.7 (2018, February 11)
8+
9+
### New
10+
11+
- [#624](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/624) Get push key avoiding .then()
12+
13+
714
## 5.1.6 (2018, February 1)
815

916
### Fixes

demo/app/main-page.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<TabViewItem.view>
1919
<ScrollView>
2020
<GridLayout columns="*, *"
21-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
21+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
2222
horizontalAlignment="stretch"
2323
class="tab-content">
2424
<Button row="0" colSpan="2" text="init firebase - do this first" tap="{{ doWebInit }}" class="button button-positive"/>
@@ -48,17 +48,17 @@
4848
<Button row="11" col="0" text="ordered query" tap="{{ doWebQueryBulgarianCompanies }}" class="button button-company"/>
4949
<Button row="11" col="1" text="remove path" tap="{{ doWebRemoveCompanies }}" class="button button-company"/>
5050

51-
<Label row="12" col="0" text="Last update path:" class="message"/>
52-
<Label row="12" col="1" text="{{ path }}" class="message" textWrap="true"/>
51+
<Button row="12" col="0" text="store with key" tap="{{ doWebStoreCompanyByFirstCreatingKey }}" class="button button-company"/>
52+
<Button row="12" col="1" text="get server info" tap="{{ doWebGetServerInfo }}" class="button button-company"/>
5353

54-
<Label row="13" col="0" text="Key:" class="message"/>
55-
<Label row="13" col="1" text="{{ key }}" class="message" textWrap="true"/>
54+
<Label row="13" col="0" text="Last update path:" class="message"/>
55+
<Label row="13" col="1" text="{{ path }}" class="message" textWrap="true"/>
5656

57-
<Label row="14" col="0" text="Value:" class="message"/>
58-
<Label row="14" col="1" text="{{ value }}" class="message" textWrap="true"/>
57+
<Label row="14" col="0" text="Key:" class="message"/>
58+
<Label row="14" col="1" text="{{ key }}" class="message" textWrap="true"/>
5959

60-
<!--<Button row="10" col="0" text="query with range" tap="{{ doQueryBulgarianCompanies }}" class="button button-company"/>-->
61-
<!--<Button row="10" col="1" text="delete path" tap="{{ doRemoveCompanies }}" class="button button-company"/>-->
60+
<Label row="15" col="0" text="Value:" class="message"/>
61+
<Label row="15" col="1" text="{{ value }}" class="message" textWrap="true"/>
6262

6363
</GridLayout>
6464
</ScrollView>

demo/app/main-view-model.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class HelloWorldModel extends Observable {
5151
public doWebLoginAnonymously(): void {
5252
this.ensureWebOnAuthChangedHandler();
5353
firebaseWebApi.auth().signInAnonymously()
54+
.then(() => console.log("User logged in"))
5455
.catch(err => {
5556
alert({
5657
title: "Login error",
@@ -64,6 +65,7 @@ export class HelloWorldModel extends Observable {
6465
public doWebLoginByPassword(): void {
6566
this.ensureWebOnAuthChangedHandler();
6667
firebaseWebApi.auth().signInWithEmailAndPassword('[email protected]', 'firebase')
68+
.then(() => console.log("User logged in"))
6769
.catch(err => {
6870
alert({
6971
title: "Login error",
@@ -79,7 +81,7 @@ export class HelloWorldModel extends Observable {
7981
if (!user || !user.email) {
8082
alert({
8183
title: "Can't fetch providers",
82-
message: "No user with emailaddress logged in.",
84+
message: "No user with an emailaddress logged in.",
8385
okButtonText: "OK, makes sense.."
8486
});
8587
return;
@@ -182,6 +184,18 @@ export class HelloWorldModel extends Observable {
182184
firebaseWebApi.database().ref(path).off("value");
183185
}
184186

187+
public doWebGetServerInfo(): void {
188+
const path = ".info";
189+
firebaseWebApi.database().ref(path)
190+
.once("value")
191+
.then(result => {
192+
this.set("path", path);
193+
this.set("key", result.key);
194+
this.set("value", JSON.stringify(result.val()));
195+
})
196+
.catch(error => console.log("doWebGetServerTime error: " + error));
197+
}
198+
185199
public doWebGetValueForCompanies(): void {
186200
const path = "/companies";
187201
firebaseWebApi.database().ref(path)
@@ -227,6 +241,22 @@ export class HelloWorldModel extends Observable {
227241
firebaseWebApi.database().ref(path).orderByChild(child);
228242
}
229243

244+
public doWebStoreCompanyByFirstCreatingKey(): void {
245+
const path = "companies",
246+
companyRef = firebaseWebApi.database().ref().child(path),
247+
newCompanyKey = companyRef.push().key,
248+
storeAtPath = `/${path}/${newCompanyKey}`,
249+
value = {
250+
name: `Company with key ${newCompanyKey}`,
251+
updateTs: firebase.ServerValue.TIMESTAMP
252+
};
253+
254+
firebaseWebApi.database().ref(storeAtPath).set(value).then(() => {
255+
this.set("path", storeAtPath);
256+
this.set("key", newCompanyKey);
257+
this.set("value", JSON.stringify(value));
258+
});
259+
}
230260

231261

232262
/***********************************************

docs/AUTHENTICATION.md

Lines changed: 120 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ All login functions below, as well as `getCurrentUser` return a 'User' object wi
2929

3030

3131
## Functions
32+
You can either use the Native API, or the Web API. It's just a matter of personal background or preference. Under the hood the implementations are identical.
33+
34+
You can also mix and match the API calls.
35+
36+
The relevant imports would be:
37+
38+
```typescript
39+
const firebase = require("nativescript-plugin-firebase");
40+
const firebaseWebApi = require("nativescript-plugin-firebase/app");
41+
```
3242

3343
### Listening to auth state changes
3444
As stated [here](https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user):
@@ -73,39 +83,56 @@ If - for some reason - you want more control over the listener you can use these
7383
```
7484

7585
### Get Current User
76-
Once the user is logged in you can retrieve the currently logged in user.
86+
Once the user is logged in you can retrieve the currently logged in user:
7787

78-
##### JavaScript
79-
```js
80-
firebase.getCurrentUser().then(
81-
function (result) {
82-
console.log(JSON.stringify(result));
83-
},
84-
function (errorMessage) {
85-
console.log(errorMessage);
86-
}
87-
);
88+
<details>
89+
<summary>Native API</summary>
90+
91+
```typescript
92+
firebase.getCurrentUser()
93+
.then(user => console.log("User uid: " + user.uid))
94+
.catch(error => console.log("Trouble in paradise: " + error));
8895
```
96+
</details>
97+
98+
<details>
99+
<summary>Web API</summary>
89100

90-
##### TypeScript
91101
```typescript
92-
firebase.getCurrentUser().then(user => {
93-
alert("User uid: " + user.uid);
94-
}, error => {
95-
alert("Trouble in paradise: " + error);
96-
});
102+
const user = firebaseWebApi.auth().currentUser;
97103
```
104+
</details>
98105

99106
### Fetch providers for email
100107
Want to know which auth providers are associated with an emailaddress?
101108

102-
##### TypeScript
109+
110+
<details>
111+
<summary>Native API</summary>
112+
103113
```typescript
104114
const emailAddress = "[email protected]";
105115
firebase.fetchProvidersForEmail(emailAddress).then((providers: Array<string>) => {
106116
console.log(`Providers for ${emailAddress}: ${JSON.stringify(providers)}`);
107117
});
108118
```
119+
</details>
120+
121+
<details>
122+
<summary>Web API</summary>
123+
124+
```js
125+
const user = firebaseWebApi.auth().currentUser;
126+
if (!user || !user.email) {
127+
console.log("Can't fetch providers; no user with an emailaddress logged in.");
128+
return;
129+
}
130+
131+
firebaseWebApi.auth().fetchProvidersForEmail(user.email)
132+
.then(result => console.log(`Providers for ${user.email}: ${JSON.stringify(result)}`))
133+
.catch(error => console.log("Fetch Providers for Email error: " + error));
134+
```
135+
</details>
109136

110137
### Updating a profile
111138
Pass in at least one of `displayName` and `photoURL`.
@@ -128,55 +155,68 @@ The logged in user will be updated, but for `getCurrentUser` to reflect the chan
128155
### Anonymous login
129156
Don't forget to enable anonymous login in your firebase instance.
130157

131-
##### JavaScript
132-
```js
133-
firebase.login({
134-
type: firebase.LoginType.ANONYMOUS
135-
}).then(
136-
function (result) {
137-
console.log(JSON.stringify(result));
138-
},
139-
function (errorMessage) {
140-
console.log(errorMessage);
141-
}
142-
);
158+
<details>
159+
<summary>Native API</summary>
160+
161+
```typescript
162+
firebase.login(
163+
{
164+
type: firebase.LoginType.ANONYMOUS
165+
})
166+
.then(user => console.log("User uid: " + user.uid))
167+
.catch(error => console.log("Trouble in paradise: " + error));
143168
```
169+
</details>
170+
171+
<details>
172+
<summary>Web API</summary>
144173

145-
##### TypeScript
146174
```typescript
147-
firebase.login({
148-
type: firebase.LoginType.ANONYMOUS
149-
}).then(user => {
150-
alert("User uid: " + user.uid);
151-
}, error => {
152-
alert("Trouble in paradise: " + error);
153-
});
175+
firebaseWebApi.auth().signInAnonymously()
176+
.then(() => console.log("User logged in"))
177+
.catch(err => console.log("Login error: " + JSON.stringify(err)));
154178
```
179+
</details>
155180

156181
### Email-Password login
157182
Don't forget to enable email-password login in your firebase instance.
158183

159-
```js
160-
firebase.login({
161-
type: firebase.LoginType.PASSWORD,
162-
passwordOptions: {
163-
164-
password: 'theirpassword'
165-
}
166-
}).then(
167-
function (result) {
168-
JSON.stringify(result);
169-
},
170-
function (errorMessage) {
171-
console.log(errorMessage);
172-
}
173-
);
184+
<details>
185+
<summary>Native API</summary>
186+
187+
```typescript
188+
firebase.login(
189+
{
190+
type: firebase.LoginType.PASSWORD,
191+
passwordOptions: {
192+
193+
password: 'theirpassword'
194+
}
195+
})
196+
.then(result => JSON.stringify(result))
197+
.catch(error => console.log(error));
198+
```
199+
</details>
200+
201+
<details>
202+
<summary>Web API</summary>
203+
204+
```typescript
205+
firebaseWebApi.auth().signInWithEmailAndPassword('[email protected]', 'firebase')
206+
.then(() => console.log("User logged in"))
207+
.catch(err => console.log("Login error: " + JSON.stringify(err)));
174208
```
209+
</details>
210+
175211

176212
#### Managing email-password accounts
213+
177214
##### Creating a Password account
178215
This may not work on an (Android) simulator. See #463.
179216

217+
<details>
218+
<summary>Native API</summary>
219+
180220
```js
181221
firebase.createUser({
182222
@@ -198,6 +238,19 @@ This may not work on an (Android) simulator. See #463.
198238
}
199239
);
200240
```
241+
</details>
242+
243+
<details>
244+
<summary>Web API</summary>
245+
246+
```typescript
247+
firebaseWebApi.auth().signOut()
248+
.then(() => console.log("Logout OK"))
249+
.catch(error => "Logout error: " + JSON.stringify(error));
250+
```
251+
</details>
252+
253+
201254

202255
#### Resetting a password
203256
```js
@@ -415,9 +468,23 @@ a Firebase auth token for the currently logged in user.
415468
### logout
416469
Shouldn't be more complicated than:
417470

471+
<details>
472+
<summary>Native API</summary>
473+
418474
```js
419475
firebase.logout();
420476
```
477+
</details>
478+
479+
<details>
480+
<summary>Web API</summary>
481+
482+
```js
483+
firebaseWebApi.auth().signOut()
484+
.then(() => console.log("Logout OK"))
485+
.catch(error => console.log("Logout error: " + JSON.stringify(error)));
486+
```
487+
</details>
421488

422489
### reauthenticate
423490
Some security-sensitive actions (deleting an account, changing a password) require that the user has recently signed in.

0 commit comments

Comments
 (0)