├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── google-services.json ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── net │ │ └── smallacademy │ │ └── authenticatorapp │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── net │ │ │ └── smallacademy │ │ │ └── authenticatorapp │ │ │ ├── EditProfile.java │ │ │ ├── Login.java │ │ │ ├── MainActivity.java │ │ │ └── Register.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_email_black_24dp.xml │ │ ├── ic_launcher_background.xml │ │ ├── ic_local_phone_black_24dp.xml │ │ ├── ic_person_black_24dp.xml │ │ └── walpaper.jpg │ │ ├── font │ │ └── aclonica.xml │ │ ├── layout │ │ ├── activity_edit_profile.xml │ │ ├── activity_login.xml │ │ ├── activity_main.xml │ │ └── activity_register.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── font_certs.xml │ │ ├── preloaded_fonts.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── net │ └── smallacademy │ └── authenticatorapp │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Firebase Email Authentication Example App 2 | 3 | In this application i have implemented firebase email authentication system. where user can create new account, login using the email and password. 4 | 5 | ## Features 6 | 1. Create New account with email 7 | 2. Saved user Data with FireStore 8 | 3. Forgot Password options 9 | 4. Verify User Email 10 | 11 | #This android app source code is provided for the Video Tutorial Series on youtube channel (SmallAcademy) You can watch Full tutorial series here. 12 | 13 | https://www.youtube.com/embed/videoseries?list=PLlGT4GXi8_8dDK5Y3KCxuKAPpil9V49rN 14 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.google.gms.google-services' 3 | 4 | android { 5 | compileSdkVersion 29 6 | buildToolsVersion "29.0.3" 7 | defaultConfig { 8 | applicationId "net.smallacademy.authenticatorapp" 9 | minSdkVersion 21 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | implementation fileTree(dir: 'libs', include: ['*.jar']) 25 | implementation 'androidx.appcompat:appcompat:1.1.0' 26 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 27 | implementation 'com.google.firebase:firebase-auth:19.3.0' 28 | implementation 'com.google.firebase:firebase-firestore:21.4.2' 29 | implementation 'com.google.firebase:firebase-storage:19.1.1' 30 | testImplementation 'junit:junit:4.13' 31 | androidTestImplementation 'androidx.test:runner:1.2.0' 32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 33 | implementation 'com.squareup.picasso:picasso:2.71828' 34 | } 35 | -------------------------------------------------------------------------------- /app/google-services.json: -------------------------------------------------------------------------------- 1 | { 2 | "project_info": { 3 | "project_number": "562743464498", 4 | "firebase_url": "https://authenticatorapp-2c565.firebaseio.com", 5 | "project_id": "authenticatorapp-2c565", 6 | "storage_bucket": "authenticatorapp-2c565.appspot.com" 7 | }, 8 | "client": [ 9 | { 10 | "client_info": { 11 | "mobilesdk_app_id": "1:562743464498:android:adc44e6c5e6e0d3b15222b", 12 | "android_client_info": { 13 | "package_name": "net.smallacademy.authenticatorapp" 14 | } 15 | }, 16 | "oauth_client": [ 17 | { 18 | "client_id": "562743464498-s3hn5ndhvh51r0nau8lk1bgc1d8fbvqu.apps.googleusercontent.com", 19 | "client_type": 1, 20 | "android_info": { 21 | "package_name": "net.smallacademy.authenticatorapp", 22 | "certificate_hash": "dfcfbc2c058958fe14455396f8d674241f74c5a7" 23 | } 24 | }, 25 | { 26 | "client_id": "562743464498-eal0p20k25ibearlj60scq92ge6l57rt.apps.googleusercontent.com", 27 | "client_type": 3 28 | } 29 | ], 30 | "api_key": [ 31 | { 32 | "current_key": "AIzaSyCbMJ2sSrC0tr4SgN3iE_eQMs7fFsMss1Q" 33 | } 34 | ], 35 | "services": { 36 | "appinvite_service": { 37 | "other_platform_oauth_client": [ 38 | { 39 | "client_id": "562743464498-eal0p20k25ibearlj60scq92ge6l57rt.apps.googleusercontent.com", 40 | "client_type": 3 41 | } 42 | ] 43 | } 44 | } 45 | } 46 | ], 47 | "configuration_version": "1" 48 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/net/smallacademy/authenticatorapp/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package net.smallacademy.authenticatorapp; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("net.smallacademy.authenticatorapp", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/net/smallacademy/authenticatorapp/EditProfile.java: -------------------------------------------------------------------------------- 1 | package net.smallacademy.authenticatorapp; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.appcompat.app.AppCompatActivity; 5 | 6 | import android.app.Activity; 7 | import android.content.Intent; 8 | import android.net.Uri; 9 | import android.os.Bundle; 10 | import android.provider.MediaStore; 11 | import android.util.Log; 12 | import android.view.View; 13 | import android.widget.Button; 14 | import android.widget.EditText; 15 | import android.widget.ImageView; 16 | import android.widget.Toast; 17 | 18 | import com.google.android.gms.tasks.OnFailureListener; 19 | import com.google.android.gms.tasks.OnSuccessListener; 20 | import com.google.firebase.auth.FirebaseAuth; 21 | import com.google.firebase.auth.FirebaseUser; 22 | import com.google.firebase.firestore.DocumentReference; 23 | import com.google.firebase.firestore.FirebaseFirestore; 24 | import com.google.firebase.storage.FirebaseStorage; 25 | import com.google.firebase.storage.StorageReference; 26 | import com.google.firebase.storage.UploadTask; 27 | import com.squareup.picasso.Picasso; 28 | 29 | import java.util.HashMap; 30 | import java.util.Map; 31 | 32 | public class EditProfile extends AppCompatActivity { 33 | 34 | public static final String TAG = "TAG"; 35 | EditText profileFullName,profileEmail,profilePhone; 36 | ImageView profileImageView; 37 | Button saveBtn; 38 | FirebaseAuth fAuth; 39 | FirebaseFirestore fStore; 40 | FirebaseUser user; 41 | StorageReference storageReference; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_edit_profile); 47 | 48 | Intent data = getIntent(); 49 | final String fullName = data.getStringExtra("fullName"); 50 | String email = data.getStringExtra("email"); 51 | String phone = data.getStringExtra("phone"); 52 | 53 | fAuth = FirebaseAuth.getInstance(); 54 | fStore = FirebaseFirestore.getInstance(); 55 | user = fAuth.getCurrentUser(); 56 | storageReference = FirebaseStorage.getInstance().getReference(); 57 | 58 | profileFullName = findViewById(R.id.profileFullName); 59 | profileEmail = findViewById(R.id.profileEmailAddress); 60 | profilePhone = findViewById(R.id.profilePhoneNo); 61 | profileImageView = findViewById(R.id.profileImageView); 62 | saveBtn = findViewById(R.id.saveProfileInfo); 63 | 64 | StorageReference profileRef = storageReference.child("users/"+fAuth.getCurrentUser().getUid()+"/profile.jpg"); 65 | profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { 66 | @Override 67 | public void onSuccess(Uri uri) { 68 | Picasso.get().load(uri).into(profileImageView); 69 | } 70 | }); 71 | 72 | profileImageView.setOnClickListener(new View.OnClickListener() { 73 | @Override 74 | public void onClick(View v) { 75 | Intent openGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 76 | startActivityForResult(openGalleryIntent,1000); 77 | } 78 | }); 79 | 80 | saveBtn.setOnClickListener(new View.OnClickListener() { 81 | @Override 82 | public void onClick(View v) { 83 | if(profileFullName.getText().toString().isEmpty() || profileEmail.getText().toString().isEmpty() || profilePhone.getText().toString().isEmpty()){ 84 | Toast.makeText(EditProfile.this, "One or Many fields are empty.", Toast.LENGTH_SHORT).show(); 85 | return; 86 | } 87 | 88 | final String email = profileEmail.getText().toString(); 89 | user.updateEmail(email).addOnSuccessListener(new OnSuccessListener() { 90 | @Override 91 | public void onSuccess(Void aVoid) { 92 | DocumentReference docRef = fStore.collection("users").document(user.getUid()); 93 | Map edited = new HashMap<>(); 94 | edited.put("email",email); 95 | edited.put("fName",profileFullName.getText().toString()); 96 | edited.put("phone",profilePhone.getText().toString()); 97 | docRef.update(edited).addOnSuccessListener(new OnSuccessListener() { 98 | @Override 99 | public void onSuccess(Void aVoid) { 100 | Toast.makeText(EditProfile.this, "Profile Updated", Toast.LENGTH_SHORT).show(); 101 | startActivity(new Intent(getApplicationContext(),MainActivity.class)); 102 | finish(); 103 | } 104 | }); 105 | Toast.makeText(EditProfile.this, "Email is changed.", Toast.LENGTH_SHORT).show(); 106 | } 107 | }).addOnFailureListener(new OnFailureListener() { 108 | @Override 109 | public void onFailure(@NonNull Exception e) { 110 | Toast.makeText(EditProfile.this, e.getMessage(), Toast.LENGTH_SHORT).show(); 111 | } 112 | }); 113 | 114 | 115 | } 116 | }); 117 | 118 | profileEmail.setText(email); 119 | profileFullName.setText(fullName); 120 | profilePhone.setText(phone); 121 | 122 | Log.d(TAG, "onCreate: " + fullName + " " + email + " " + phone); 123 | } 124 | 125 | 126 | @Override 127 | protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) { 128 | super.onActivityResult(requestCode, resultCode, data); 129 | if(requestCode == 1000){ 130 | if(resultCode == Activity.RESULT_OK){ 131 | Uri imageUri = data.getData(); 132 | 133 | //profileImage.setImageURI(imageUri); 134 | 135 | uploadImageToFirebase(imageUri); 136 | 137 | 138 | } 139 | } 140 | 141 | } 142 | 143 | private void uploadImageToFirebase(Uri imageUri) { 144 | // uplaod image to firebase storage 145 | final StorageReference fileRef = storageReference.child("users/"+fAuth.getCurrentUser().getUid()+"/profile.jpg"); 146 | fileRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener() { 147 | @Override 148 | public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 149 | fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { 150 | @Override 151 | public void onSuccess(Uri uri) { 152 | Picasso.get().load(uri).into(profileImageView); 153 | } 154 | }); 155 | } 156 | }).addOnFailureListener(new OnFailureListener() { 157 | @Override 158 | public void onFailure(@NonNull Exception e) { 159 | Toast.makeText(getApplicationContext(), "Failed.", Toast.LENGTH_SHORT).show(); 160 | } 161 | }); 162 | 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /app/src/main/java/net/smallacademy/authenticatorapp/Login.java: -------------------------------------------------------------------------------- 1 | package net.smallacademy.authenticatorapp; 2 | 3 | import android.content.DialogInterface; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.text.TextUtils; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.ProgressBar; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import androidx.annotation.NonNull; 15 | import androidx.appcompat.app.AlertDialog; 16 | import androidx.appcompat.app.AppCompatActivity; 17 | 18 | import com.google.android.gms.tasks.OnCompleteListener; 19 | import com.google.android.gms.tasks.OnFailureListener; 20 | import com.google.android.gms.tasks.OnSuccessListener; 21 | import com.google.android.gms.tasks.Task; 22 | import com.google.firebase.auth.AuthResult; 23 | import com.google.firebase.auth.FirebaseAuth; 24 | 25 | public class Login extends AppCompatActivity { 26 | EditText mEmail,mPassword; 27 | Button mLoginBtn; 28 | TextView mCreateBtn,forgotTextLink; 29 | ProgressBar progressBar; 30 | FirebaseAuth fAuth; 31 | 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_login); 37 | 38 | mEmail = findViewById(R.id.Email); 39 | mPassword = findViewById(R.id.password); 40 | progressBar = findViewById(R.id.progressBar); 41 | fAuth = FirebaseAuth.getInstance(); 42 | mLoginBtn = findViewById(R.id.loginBtn); 43 | mCreateBtn = findViewById(R.id.createText); 44 | forgotTextLink = findViewById(R.id.forgotPassword); 45 | 46 | 47 | mLoginBtn.setOnClickListener(new View.OnClickListener() { 48 | @Override 49 | public void onClick(View v) { 50 | 51 | String email = mEmail.getText().toString().trim(); 52 | String password = mPassword.getText().toString().trim(); 53 | 54 | if(TextUtils.isEmpty(email)){ 55 | mEmail.setError("Email is Required."); 56 | return; 57 | } 58 | 59 | if(TextUtils.isEmpty(password)){ 60 | mPassword.setError("Password is Required."); 61 | return; 62 | } 63 | 64 | if(password.length() < 6){ 65 | mPassword.setError("Password Must be >= 6 Characters"); 66 | return; 67 | } 68 | 69 | progressBar.setVisibility(View.VISIBLE); 70 | 71 | // authenticate the user 72 | 73 | fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener() { 74 | @Override 75 | public void onComplete(@NonNull Task task) { 76 | if(task.isSuccessful()){ 77 | Toast.makeText(Login.this, "Logged in Successfully", Toast.LENGTH_SHORT).show(); 78 | startActivity(new Intent(getApplicationContext(),MainActivity.class)); 79 | }else { 80 | Toast.makeText(Login.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show(); 81 | progressBar.setVisibility(View.GONE); 82 | } 83 | 84 | } 85 | }); 86 | 87 | } 88 | }); 89 | 90 | mCreateBtn.setOnClickListener(new View.OnClickListener() { 91 | @Override 92 | public void onClick(View v) { 93 | startActivity(new Intent(getApplicationContext(),Register.class)); 94 | } 95 | }); 96 | 97 | forgotTextLink.setOnClickListener(new View.OnClickListener() { 98 | @Override 99 | public void onClick(View v) { 100 | 101 | final EditText resetMail = new EditText(v.getContext()); 102 | final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext()); 103 | passwordResetDialog.setTitle("Reset Password ?"); 104 | passwordResetDialog.setMessage("Enter Your Email To Received Reset Link."); 105 | passwordResetDialog.setView(resetMail); 106 | 107 | passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 108 | @Override 109 | public void onClick(DialogInterface dialog, int which) { 110 | // extract the email and send reset link 111 | String mail = resetMail.getText().toString(); 112 | fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener() { 113 | @Override 114 | public void onSuccess(Void aVoid) { 115 | Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show(); 116 | } 117 | }).addOnFailureListener(new OnFailureListener() { 118 | @Override 119 | public void onFailure(@NonNull Exception e) { 120 | Toast.makeText(Login.this, "Error ! Reset Link is Not Sent" + e.getMessage(), Toast.LENGTH_SHORT).show(); 121 | } 122 | }); 123 | 124 | } 125 | }); 126 | 127 | passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 128 | @Override 129 | public void onClick(DialogInterface dialog, int which) { 130 | // close the dialog 131 | } 132 | }); 133 | 134 | passwordResetDialog.create().show(); 135 | 136 | } 137 | }); 138 | 139 | 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /app/src/main/java/net/smallacademy/authenticatorapp/MainActivity.java: -------------------------------------------------------------------------------- 1 | package net.smallacademy.authenticatorapp; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.appcompat.app.AlertDialog; 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | import android.app.Activity; 8 | import android.content.ContentResolver; 9 | import android.content.DialogInterface; 10 | import android.content.Intent; 11 | import android.net.Uri; 12 | import android.os.Bundle; 13 | import android.provider.MediaStore; 14 | import android.util.Log; 15 | import android.view.View; 16 | import android.webkit.MimeTypeMap; 17 | import android.widget.Button; 18 | import android.widget.EditText; 19 | import android.widget.ImageView; 20 | import android.widget.TextView; 21 | import android.widget.Toast; 22 | 23 | import com.google.android.gms.tasks.OnFailureListener; 24 | import com.google.android.gms.tasks.OnSuccessListener; 25 | import com.google.firebase.auth.FirebaseAuth; 26 | import com.google.firebase.auth.FirebaseUser; 27 | import com.google.firebase.firestore.DocumentReference; 28 | import com.google.firebase.firestore.DocumentSnapshot; 29 | import com.google.firebase.firestore.EventListener; 30 | import com.google.firebase.firestore.FirebaseFirestore; 31 | import com.google.firebase.firestore.FirebaseFirestoreException; 32 | import com.google.firebase.storage.FirebaseStorage; 33 | import com.google.firebase.storage.StorageReference; 34 | import com.google.firebase.storage.UploadTask; 35 | import com.squareup.picasso.Picasso; 36 | 37 | import javax.annotation.Nullable; 38 | 39 | public class MainActivity extends AppCompatActivity { 40 | private static final int GALLERY_INTENT_CODE = 1023 ; 41 | TextView fullName,email,phone,verifyMsg; 42 | FirebaseAuth fAuth; 43 | FirebaseFirestore fStore; 44 | String userId; 45 | Button resendCode; 46 | Button resetPassLocal,changeProfileImage; 47 | FirebaseUser user; 48 | ImageView profileImage; 49 | StorageReference storageReference; 50 | 51 | 52 | @Override 53 | protected void onCreate(Bundle savedInstanceState) { 54 | super.onCreate(savedInstanceState); 55 | setContentView(R.layout.activity_main); 56 | phone = findViewById(R.id.profilePhone); 57 | fullName = findViewById(R.id.profileName); 58 | email = findViewById(R.id.profileEmail); 59 | resetPassLocal = findViewById(R.id.resetPasswordLocal); 60 | 61 | profileImage = findViewById(R.id.profileImage); 62 | changeProfileImage = findViewById(R.id.changeProfile); 63 | 64 | 65 | fAuth = FirebaseAuth.getInstance(); 66 | fStore = FirebaseFirestore.getInstance(); 67 | storageReference = FirebaseStorage.getInstance().getReference(); 68 | 69 | StorageReference profileRef = storageReference.child("users/"+fAuth.getCurrentUser().getUid()+"/profile.jpg"); 70 | profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { 71 | @Override 72 | public void onSuccess(Uri uri) { 73 | Picasso.get().load(uri).into(profileImage); 74 | } 75 | }); 76 | 77 | resendCode = findViewById(R.id.resendCode); 78 | verifyMsg = findViewById(R.id.verifyMsg); 79 | 80 | 81 | userId = fAuth.getCurrentUser().getUid(); 82 | user = fAuth.getCurrentUser(); 83 | 84 | if(!user.isEmailVerified()){ 85 | verifyMsg.setVisibility(View.VISIBLE); 86 | resendCode.setVisibility(View.VISIBLE); 87 | 88 | resendCode.setOnClickListener(new View.OnClickListener() { 89 | @Override 90 | public void onClick(final View v) { 91 | 92 | user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener() { 93 | @Override 94 | public void onSuccess(Void aVoid) { 95 | Toast.makeText(v.getContext(), "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show(); 96 | } 97 | }).addOnFailureListener(new OnFailureListener() { 98 | @Override 99 | public void onFailure(@NonNull Exception e) { 100 | Log.d("tag", "onFailure: Email not sent " + e.getMessage()); 101 | } 102 | }); 103 | } 104 | }); 105 | } 106 | 107 | 108 | 109 | 110 | DocumentReference documentReference = fStore.collection("users").document(userId); 111 | documentReference.addSnapshotListener(this, new EventListener() { 112 | @Override 113 | public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) { 114 | if(documentSnapshot.exists()){ 115 | phone.setText(documentSnapshot.getString("phone")); 116 | fullName.setText(documentSnapshot.getString("fName")); 117 | email.setText(documentSnapshot.getString("email")); 118 | 119 | }else { 120 | Log.d("tag", "onEvent: Document do not exists"); 121 | } 122 | } 123 | }); 124 | 125 | 126 | resetPassLocal.setOnClickListener(new View.OnClickListener() { 127 | @Override 128 | public void onClick(View v) { 129 | 130 | final EditText resetPassword = new EditText(v.getContext()); 131 | 132 | final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext()); 133 | passwordResetDialog.setTitle("Reset Password ?"); 134 | passwordResetDialog.setMessage("Enter New Password > 6 Characters long."); 135 | passwordResetDialog.setView(resetPassword); 136 | 137 | passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 138 | @Override 139 | public void onClick(DialogInterface dialog, int which) { 140 | // extract the email and send reset link 141 | String newPassword = resetPassword.getText().toString(); 142 | user.updatePassword(newPassword).addOnSuccessListener(new OnSuccessListener() { 143 | @Override 144 | public void onSuccess(Void aVoid) { 145 | Toast.makeText(MainActivity.this, "Password Reset Successfully.", Toast.LENGTH_SHORT).show(); 146 | } 147 | }).addOnFailureListener(new OnFailureListener() { 148 | @Override 149 | public void onFailure(@NonNull Exception e) { 150 | Toast.makeText(MainActivity.this, "Password Reset Failed.", Toast.LENGTH_SHORT).show(); 151 | } 152 | }); 153 | } 154 | }); 155 | 156 | passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 157 | @Override 158 | public void onClick(DialogInterface dialog, int which) { 159 | // close 160 | } 161 | }); 162 | 163 | passwordResetDialog.create().show(); 164 | 165 | } 166 | }); 167 | 168 | changeProfileImage.setOnClickListener(new View.OnClickListener() { 169 | @Override 170 | public void onClick(View v) { 171 | // open gallery 172 | Intent i = new Intent(v.getContext(),EditProfile.class); 173 | i.putExtra("fullName",fullName.getText().toString()); 174 | i.putExtra("email",email.getText().toString()); 175 | i.putExtra("phone",phone.getText().toString()); 176 | startActivity(i); 177 | // 178 | 179 | } 180 | }); 181 | 182 | 183 | } 184 | 185 | 186 | 187 | 188 | public void logout(View view) { 189 | FirebaseAuth.getInstance().signOut();//logout 190 | startActivity(new Intent(getApplicationContext(),Login.class)); 191 | finish(); 192 | } 193 | 194 | 195 | } 196 | -------------------------------------------------------------------------------- /app/src/main/java/net/smallacademy/authenticatorapp/Register.java: -------------------------------------------------------------------------------- 1 | package net.smallacademy.authenticatorapp; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.text.TextUtils; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.ProgressBar; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import androidx.annotation.NonNull; 15 | import androidx.appcompat.app.AppCompatActivity; 16 | 17 | import com.google.android.gms.tasks.OnCompleteListener; 18 | import com.google.android.gms.tasks.OnFailureListener; 19 | import com.google.android.gms.tasks.OnSuccessListener; 20 | import com.google.android.gms.tasks.Task; 21 | import com.google.firebase.auth.AuthResult; 22 | import com.google.firebase.auth.FirebaseAuth; 23 | import com.google.firebase.auth.FirebaseUser; 24 | import com.google.firebase.firestore.DocumentReference; 25 | import com.google.firebase.firestore.FirebaseFirestore; 26 | 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | 30 | public class Register extends AppCompatActivity { 31 | public static final String TAG = "TAG"; 32 | EditText mFullName,mEmail,mPassword,mPhone; 33 | Button mRegisterBtn; 34 | TextView mLoginBtn; 35 | FirebaseAuth fAuth; 36 | ProgressBar progressBar; 37 | FirebaseFirestore fStore; 38 | String userID; 39 | 40 | @Override 41 | protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_register); 44 | 45 | mFullName = findViewById(R.id.fullName); 46 | mEmail = findViewById(R.id.Email); 47 | mPassword = findViewById(R.id.password); 48 | mPhone = findViewById(R.id.phone); 49 | mRegisterBtn= findViewById(R.id.registerBtn); 50 | mLoginBtn = findViewById(R.id.createText); 51 | 52 | fAuth = FirebaseAuth.getInstance(); 53 | fStore = FirebaseFirestore.getInstance(); 54 | progressBar = findViewById(R.id.progressBar); 55 | 56 | if(fAuth.getCurrentUser() != null){ 57 | startActivity(new Intent(getApplicationContext(),MainActivity.class)); 58 | finish(); 59 | } 60 | 61 | 62 | mRegisterBtn.setOnClickListener(new View.OnClickListener() { 63 | @Override 64 | public void onClick(View v) { 65 | final String email = mEmail.getText().toString().trim(); 66 | String password = mPassword.getText().toString().trim(); 67 | final String fullName = mFullName.getText().toString(); 68 | final String phone = mPhone.getText().toString(); 69 | 70 | if(TextUtils.isEmpty(email)){ 71 | mEmail.setError("Email is Required."); 72 | return; 73 | } 74 | 75 | if(TextUtils.isEmpty(password)){ 76 | mPassword.setError("Password is Required."); 77 | return; 78 | } 79 | 80 | if(password.length() < 6){ 81 | mPassword.setError("Password Must be >= 6 Characters"); 82 | return; 83 | } 84 | 85 | progressBar.setVisibility(View.VISIBLE); 86 | 87 | // register the user in firebase 88 | 89 | fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener() { 90 | @Override 91 | public void onComplete(@NonNull Task task) { 92 | if(task.isSuccessful()){ 93 | 94 | // send verification link 95 | 96 | FirebaseUser fuser = fAuth.getCurrentUser(); 97 | fuser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener() { 98 | @Override 99 | public void onSuccess(Void aVoid) { 100 | Toast.makeText(Register.this, "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show(); 101 | } 102 | }).addOnFailureListener(new OnFailureListener() { 103 | @Override 104 | public void onFailure(@NonNull Exception e) { 105 | Log.d(TAG, "onFailure: Email not sent " + e.getMessage()); 106 | } 107 | }); 108 | 109 | Toast.makeText(Register.this, "User Created.", Toast.LENGTH_SHORT).show(); 110 | userID = fAuth.getCurrentUser().getUid(); 111 | DocumentReference documentReference = fStore.collection("users").document(userID); 112 | Map user = new HashMap<>(); 113 | user.put("fName",fullName); 114 | user.put("email",email); 115 | user.put("phone",phone); 116 | documentReference.set(user).addOnSuccessListener(new OnSuccessListener() { 117 | @Override 118 | public void onSuccess(Void aVoid) { 119 | Log.d(TAG, "onSuccess: user Profile is created for "+ userID); 120 | } 121 | }).addOnFailureListener(new OnFailureListener() { 122 | @Override 123 | public void onFailure(@NonNull Exception e) { 124 | Log.d(TAG, "onFailure: " + e.toString()); 125 | } 126 | }); 127 | startActivity(new Intent(getApplicationContext(),MainActivity.class)); 128 | 129 | }else { 130 | Toast.makeText(Register.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show(); 131 | progressBar.setVisibility(View.GONE); 132 | } 133 | } 134 | }); 135 | } 136 | }); 137 | 138 | 139 | 140 | mLoginBtn.setOnClickListener(new View.OnClickListener() { 141 | @Override 142 | public void onClick(View v) { 143 | startActivity(new Intent(getApplicationContext(),Login.class)); 144 | } 145 | }); 146 | 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_email_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_local_phone_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_person_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/walpaper.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bikashthapa01/firebase-authentication-android/bfb976bfb09e3e83d8238fe3712b19200eb2486c/app/src/main/res/drawable/walpaper.jpg -------------------------------------------------------------------------------- /app/src/main/res/font/aclonica.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_edit_profile.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 22 | 23 | 32 | 33 | 46 | 47 | 60 | 61 |