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

Commit f8653ee

Browse files
#2 Add Login / Logout
1 parent bb76149 commit f8653ee

File tree

5 files changed

+145
-49
lines changed

5 files changed

+145
-49
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ If you can spare 41 seconds, please check this video of the [demo app](https://g
1111
### Use when
1212
* you need to store JSON data in the cloud,
1313
* you want to sync that data to other devices and platforms,
14+
* you want to optionally protect that data by having users log in,
1415
* you want to update clients at the moment the data changes (think chat and multiplayer games).
1516

1617
## Prerequisites
@@ -38,7 +39,7 @@ And here's the comprehensive list of supported functions:
3839
firebase.init({
3940
url: 'https://resplendent-fire-4211.firebaseio.com'
4041
}).then(
41-
function (result) {
42+
function (instance) {
4243
console.log("firebase.init done");
4344
},
4445
function (error) {
@@ -133,6 +134,54 @@ but if you only want to wipe everything at '/users', do this:
133134
firebase.remove("/users");
134135
```
135136

137+
### login
138+
v 1.1.0 of this plugin adds the capability to log your users in. Either anonymously or by email and password.
139+
You need to add support for those features in your Firebase instance at the 'Login & Auth' tab.
140+
141+
You can expect more login mechanisms to be added in the future.
142+
143+
#### Anonymous login
144+
```js
145+
firebase.login({
146+
// note that you need to enable anonymous login in your firebase instance
147+
type: firebase.loginType.ANONYMOUS
148+
}).then(
149+
function (result) {
150+
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
151+
JSON.stringify(result);
152+
},
153+
function (errorMessage) {
154+
console.log(errorMessage);
155+
}
156+
)
157+
};
158+
```
159+
160+
#### Password login
161+
```js
162+
firebase.login({
163+
// note that you need to enable email-password login in your firebase instance
164+
type: firebase.loginType.PASSWORD,
165+
166+
password: 'theirpassword'
167+
}).then(
168+
function (result) {
169+
// the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
170+
JSON.stringify(result);
171+
},
172+
function (errorMessage) {
173+
console.log(errorMessage);
174+
}
175+
)
176+
};
177+
```
178+
179+
### logout
180+
Shouldn't be more complicated than:
181+
182+
```js
183+
firebase.logout();
184+
```
136185

137186
## Pro tips
138187

firebase-common.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
var firebase = {};
22

3+
firebase.loginType = {
4+
ANONYMOUS: "anonymous",
5+
PASSWORD: "password"
6+
};
7+
38
firebase.instance = null;
49

10+
// this implementation is actually the same for both platforms, woohoo :)
11+
firebase.logout = function (arg) {
12+
return new Promise(function (resolve, reject) {
13+
try {
14+
instance.unauth();
15+
resolve();
16+
} catch (ex) {
17+
console.log("Error in firebase.logout: " + ex);
18+
reject(ex);
19+
}
20+
});
21+
};
22+
523
module.exports = firebase;

firebase.android.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,44 @@ firebase.init = function (arg) {
9090
var Firebase = com.firebase.client.Firebase;
9191
Firebase.setAndroidContext(appModule.android.context);
9292
instance = new Firebase(arg.url);
93-
resolve();
93+
resolve(instance);
9494
} catch (ex) {
9595
console.log("Error in firebase.init: " + ex);
9696
reject(ex);
9797
}
9898
});
9999
};
100100

101-
// TODO
102101
firebase.login = function (arg) {
103102
return new Promise(function (resolve, reject) {
104103
try {
105-
106104
var authorizer = new com.firebase.client.Firebase.AuthResultHandler({
107105
onAuthenticated: function (authData) {
108-
resolve(authData);
106+
resolve({
107+
uid: authData.getUid(),
108+
provider: authData.getProvider(),
109+
expiresAtUnixEpochSeconds: authData.getExpires(),
110+
profileImageURL: authData.getProviderData().get("profileImageURL"),
111+
token: authData.getToken()
112+
});
109113
},
110114
onAuthenticationError: function (firebaseError) {
111-
reject({
112-
error: firebaseError,
113-
errorMessage: firebaseError.message
114-
});
115+
reject(firebaseError.getMessage());
115116
}
116117
});
117118

118-
instance.authAnonymously(authorizer);
119+
var type = arg.type;
120+
if (type === firebase.loginType.ANONYMOUS) {
121+
instance.authAnonymously(authorizer);
122+
} else if (type === firebase.loginType.PASSWORD) {
123+
if (!arg.email || !arg.password) {
124+
reject("Auth type emailandpassword requires an email and password argument");
125+
} else {
126+
instance.authWithPassword(arg.email, arg.password, authorizer);
127+
}
128+
} else {
129+
reject ("Unsupported auth type: " + type);
130+
}
119131
} catch (ex) {
120132
console.log("Error in firebase.login: " + ex);
121133
reject(ex);
@@ -141,7 +153,7 @@ firebase.addChildEventListener = function (updateCallback, path) {
141153
}
142154
});
143155
instance.child(path).addChildEventListener(listener);
144-
resolve(listener);
156+
resolve();
145157
} catch (ex) {
146158
console.log("Error in firebase.addChildEventListener: " + ex);
147159
reject(ex);
@@ -158,13 +170,12 @@ firebase.addValueEventListener = function (updateCallback, path) {
158170
},
159171
onCancelled: function (firebaseError) {
160172
updateCallback({
161-
error: firebaseError,
162-
errorMessage: firebaseError.message
173+
error: firebaseError.getMessage()
163174
});
164175
}
165176
});
166177
instance.child(path).addValueEventListener(listener);
167-
resolve(listener);
178+
resolve();
168179
} catch (ex) {
169180
console.log("Error in firebase.addValueEventListener: " + ex);
170181
reject(ex);

firebase.ios.js

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,6 @@ firebase.toJsObject = function(objCObj) {
4141
return node;
4242
};
4343

44-
firebase.addChildEventListener = function (updateCallback, path) {
45-
return new Promise(function (resolve, reject) {
46-
try {
47-
var where = instance;
48-
if (path !== undefined) {
49-
where = instance.childByAppendingPath(path);
50-
}
51-
where.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
52-
updateCallback(firebase.getCallbackData('ChildAdded', snapshot));
53-
});
54-
where.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
55-
updateCallback(firebase.getCallbackData('ChildRemoved', snapshot));
56-
});
57-
where.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
58-
updateCallback(firebase.getCallbackData('ChildChanged', snapshot));
59-
});
60-
where.observeEventTypeWithBlock(FEventType.FEventTypeChildMoved, function (snapshot) {
61-
updateCallback(firebase.getCallbackData('ChildMoved', snapshot));
62-
});
63-
resolve();
64-
} catch (ex) {
65-
console.log("Error in firebase.addChildEventListener: " + ex);
66-
reject(ex);
67-
}
68-
});
69-
};
70-
7144
firebase.getCallbackData = function(type, snapshot) {
7245
return {
7346
type: type,
@@ -80,7 +53,7 @@ firebase.init = function (arg) {
8053
return new Promise(function (resolve, reject) {
8154
try {
8255
instance = new Firebase(arg.url);
83-
resolve();
56+
resolve(instance);
8457
} catch (ex) {
8558
console.log("Error in firebase.init: " + ex);
8659
reject(ex);
@@ -91,20 +64,66 @@ firebase.init = function (arg) {
9164
firebase.login = function (arg) {
9265
return new Promise(function (resolve, reject) {
9366
try {
94-
instance.authAnonymouslyWithCompletionBlock(function(error, result) {
67+
var onCompletion = function(error, authData) {
9568
if (error) {
96-
reject(error);
69+
reject(error.localizedDescription);
9770
} else {
98-
resolve(result);
71+
resolve({
72+
uid: authData.uid,
73+
provider: authData.provider,
74+
expiresAtUnixEpochSeconds: authData.expires,
75+
profileImageURL: authData.providerData["profileImageURL"],
76+
token: authData.token
77+
});
9978
}
100-
});
79+
};
80+
81+
var type = arg.type;
82+
if (type === firebase.loginType.ANONYMOUS) {
83+
instance.authAnonymouslyWithCompletionBlock(onCompletion);
84+
} else if (type === firebase.loginType.PASSWORD) {
85+
if (!arg.email || !arg.password) {
86+
reject("Auth type emailandpassword requires an email and password argument");
87+
} else {
88+
instance.authUserPasswordWithCompletionBlock(arg.email, arg.password, onCompletion);
89+
}
90+
} else {
91+
reject ("Unsupported auth type: " + type);
92+
}
10193
} catch (ex) {
10294
console.log("Error in firebase.login: " + ex);
10395
reject(ex);
10496
}
10597
});
10698
};
10799

100+
firebase.addChildEventListener = function (updateCallback, path) {
101+
return new Promise(function (resolve, reject) {
102+
try {
103+
var where = instance;
104+
if (path !== undefined) {
105+
where = instance.childByAppendingPath(path);
106+
}
107+
where.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
108+
updateCallback(firebase.getCallbackData('ChildAdded', snapshot));
109+
});
110+
where.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
111+
updateCallback(firebase.getCallbackData('ChildRemoved', snapshot));
112+
});
113+
where.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
114+
updateCallback(firebase.getCallbackData('ChildChanged', snapshot));
115+
});
116+
where.observeEventTypeWithBlock(FEventType.FEventTypeChildMoved, function (snapshot) {
117+
updateCallback(firebase.getCallbackData('ChildMoved', snapshot));
118+
});
119+
resolve();
120+
} catch (ex) {
121+
console.log("Error in firebase.addChildEventListener: " + ex);
122+
reject(ex);
123+
}
124+
});
125+
};
126+
108127
firebase.addValueEventListener = function (updateCallback, path) {
109128
return new Promise(function (resolve, reject) {
110129
try {
@@ -119,8 +138,7 @@ firebase.addValueEventListener = function (updateCallback, path) {
119138
},
120139
function (firebaseError) {
121140
updateCallback({
122-
error: firebaseError,
123-
errorMessage: firebaseError.localizedDescription
141+
error: firebaseError.localizedDescription
124142
});
125143
});
126144
resolve();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-plugin-firebase",
3-
"version": "1.1.0-dev",
3+
"version": "1.1.0",
44
"description" : "Fire. Base. Firebase!",
55
"main" : "firebase.js",
66
"nativescript": {

0 commit comments

Comments
 (0)