-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user creation on login with Firebase
- Loading branch information
1 parent
f41b711
commit a117252
Showing
11 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...a/com/mac/themac/GoogleOAuthActivity.java → .../themac/activity/GoogleOAuthActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters