Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateAccount Activity #28

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
21 changes: 19 additions & 2 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res;file://$MODULE_DIR$/build/generated/res/google-services/debug;file://$MODULE_DIR$/build/generated/res/resValues/debug" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res;file://$MODULE_DIR$/build/generated/res/google-services/debug" />
<option name="TEST_RES_FOLDERS_RELATIVE_PATH" value="" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
</configuration>
Expand Down Expand Up @@ -97,7 +97,6 @@
<orderEntry type="library" scope="TEST" name="Gradle: net.sf.kxml:kxml2:2.3.0@jar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: com.squareup:javawriter:2.1.1@jar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: javax.inject:javax.inject:1@jar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: com.google.code.findbugs:jsr305:2.0.1@jar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: androidx.test.ext:junit:1.1.0@aar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: androidx.test.espresso:espresso-core:3.1.1@aar" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: androidx.test:runner:1.1.1@aar" level="project" />
Expand All @@ -109,14 +108,31 @@
<orderEntry type="library" name="Gradle: androidx.lifecycle:lifecycle-common:2.0.0@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.annotation:annotation:1.1.0@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.constraintlayout:constraintlayout-solver:1.1.3@jar" level="project" />
<orderEntry type="library" name="Gradle: io.grpc:grpc-protobuf-lite:1.12.0@jar" level="project" />
<orderEntry type="library" name="Gradle: com.google.protobuf:protobuf-lite:3.0.1@jar" level="project" />
<orderEntry type="library" name="Gradle: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava@jar" level="project" />
<orderEntry type="library" name="Gradle: io.grpc:grpc-okhttp:1.12.0@jar" level="project" />
<orderEntry type="library" name="Gradle: com.squareup.okhttp:okhttp:2.7.2@jar" level="project" />
<orderEntry type="library" name="Gradle: com.squareup.okio:okio:1.13.0@jar" level="project" />
<orderEntry type="library" name="Gradle: io.grpc:grpc-stub:1.12.0@jar" level="project" />
<orderEntry type="library" name="Gradle: io.grpc:grpc-core:1.12.0@jar" level="project" />
<orderEntry type="library" name="Gradle: io.grpc:grpc-context:1.12.0@jar" level="project" />
<orderEntry type="library" name="Gradle: com.google.code.gson:gson:2.7@jar" level="project" />
<orderEntry type="library" name="Gradle: com.google.guava:guava:20.0@jar" level="project" />
<orderEntry type="library" name="Gradle: com.google.errorprone:error_prone_annotations:2.1.2@jar" level="project" />
<orderEntry type="library" name="Gradle: com.google.code.findbugs:jsr305:3.0.0@jar" level="project" />
<orderEntry type="library" name="Gradle: io.opencensus:opencensus-contrib-grpc-metrics:0.11.0@jar" level="project" />
<orderEntry type="library" name="Gradle: io.opencensus:opencensus-api:0.11.0@jar" level="project" />
<orderEntry type="library" name="Gradle: androidx.appcompat:appcompat:1.0.2@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.recyclerview:recyclerview:1.1.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.firebase:firebase-auth:16.0.5@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.firebase:firebase-firestore:17.1.2@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.android.gms:play-services-flags:16.0.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.firebase:firebase-auth-interop:16.0.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.android.gms:play-services-base:16.0.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.firebase:firebase-common:16.0.3@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.android.gms:play-services-tasks:16.0.1@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.firebase:firebase-database-collection:16.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.android.gms:play-services-basement:16.1.0@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.legacy:legacy-support-v4:1.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.fragment:fragment:1.0.0@aar" level="project" />
Expand Down Expand Up @@ -146,5 +162,6 @@
<orderEntry type="library" name="Gradle: androidx.lifecycle:lifecycle-livedata-core:2.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.arch.core:core-runtime:2.0.0@aar" level="project" />
<orderEntry type="library" name="Gradle: androidx.constraintlayout:constraintlayout:1.1.3@aar" level="project" />
<orderEntry type="library" name="Gradle: com.google.firebase:protolite-well-known-types:16.0.0@aar" level="project" />
</component>
</module>
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-firestore:17.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Expand Down
212 changes: 193 additions & 19 deletions app/src/main/java/in/hackslash/messsy/onboarding/AccountsUtil.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,136 @@
package in.hackslash.messsy.onboarding;


public class AccountsUtil {
import android.util.Log;
import android.util.Patterns;

private onCompleteListener onCompleteListener;
import androidx.annotation.NonNull;

public AccountsUtil createAccount(User user, String password){
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

// TODO Check if a user is logged in and return ALREADY_LOGGED_IN if a logged in user is found
import java.util.HashMap;

// TODO Validate user here and return WRONG_INPUT if anything is wrong
public class AccountsUtil {

// TODO Check DB if the user exists and return DUPLICATE_USER if user exists
private onCompleteListener onCompleteListener;

public AccountsUtil createAccount(final User user, String password) {

String email = user.getEmail();
final FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
final FirebaseFirestore firestore = FirebaseFirestore.getInstance();
// Check if a user is logged in and return ALREADY_LOGGED_IN if a logged in user is found
if (currentUser != null) {
onCompleteListener.onComplete(Status.ALREADY_LOGGED_IN, user);
}
// Validate user here and return WRONG_INPUT if anything is wrong
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches() || password.length() < 6
|| user.getName().length()==0 || user.getRoomno().length()==0) {
onCompleteListener.onComplete(Status.WRONG_INPUT, user);
}
// Check DB if the user exists and return DUPLICATE_USER if user exists

// Create new account using createUserWithEmailAndPassword and return SUCCESS if successful
else {
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//user account created
String userid=currentUser.getUid();
String name=user.getName();
String rooomno=user.getRoomno();
String email=user.getEmail();
HashMap<String,Object> map=new HashMap<>();
map.put("name",name);
map.put("roomno",rooomno);
map.put("email",email);
firestore.collection("users").document(userid)
.set(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
// user data written on DB
onCompleteListener.onComplete(Status.SUCCESS, user);
}
}
});
} else {
String error_code = ((FirebaseAuthException) task.getException()).getErrorCode();
if (error_code.equals("ERROR_EMAIL_ALREADY_IN_USE")) {
onCompleteListener.onComplete(Status.DUPLICATE_USER, user);
}
}
}
});
}

