Skip to content

Commit

Permalink
Add user creation on login with Firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
samirjaiswal committed Jun 30, 2015
1 parent f41b711 commit a117252
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 23 deletions.
9 changes: 4 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".TwitterOAuthActivity" />
<activity android:name="com.facebook.FacebookActivity" />

<activity android:name=".activity.TwitterOAuthActivity" />
<activity android:name="com.facebook.FacebookActivity" />
<activity
android:name=".LoginActivity"
android:name=".activity.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".GoogleOAuthActivity"
android:name=".activity.GoogleOAuthActivity"
android:label="@string/title_activity_google_oauth" >
</activity>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/mac/themac/TheMACApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void onCreate() {

//initialize Firebase
Firebase.setAndroidContext(this);
Firebase mFirebase = new Firebase("https://luminous-heat-7934.firebaseio.com/");
mFirebase = new Firebase( getString(R.string.firebase_url));

//initialize Facebook
FacebookSdk.sdkInitialize(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mac.themac;
package com.mac.themac.activity;

import android.app.Activity;
import android.content.Intent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mac.themac;
package com.mac.themac.activity;

import android.app.Activity;
import android.app.AlertDialog;
Expand All @@ -7,7 +7,6 @@
import android.content.IntentSender;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -21,8 +20,10 @@
import com.facebook.login.LoginManager;
import com.facebook.login.widget.LoginButton;
import com.firebase.client.AuthData;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
Expand All @@ -31,6 +32,9 @@
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import com.mac.themac.R;
import com.mac.themac.TheMACApplication;
import com.mac.themac.model.User;

import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -128,6 +132,10 @@ public class LoginActivity extends Activity implements
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* Create the Firebase ref that is used for all authentication with Firebase */
mFirebaseRef = ((TheMACApplication)getApplication()).getFirebaseRef();

/* Load the view and display it */
setContentView(R.layout.activity_login);

Expand Down Expand Up @@ -214,9 +222,6 @@ public void onClick(View view) {
***************************************/
mLoggedInStatusTextView = (TextView) findViewById(R.id.login_status);

/* Create the Firebase ref that is used for all authentication with Firebase */
mFirebaseRef = new Firebase(getResources().getString(R.string.firebase_url));

/* Setup the progress dialog that is displayed later when authenticating with Firebase */
mAuthProgressDialog = new ProgressDialog(this);
mAuthProgressDialog.setTitle("Loading");
Expand Down Expand Up @@ -367,6 +372,23 @@ private void setAuthenticatedUser(AuthData authData) {
if (name != null) {
mLoggedInStatusTextView.setText("Logged in as " + name + " (" + authData.getProvider() + ")");
}

final User loggedInUser = new User(authData);

//Retrieve firebase user, create one if doesn't exist
final Firebase loggedInUserRef = mFirebaseRef.child("users/" + loggedInUser.id());

loggedInUserRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
loggedInUser.updateFirebaseData(loggedInUserRef, dataSnapshot);
}

@Override
public void onCancelled(FirebaseError firebaseError) {
showErrorDialog(firebaseError.getMessage());
}
});
} else {
/* No authenticated user show all the login buttons */
mFacebookLoginButton.setVisibility(View.VISIBLE);
Expand All @@ -378,7 +400,7 @@ private void setAuthenticatedUser(AuthData authData) {
}
this.mAuthData = authData;
/* invalidate options menu to hide/show the logout button */
//supportInvalidateOptionsMenu();
invalidateOptionsMenu();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mac.themac;
package com.mac.themac.activity;

import android.app.Activity;
import android.content.Intent;
Expand All @@ -9,6 +9,8 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.mac.themac.R;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
Expand Down
116 changes: 116 additions & 0 deletions app/src/main/java/com/mac/themac/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.mac.themac.model;

import com.firebase.client.AuthData;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;

import java.util.Date;
import java.util.Map;

/**
* Created by Samir on 6/29/2015.
*/
public class User {

private String mId;
private String mProvider;
private String mName;
private String mEmail;
private boolean mIsAdmin;
private boolean mIsHead;
private Date mCreated;

public User(AuthData authData) {
mId = authData.getUid();
mProvider = authData.getProvider();
mCreated = new Date();
mIsAdmin = false;
mIsHead = false;

Map<String, Object> authKeyValueMappings = authData.getProviderData();

if(authKeyValueMappings.containsKey("displayName")){
mName = authKeyValueMappings.get("displayName").toString();
}
if(authKeyValueMappings.containsKey("email")){
mEmail = authKeyValueMappings.get("email").toString();
}

}

//Firebase specific "get" functions: Any public function with "get" prefix will be used
//for generating Firebase document db key-value pair
public String getProvider() {
return mProvider;
}

public String getName() {
return mName;
}

public String getEmail() {
return mEmail;
}

public boolean getIsAdmin() {
return mIsAdmin;
}

public boolean getIsHead() {
return mIsHead;
}

public Date getCreated() {
return mCreated;
}
/////////////////////////////////////////////////////////

public String id() {
return mId;
}

public void updateFirebaseData(Firebase loggedInUserRef, DataSnapshot dataSnapshot) {

if(dataSnapshot.getValue() == null)//user doesn't exist, add new
loggedInUserRef.setValue(this);
else {//User exists, update values if any changes
Iterable<DataSnapshot> userData = dataSnapshot.getChildren();

//Update any existing key-mapping
for (DataSnapshot userProp : userData) {
switch (userProp.getKey()) {
case "isAdmin":
if (userProp.getValue() == null || !userProp.getValue().equals(this.getIsAdmin())) {
loggedInUserRef.child("isAdmin").setValue(this.getIsAdmin());
}
break;
case "isHead":
if (userProp.getValue() == null || !userProp.getValue().equals(this.getIsHead())) {
loggedInUserRef.child("isHead").setValue(this.getIsHead());
}
break;
case "name":
if (userProp.getValue() == null || !userProp.getValue().equals(this.getName())) {
loggedInUserRef.child("name").setValue(this.getName());
}
break;
case "email":
if ((userProp.getValue() == null || !userProp.getValue().equals(this.getEmail()))) {
loggedInUserRef.child("email").setValue(this.getEmail());
}
break;
default:
break;
}
}

//Add new key-mapping if needed
if(!dataSnapshot.hasChild("name")){
loggedInUserRef.child("name").setValue(this.getName());
}
if(!dataSnapshot.hasChild("email")){
loggedInUserRef.child("email").setValue(this.getEmail());
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_google_oauth.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mac.themac.GoogleOAuthActivity">
tools:context="com.mac.themac.activity.GoogleOAuthActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.firebase.samples.logindemo.MainActivity">
tools:context="com.mac.themac.activity.LoginActivity">

<TextView
android:id="@+id/login_status"
Expand All @@ -23,7 +23,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/login_with_facebook" />
android:text="@string/login_with_facebook"
/>

<com.google.android.gms.common.SignInButton
android:id="@+id/login_with_google"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_google_oauth.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mac.themac.GoogleOAuthActivity">
tools:context="com.mac.themac.activity.GoogleOAuthActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
6 changes: 2 additions & 4 deletions app/src/main/res/menu/menu_login.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="com.mac.themac.LoginActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
xmlns:tools="http://schemas.android.com/tools" tools:context="com.mac.themac.activity.LoginActivity">
<item
android:id="@+id/action_logout"
android:title="@string/action_logout"
android:orderInCategory="100"
app:showAsAction="never" />
app:showAsAction="never"/>
</menu>
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

Expand Down

0 comments on commit a117252

Please sign in to comment.