Skip to content

Commit e30e0d4

Browse files
committed
start of refactor - will add a detailed commit log on PR.
1 parent 287c1f2 commit e30e0d4

File tree

12 files changed

+1194
-434
lines changed

12 files changed

+1194
-434
lines changed

android/.idea/gradle.xml

-11
This file was deleted.

android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ android {
2020

2121
dependencies {
2222
compile 'com.facebook.react:react-native:0.20.+'
23-
compile 'com.google.android.gms:play-services-base:9.8.0'
23+
compile 'com.google.android.gms:play-services-base:9.8.1'
2424

2525
compile 'com.google.firebase:firebase-core:9.8.0'
2626
compile 'com.google.firebase:firebase-auth:9.8.0'

android/src/main/java/io/fullstack/firestack/FirestackAuth.java

+452-280
Large diffs are not rendered by default.

lib/firestack.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class Firestack extends Singleton {
4949
delete instance.options.remoteConfig;
5050

5151
instance.configured = instance.options.configure || false;
52-
instance.auth = null;
52+
instance._auth = null;
5353

5454
instance.eventHandlers = {};
5555

@@ -104,6 +104,7 @@ export class Firestack extends Singleton {
104104
* idea or not (imperative vs. direct manipulation/proxy)
105105
*/
106106
auth() {
107+
console.log('auth GET');
107108
if (!this._auth) {
108109
this._auth = new Authentication(this);
109110
}

lib/modules/authentication.js

+151-38
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
1-
2-
import {NativeModules, NativeEventEmitter} from 'react-native';
1+
import { NativeModules, NativeEventEmitter } from 'react-native';
32
const FirestackAuth = NativeModules.FirestackAuth
43
const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth);
54

5+
66
import promisify from '../utils/promisify'
77
import { Base } from './base'
8+
import { default as User} from './user';
89

910
export class Authentication extends Base {
10-
constructor(firestack, options={}) {
11+
constructor(firestack, options = {}) {
1112
super(firestack, options);
13+
this._authResult = null;
14+
this.authenticated = false;
15+
this._user = null;
16+
17+
// always track auth changes internall so we can access them synchronously
18+
FirestackAuthEvt.addListener('listenForAuth', this._onAuthStateChanged.bind(this));
19+
FirestackAuth.listenForAuth();
1220
}
1321

14-
// Auth
15-
listenForAuth(callback) {
16-
this.log.info('Setting up listenForAuth callback');
17-
const sub = this._on('listenForAuth', callback, FirestackAuthEvt);
22+
/**
23+
* Internal auth changed listener
24+
* @param auth
25+
* @private
26+
*/
27+
_onAuthStateChanged(auth) {
28+
this._authResult = auth;
29+
this.authenticated = auth ? auth.authenticated || false : false
30+
if (auth && !this._user) this._user = new User(this, auth);
31+
else if (!auth && this._user) this._user = null;
32+
else this._user._updateValues(auth);
33+
}
34+
35+
/*
36+
* WEB API
37+
*/
38+
39+
/**
40+
* Listen for auth changes.
41+
* @param callback
42+
*/
43+
onAuthStateChanged(listener) {
44+
this.log.info('Creating onAuthStateChanged listener');
45+
const sub = this._on('listenForAuth', listener, FirestackAuthEvt);
1846
FirestackAuth.listenForAuth();
19-
this.log.info('Listening for auth...');
47+
this.log.info('Listening for onAuthStateChanged events...');
2048
return promisify(() => sub, FirestackAuth)(sub);
2149
}
2250

23-
unlistenForAuth() {
24-
this.log.info('Unlistening for auth');
51+
/**
52+
* Remove auth change listener
53+
* @param listener
54+
*/
55+
offAuthStateChanged(listener) {
56+
this.log.info('Removing onAuthStateChanged listener');
2557
this._off('listenForAuth');
2658
return promisify('unlistenForAuth', FirestackAuth)();
2759
}
@@ -32,8 +64,8 @@ export class Authentication extends Base {
3264
* @param {string} password The user's password
3365
* @return {Promise} A promise indicating the completion
3466
*/
35-
createUserWithEmail(email, password) {
36-
this.log.info('Creating user with email', email);
67+
createUserWithEmailAndPassword(email, password) {
68+
this.log.info('Creating user with email and password', email);
3769
return promisify('createUserWithEmail', FirestackAuth)(email, password);
3870
}
3971

@@ -43,19 +75,44 @@ export class Authentication extends Base {
4375
* @param {string} password The user's password
4476
* @return {Promise} A promise that is resolved upon completion
4577
*/
46-
signInWithEmail(email, password) {
78+
signInWithEmailAndPassword(email, password) {
79+
this.log.info('Signing in user with email and password', email);
4780
return promisify('signInWithEmail', FirestackAuth)(email, password)
4881
}
4982

83+
5084
/**
51-
* Sign the user in with a third-party authentication provider
52-
* @param {string} provider The name of the provider to use for login
53-
* @param {string} authToken The authToken granted by the provider
54-
* @param {string} authSecret The authToken secret granted by the provider
55-
* @return {Promise} A promise resolved upon completion
85+
* Update the current user's email
86+
* @param {string} email The user's _new_ email
87+
* @return {Promise} A promise resolved upon completion
5688
*/
57-
signInWithProvider(provider, authToken, authSecret) {
58-
return promisify('signInWithProvider', FirestackAuth)(provider, authToken, authSecret)
89+
updateEmail(email) {
90+
return promisify('updateUserEmail', FirestackAuth)(email);
91+
}
92+
93+
/**
94+
* Send verification email to current user.
95+
*/
96+
sendEmailVerification() {
97+
return promisify('sendEmailVerification', FirestackAuth)();
98+
}
99+
100+
/**
101+
* Update the current user's password
102+
* @param {string} email the new password
103+
* @return {Promise}
104+
*/
105+
updatePassword(password) {
106+
return promisify('updateUserPassword', FirestackAuth)(password);
107+
}
108+
109+
/**
110+
* Update the current user's profile
111+
* @param {Object} obj An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
112+
* @return {Promise}
113+
*/
114+
updateProfile(updates) {
115+
return promisify('updateUserProfile', FirestackAuth)(updates);
59116
}
60117

61118
/**
@@ -68,11 +125,14 @@ export class Authentication extends Base {
68125
}
69126

70127
/**
71-
* Sign a user in anonymously
72-
* @return {Promise} A promise resolved upon completion
128+
* Sign the user in with a third-party authentication provider
129+
* @param {string} provider The name of the provider to use for login
130+
* @param {string} authToken The authToken granted by the provider
131+
* @param {string} authSecret The authToken secret granted by the provider
132+
* @return {Promise} A promise resolved upon completion
73133
*/
74-
signInAnonymously() {
75-
return promisify('signInAnonymously', FirestackAuth)();
134+
signInWithProvider(provider, authToken, authSecret) {
135+
return promisify('signInWithProvider', FirestackAuth)(provider, authToken, authSecret)
76136
}
77137

78138
/**
@@ -86,22 +146,72 @@ export class Authentication extends Base {
86146
return promisify('reauthenticateWithCredentialForProvider', FirestackAuth)(provider, token, secret)
87147
}
88148

149+
89150
/**
90-
* Update the current user's email
91-
* @param {string} email The user's _new_ email
92-
* @return {Promise} A promise resolved upon completion
151+
* Sign a user in anonymously
152+
* @return {Promise} A promise resolved upon completion
93153
*/
94-
updateUserEmail(email) {
95-
return promisify('updateUserEmail', FirestackAuth)(email);
154+
signInAnonymously() {
155+
return promisify('signInAnonymously', FirestackAuth)();
96156
}
97157

158+
159+
/*
160+
* Old deprecated api stubs
161+
*/
162+
163+
98164
/**
99-
* Update the current user's password
100-
* @param {string} email the new password
101-
* @return {Promise}
165+
* @deprecated
166+
* @param args
102167
*/
103-
updatePassword(password) {
104-
return promisify('updateUserPassword', FirestackAuth)(password);
168+
listenForAuth(...args) {
169+
console.warn('Firestack: listenForAuth is now deprecated, please use onAuthStateChanged');
170+
this.onAuthStateChanged(...args);
171+
}
172+
173+
/**
174+
* @deprecated
175+
* @param args
176+
*/
177+
unlistenForAuth(...args) {
178+
console.warn('Firestack: unlistenForAuth is now deprecated, please use offAuthStateChanged');
179+
this.offAuthStateChanged(...args);
180+
}
181+
182+
/**
183+
* Create a user with the email/password functionality
184+
* @deprecated
185+
* @param {string} email The user's email
186+
* @param {string} password The user's password
187+
* @return {Promise} A promise indicating the completion
188+
*/
189+
createUserWithEmail(...args) {
190+
console.warn('Firestack: createUserWithEmail is now deprecated, please use createUserWithEmailAndPassword');
191+
this.createUserWithEmailAndPassword(...args);
192+
}
193+
194+
/**
195+
* Sign a user in with email/password
196+
* @deprecated
197+
* @param {string} email The user's email
198+
* @param {string} password The user's password
199+
* @return {Promise} A promise that is resolved upon completion
200+
*/
201+
signInWithEmail(...args) {
202+
console.warn('Firestack: signInWithEmail is now deprecated, please use signInWithEmailAndPassword');
203+
this.signInWithEmailAndPassword(...args);
204+
}
205+
206+
/**
207+
* Update the current user's email
208+
* @deprecated
209+
* @param {string} email The user's _new_ email
210+
* @return {Promise} A promise resolved upon completion
211+
*/
212+
updateUserEmail(...args) {
213+
console.warn('Firestack: updateUserEmail is now deprecated, please use updateEmail');
214+
this.updateEmail(...args);
105215
}
106216

107217
/**
@@ -119,6 +229,7 @@ export class Authentication extends Base {
119229
deleteUser() {
120230
return promisify('deleteUser', FirestackAuth)()
121231
}
232+
122233
/**
123234
* get the token of current user
124235
* @return {Promise}
@@ -129,11 +240,13 @@ export class Authentication extends Base {
129240

130241
/**
131242
* Update the current user's profile
243+
* @deprecated
132244
* @param {Object} obj An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
133245
* @return {Promise}
134246
*/
135-
updateUserProfile(obj) {
136-
return promisify('updateUserProfile', FirestackAuth)(obj);
247+
updateUserProfile(...args) {
248+
console.warn('Firestack: updateUserProfile is now deprecated, please use updateProfile');
249+
this.updateProfile(...args);
137250
}
138251

139252
/**
@@ -148,8 +261,8 @@ export class Authentication extends Base {
148261
* Get the currently signed in user
149262
* @return {Promise}
150263
*/
151-
getCurrentUser() {
152-
return promisify('getCurrentUser', FirestackAuth)();
264+
get currentUser() {
265+
return this._user;
153266
}
154267

155268
get namespace() {

lib/modules/base.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
*/
44
import Log from '../utils/log'
55

6-
import {NativeModules, NativeEventEmitter, AsyncStorage} from 'react-native';
6+
import { NativeModules, NativeEventEmitter, AsyncStorage } from 'react-native';
7+
import { default as EventEmitter } from './../utils/eventEmitter';
8+
79
const FirestackModule = NativeModules.Firestack;
810
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
911

1012
import promisify from '../utils/promisify'
1113

1214
let logs = {};
13-
export class Base {
14-
constructor(firestack, options={}) {
15+
export class Base extends EventEmitter {
16+
constructor(firestack, options = {}) {
17+
super();
1518
this.firestack = firestack;
1619
this.eventHandlers = {};
1720

@@ -88,13 +91,13 @@ export class ReferenceBase extends Base {
8891
super(firestack);
8992

9093
this.path = Array.isArray(path) ?
91-
path :
92-
(typeof path == 'string' ?
93-
[path] : []);
94+
path :
95+
(typeof path == 'string' ?
96+
[path] : []);
9497

9598
// sanitize path, just in case
9699
this.path = this.path
97-
.filter(str => str !== "" );
100+
.filter(str => str !== "");
98101
}
99102

100103
get key() {
@@ -110,4 +113,4 @@ export class ReferenceBase extends Base {
110113
}
111114
return pathStr;
112115
}
113-
}
116+
}

0 commit comments

Comments
 (0)