// TODO Create new account using createUserWithEmailAndPassword and return SUCCESS if successful

// Leave as it is
return this;
}

public AccountsUtil loginUser(User user, String password) {

// TODO Check if a user is logged in and return ALREADY_LOGGED_IN if a logged in user is found

// TODO Validate user here and return WRONG_INPUT if anything is wrong

// TODO Check DB if the user exists and return USER_DOES_NOT_EXIST if user is not found

// TODO Login using the credentials and return SUCCESS if successful
public AccountsUtil loginUser(final User user, final String password) {

final String email = user.getEmail();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
final FirebaseFirestore firestore = FirebaseFirestore.getInstance();
// Check if a user is logged in and return ALREADY_LOGGED_IN if a logged in user is found
if (currentUser != null) {
onCompleteListener.onComplete(Status.ALREADY_LOGGED_IN, user);
}
// Validate user here and return WRONG_INPUT if anything is wrong
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches() || password.length() < 6) {
onCompleteListener.onComplete(Status.WRONG_INPUT, user);
}
// Check DB if the user exists and return USER_DOES_NOT_EXIST if user is not found

// Login using the credentials and return SUCCESS if successful

else{
FirebaseAuth.getInstance().signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
// user logged in
// checking user data in DB
String user_id=FirebaseAuth.getInstance().getCurrentUser().getUid();
firestore.collection("users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot doc=task.getResult();
if(doc.exists()){
//user exist
onCompleteListener.onComplete(Status.SUCCESS,user);
}
else{
onCompleteListener.onComplete(Status.USER_DOES_NOT_EXIST,user);
}
}
}
});
}
else{
Log.d("AccountsUtil", "onComplete: "+"login failed");
}
}
});
}

// Leave as it is
return this;
}

public AccountsUtil updateUser(User user, String password) {
public AccountsUtil updateUser(final User user, final String password) {

// TODO Check DB if the user exists and return USER_DOES_NOT_EXIST if user is not found

Expand All @@ -42,18 +139,95 @@ public AccountsUtil updateUser(User user, String password) {
// TODO Validate user here and return WRONG_INPUT if anything is wrong

// TODO Update the user data on DB as well as Auth if required and return SUCCESS

final FirebaseUser currentuser=FirebaseAuth.getInstance().getCurrentUser();
final String user_id=currentuser.getUid();
final FirebaseFirestore firestore=FirebaseFirestore.getInstance();
FirebaseFirestore.getInstance().collection("users").document(user_id).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot doc=task.getResult();
if(doc.exists()){
//user exist
String name_n=user.getName();
String roomno_n=user.getRoomno();
String email_n=user.getEmail();
String name_old=doc.getString("name");
String roomno_old=doc.getString("roomno");
String email_old=currentuser.getEmail();
if(roomno_n.length()==0 || name_n.length()==0){
onCompleteListener.onComplete(Status.WRONG_INPUT,user);
}
else if(roomno_n.equals(roomno_old) || name_n.equals(name_old)){
onCompleteListener.onComplete(Status.DUPLICATE_USER,user);
}
else{
HashMap<String,Object> map=new HashMap<>();
map.put("name",name_n);
map.put("roomno",roomno_n);
firestore.collection("users").document(user_id).update(map);
// if new email provided then updating it
if(!email_n.equals(email_old)){
currentuser.updateEmail(email_n).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.d("AccountsUtil", "email updated ");
}
}
});
}
if(password.length()>=6){
currentuser.updatePassword(password).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d("AccountsUtil", "password updated");
}
});
}
onCompleteListener.onComplete(Status.SUCCESS,user);
}
}
else{
onCompleteListener.onComplete(Status.USER_DOES_NOT_EXIST,user);
}
}
}
});
// Leave as it is
return this;

}

public AccountsUtil fetchUser(String userID) {
public AccountsUtil fetchUser(final String userID) {

// TODO Check DB if the user exists and return USER_DOES_NOT_EXIST if user is not found

// TODO Return the user in a User object if found with a SUCCESS Status

FirebaseFirestore.getInstance().collection("users").document(userID).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
DocumentSnapshot doc=task.getResult();
if(doc.exists()){
//user exist
String name=doc.getString("name");
String roomno=doc.getString("roomno");
String email=doc.getString("email");
User user=new User(email,name,roomno,userID);
onCompleteListener.onComplete(Status.SUCCESS,user);
}
else{
User user=new User();
onCompleteListener.onComplete(Status.USER_DOES_NOT_EXIST,user);
}
}
}
});

// Leave as it is
return this;
}
Expand Down
Loading