Skip to content
Prev Previous commit
Next Next commit
- JS: added EmailAuthProvider interface
    - .credential(email, password);

- Android/JS: Added auth().link(credential);

TODO other providers.

@chrisbianca - ios needs doing if you need this / have time =]
@Ehesp needs testing
  • Loading branch information
Salakar committed Jan 17, 2017
commit 145949bf1e3d8f269b0541831536e0e8c21051db
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.auth.EmailAuthProvider;


import io.fullstack.firestack.Utils;

Expand All @@ -51,7 +53,7 @@ public FirestackAuth(ReactApplicationContext reactContext) {
mReactContext = reactContext;
mAuth = FirebaseAuth.getInstance();

Log.d(TAG, "New FirestackAuth instance");
Log.d(TAG, "New FirestackAuth instance");
}

@Override
Expand Down Expand Up @@ -166,6 +168,43 @@ public void signInWithProvider(final String provider, final String authToken, fi
Utils.todoNote(TAG, "signInWithProvider", callback);
}

@ReactMethod
public void linkPassword(final String email, final String password, final Callback callback) {
FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
user
.linkWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (task.isSuccessful()) {
Log.d(TAG, "user linked with password credential");
userCallback(mAuth.getCurrentUser(), callback);
} else {
userErrorCallback(task, callback);
}
} catch (Exception ex) {
userExceptionCallback(ex, callback);
}
}
});
} else {
callbackNoUser(callback, true);
}
}

@ReactMethod
public void link(final String provider, final String authToken, final String authSecret, final Callback callback) {
if (provider.equals("password")) {
linkPassword(authToken, authSecret, callback);
} else
// TODO other providers
Utils.todoNote(TAG, "linkWithProvider", callback);
}

@ReactMethod
public void signInAnonymously(final Callback callback) {
Log.d(TAG, "signInAnonymously:called:");
Expand Down Expand Up @@ -514,7 +553,7 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {

private void userErrorCallback(Task task, final Callback onFail) {
WritableMap error = Arguments.createMap();
error.putString("code", ((FirebaseAuthException)task.getException()).getErrorCode());
error.putString("code", ((FirebaseAuthException) task.getException()).getErrorCode());
error.putString("message", task.getException().getMessage());
onFail.invoke(error);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/auth/Email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
credential(email, password) {
return {
token: email,
secret: password,
provider: 'password',
};
},
};
18 changes: 15 additions & 3 deletions lib/modules/auth.js → lib/modules/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @flow
import { NativeModules, NativeEventEmitter } from 'react-native';

import User from './user';
import { Base } from './base';
import { promisify, toWebSDKErrorCode } from '../utils';
import User from './../user';
import { Base } from './../base';
import EmailAuthProvider from './Email';
import { promisify } from './../../utils';

const FirestackAuth = NativeModules.FirestackAuth;
const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth);
Expand All @@ -22,6 +23,9 @@ export default class Auth extends Base {
this._authResult = null;
this.authenticated = false;

// attach auth providers
// TODO add missing providers
this.EmailAuthProvider = EmailAuthProvider;
// start listening straight away
// generally though the initial event fired will get ignored
// but this is ok as we fake it with the getCurrentUser below
Expand Down Expand Up @@ -125,6 +129,14 @@ export default class Auth extends Base {
return promisify('updateUserProfile', FirestackAuth, 'auth/')(updates);
}

/**
*
* @param credential
*/
link(credential: CredentialType) {
return promisify('link', FirestackAuth, 'auth/')(credential.provider, credential.token, credential.secret);
}

/**
* Sign the user in with a custom auth token
* @param {string} customToken A self-signed custom auth token.
Expand Down