Skip to content

Commit

Permalink
Firebase support for password authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
collinjackson committed Jan 25, 2016
1 parent b93432e commit bfdcb69
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
1 change: 1 addition & 0 deletions sky/services/firebase/firebase.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ interface Firebase {
AddChildEventListener(ChildEventListener listener);
ObserveSingleEventOfType(EventType eventType) => (DataSnapshot snapshot);
AuthWithOAuthToken(string provider, string credentials) => (Error? error, AuthData? authData);
AuthWithPassword(string email, string password) => (Error? error, AuthData? authData);
SetValue(string jsonValue) => (Error? error);
};
58 changes: 35 additions & 23 deletions sky/services/firebase/src/org/domokit/firebase/FirebaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public FirebaseImpl(Context context) {
if (context != mContext)
com.firebase.client.Firebase.setAndroidContext(context);
mContext = context;
Log.v(TAG, "constructed");
}

public static void connectToService(Context context, Core core, MessagePipeHandle pipe) {
Expand Down Expand Up @@ -135,12 +134,11 @@ public void onConnectionError(MojoException e) {
}

@Override
public void observeSingleEventOfType(int eventType, ObserveSingleEventOfTypeResponse response) {
final ObserveSingleEventOfTypeResponse responseCopy = response;
public void observeSingleEventOfType(int eventType, final ObserveSingleEventOfTypeResponse response) {
mClient.addListenerForSingleValueEvent(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(com.firebase.client.DataSnapshot snapshot) {
responseCopy.call(toMojoSnapshot(snapshot));
response.call(toMojoSnapshot(snapshot));
}

@Override
Expand All @@ -151,35 +149,40 @@ public void onCancelled(FirebaseError error) {
}

@Override
public void authWithOAuthToken(String provider, String credentials, AuthWithOAuthTokenResponse response) {
final AuthWithOAuthTokenResponse responseCopy = response;
Log.v(TAG, "Authenticating " + mContext + " " + provider + " " + credentials);
public void authWithOAuthToken(String provider, String credentials, final AuthWithOAuthTokenResponse response) {
mClient.authWithOAuthToken(provider, credentials, new AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
org.chromium.mojom.firebase.AuthData mojoAuthData =
new org.chromium.mojom.firebase.AuthData();
mojoAuthData.uid = authData.getUid();
mojoAuthData.provider = authData.getProvider();
mojoAuthData.token = authData.getToken();
responseCopy.call(null, mojoAuthData);
}
public void onAuthenticationError(FirebaseError error) {
responseCopy.call(toMojoError(error), null);
}
@Override
public void onAuthenticated(AuthData authData) {
response.call(null, toMojoAuthData(authData));
}
public void onAuthenticationError(FirebaseError error) {
response.call(toMojoError(error), null);
}
});
}

@Override
public void authWithPassword(String email, String password, final AuthWithPasswordResponse response) {
mClient.authWithPassword(email, password, new AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
response.call(null, toMojoAuthData(authData));
}
public void onAuthenticationError(FirebaseError error) {
response.call(toMojoError(error), null);
}
});
}

@Override
public void setValue(String jsonValue, SetValueResponse response) {
final SetValueResponse responseCopy = response;
public void setValue(String jsonValue, final SetValueResponse response) {
try {
JSONObject root = new JSONObject(jsonValue);
Object value = toMap(root).get("value");
mClient.setValue(value, null, new CompletionListener() {
@Override
public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
responseCopy.call(toMojoError(error));
response.call(toMojoError(error));
}
});
} catch(JSONException e) {
Expand All @@ -188,7 +191,7 @@ public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) {
mojoError.code = -1;
mojoError.message = "setValue JSONException";
Log.e(TAG, "setValue JSONException", e);
responseCopy.call(mojoError);
response.call(mojoError);
}
}

Expand All @@ -211,6 +214,15 @@ org.chromium.mojom.firebase.Error toMojoError(FirebaseError error) {
return mojoError;
}

org.chromium.mojom.firebase.AuthData toMojoAuthData(AuthData authData) {
org.chromium.mojom.firebase.AuthData mojoAuthData =
new org.chromium.mojom.firebase.AuthData();
mojoAuthData.uid = authData.getUid();
mojoAuthData.provider = authData.getProvider();
mojoAuthData.token = authData.getToken();
return mojoAuthData;
}

// public domain code from https://gist.github.com/codebutler/2339666
@SuppressWarnings({ "rawtypes", "unchecked" })
static Map<String, Object> toMap(JSONObject object) throws JSONException {
Expand Down

0 comments on commit bfdcb69

Please sign in to comment.