Skip to content

Commit 7d6536e

Browse files
committed
standardise auth messages to match web sdk error messages
1 parent 3845a12 commit 7d6536e

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.firebase.auth.UserProfileChangeRequest;
2626
import com.google.firebase.auth.FacebookAuthProvider;
2727
import com.google.firebase.auth.FirebaseAuth;
28+
import com.google.firebase.auth.FirebaseAuthException;
2829
import com.google.firebase.auth.FirebaseUser;
2930
import com.google.firebase.auth.GetTokenResult;
3031
import com.google.firebase.auth.GoogleAuthProvider;
@@ -513,19 +514,15 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
513514

514515
private void userErrorCallback(Task task, final Callback onFail) {
515516
WritableMap error = Arguments.createMap();
516-
error.putInt("errorCode", task.getException().hashCode());
517-
error.putString("errorMessage", task.getException().getMessage());
518-
error.putString("allErrorMessage", task.getException().toString());
519-
517+
error.putString("code", ((FirebaseAuthException)task.getException()).getErrorCode());
518+
error.putString("message", task.getException().getMessage());
520519
onFail.invoke(error);
521520
}
522521

523522
private void userExceptionCallback(Exception ex, final Callback onFail) {
524523
WritableMap error = Arguments.createMap();
525-
error.putInt("errorCode", ex.hashCode());
526-
error.putString("errorMessage", ex.getMessage());
527-
error.putString("allErrorMessage", ex.toString());
528-
524+
error.putInt("code", ex.hashCode());
525+
error.putString("message", ex.getMessage());
529526
onFail.invoke(error);
530527
}
531528

lib/modules/auth.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NativeModules, NativeEventEmitter } from 'react-native';
33

44
import User from './user';
55
import { Base } from './base';
6-
import { promisify } from '../utils';
6+
import { promisify, toWebSDKErrorCode } from '../utils';
77

88
const FirestackAuth = NativeModules.FirestackAuth;
99
const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth);
@@ -75,7 +75,7 @@ export default class Auth extends Base {
7575
*/
7676
createUserWithEmailAndPassword(email: string, password: string): Promise<Object> {
7777
this.log.info('Creating user with email and password', email);
78-
return promisify('createUserWithEmail', FirestackAuth)(email, password);
78+
return promisify('createUserWithEmail', FirestackAuth, 'auth/')(email, password);
7979
}
8080

8181
/**
@@ -86,7 +86,7 @@ export default class Auth extends Base {
8686
*/
8787
signInWithEmailAndPassword(email: string, password: string): Promise<Object> {
8888
this.log.info('Signing in user with email and password', email);
89-
return promisify('signInWithEmail', FirestackAuth)(email, password);
89+
return promisify('signInWithEmail', FirestackAuth, 'auth/')(email, password);
9090
}
9191

9292
// TODO move user methods to User class
@@ -97,14 +97,14 @@ export default class Auth extends Base {
9797
* @return {Promise} A promise resolved upon completion
9898
*/
9999
updateEmail(email: string): Promise<Object> {
100-
return promisify('updateUserEmail', FirestackAuth)(email);
100+
return promisify('updateUserEmail', FirestackAuth, 'auth/')(email);
101101
}
102102

103103
/**
104104
* Send verification email to current user.
105105
*/
106106
sendEmailVerification(): Promise<Object> {
107-
return promisify('sendEmailVerification', FirestackAuth)();
107+
return promisify('sendEmailVerification', FirestackAuth, 'auth/')();
108108
}
109109

110110
/**
@@ -113,7 +113,7 @@ export default class Auth extends Base {
113113
* @return {Promise}
114114
*/
115115
updatePassword(password: string): Promise<Object> {
116-
return promisify('updateUserPassword', FirestackAuth)(password);
116+
return promisify('updateUserPassword', FirestackAuth, 'auth/')(password);
117117
}
118118

119119
/**
@@ -122,7 +122,7 @@ export default class Auth extends Base {
122122
* @return {Promise}
123123
*/
124124
updateProfile(updates: Object = {}): Promise<Object> {
125-
return promisify('updateUserProfile', FirestackAuth)(updates);
125+
return promisify('updateUserProfile', FirestackAuth, 'auth/')(updates);
126126
}
127127

128128
/**
@@ -139,47 +139,47 @@ export default class Auth extends Base {
139139
* @return {Promise} A promise resolved upon completion
140140
*/
141141
signInWithCredential(credential: CredentialType): Promise<Object> {
142-
return promisify('signInWithProvider', FirestackAuth)(credential.provider, credential.token, credential.secret);
142+
return promisify('signInWithProvider', FirestackAuth, 'auth/')(credential.provider, credential.token, credential.secret);
143143
}
144144

145145
/**
146146
* Re-authenticate a user with a third-party authentication provider
147147
* @return {Promise} A promise resolved upon completion
148148
*/
149149
reauthenticateUser(credential: CredentialType): Promise<Object> {
150-
return promisify('reauthenticateWithCredentialForProvider', FirestackAuth)(credential.provider, credential.token, credential.secret);
150+
return promisify('reauthenticateWithCredentialForProvider', FirestackAuth, 'auth/')(credential.provider, credential.token, credential.secret);
151151
}
152152

153153
/**
154154
* Sign a user in anonymously
155155
* @return {Promise} A promise resolved upon completion
156156
*/
157157
signInAnonymously(): Promise<Object> {
158-
return promisify('signInAnonymously', FirestackAuth)();
158+
return promisify('signInAnonymously', FirestackAuth, 'auth/')();
159159
}
160160

161161
/**
162162
* Send reset password instructions via email
163163
* @param {string} email The email to send password reset instructions
164164
*/
165165
sendPasswordResetEmail(email: string): Promise<Object> {
166-
return promisify('sendPasswordResetWithEmail', FirestackAuth)(email);
166+
return promisify('sendPasswordResetWithEmail', FirestackAuth, 'auth/')(email);
167167
}
168168

169169
/**
170170
* Delete the current user
171171
* @return {Promise}
172172
*/
173173
deleteUser(): Promise<Object> {
174-
return promisify('deleteUser', FirestackAuth)();
174+
return promisify('deleteUser', FirestackAuth, 'auth/')();
175175
}
176176

177177
/**
178178
* get the token of current user
179179
* @return {Promise}
180180
*/
181181
getToken(): Promise<Object> {
182-
return promisify('getToken', FirestackAuth)();
182+
return promisify('getToken', FirestackAuth, 'auth/')();
183183
}
184184

185185

@@ -188,15 +188,15 @@ export default class Auth extends Base {
188188
* @return {Promise}
189189
*/
190190
signOut(): Promise<Object> {
191-
return promisify('signOut', FirestackAuth)();
191+
return promisify('signOut', FirestackAuth, 'auth/')();
192192
}
193193

194194
/**
195195
* Get the currently signed in user
196196
* @return {Promise}
197197
*/
198198
getCurrentUser(): Promise<Object> {
199-
return promisify('getCurrentUser', FirestackAuth)();
199+
return promisify('getCurrentUser', FirestackAuth, 'auth/')();
200200
}
201201

202202
/**

lib/utils/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ const hasOwnProperty = Object.hasOwnProperty;
44
const DEFAULT_CHUNK_SIZE = 50;
55

66
// internal promise handler
7-
const _handler = (resolve, reject, err, resp) => {
7+
const _handler = (resolve, reject, errorPrefix, err, resp) => {
88
// resolve / reject after events etc
99
setImmediate(() => {
10-
if (err) return reject(err);
10+
if (err) return reject(errorPrefix ? { code: toWebSDKErrorCode(err.code), message: err.message } : err);
1111
return resolve(resp);
1212
});
1313
};
1414

15+
export function toWebSDKErrorCode(code, prefix) {
16+
return code.toLowerCase().replace('error_', prefix).replace(/_/g, '-');
17+
}
18+
1519
/**
1620
* Deep get a value from an object.
1721
* @website https://github.com/Salakar/deeps
@@ -131,13 +135,14 @@ export function noop(): void {
131135
* Wraps a native module method to support promises.
132136
* @param fn
133137
* @param NativeModule
138+
* @param errorPrefix
134139
*/
135-
export function promisify(fn: Function, NativeModule: Object): Function<Promise> {
140+
export function promisify(fn: Function, NativeModule: Object, errorPrefix): Function<Promise> {
136141
return (...args) => {
137142
return new Promise((resolve, reject) => {
138143
const _fn = typeof fn === 'function' ? fn : NativeModule[fn];
139144
if (!_fn || typeof _fn !== 'function') return reject(new Error('Missing function for promisify.'));
140-
return _fn.apply(NativeModule, [...args, _handler.bind(_handler, resolve, reject)]);
145+
return _fn.apply(NativeModule, [...args, _handler.bind(_handler, resolve, reject, errorPrefix)]);
141146
});
142147
};
143148
}

0 commit comments

Comments
 (0)