Last active
April 3, 2019 22:38
-
-
Save aminekarimii/17e248e642a8a984cbfabb25aaf6e903 to your computer and use it in GitHub Desktop.
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
package com.classroom.app1; | |
// implementation 'com.google.firebase:firebase-core:16.0.7' | |
/* | |
1- Edittext -> IDs Layouts | |
2- Register.java init Edit text -> findviewbyid | |
3- implement View.OnClickListener -> implement method (OnClick) definit lcode onclick if(i = R.id.view) | |
4- Button -> findviewbyid().setOnCliickListenner ... | |
5- copier coller lcode | |
*/ | |
<uses-permission android:name="android.permission.INTERNET" /> | |
import android.content.Intent; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.RequiresApi; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.classroom.app1.UI.HomeActivity; | |
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.FirebaseUser; | |
public class Login extends BaseActivity implements View.OnClickListener { | |
private static final String TAG = "EmailPassword"; | |
private EditText mEmailField; | |
private EditText mPasswordField; | |
// [START declare_auth] | |
private FirebaseAuth mAuth; | |
// [END declare_auth] | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.login); | |
// Views | |
mEmailField = findViewById(R.id.fieldEmail); | |
mPasswordField = findViewById(R.id.fieldPassword); | |
// Buttons | |
findViewById(R.id.emailSignInButton).setOnClickListener(this); | |
findViewById(R.id.emailCreateAccountButton).setOnClickListener(this); | |
// [START initialize_auth] | |
// Initialize Firebase Auth | |
mAuth = FirebaseAuth.getInstance(); | |
// [END initialize_auth] | |
} | |
// [START on_start_check_user] | |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
@Override | |
public void onStart() { | |
super.onStart(); | |
// Check if user is signed in (non-null) and update UI accordingly. | |
FirebaseUser currentUser = mAuth.getCurrentUser(); | |
updateUI(currentUser); | |
} | |
// [END on_start_check_user] | |
private void signIn(String email, String password) { | |
Log.d(TAG, "signIn:" + email); | |
if (!validateForm()) { | |
return; | |
} | |
showProgressDialog(); | |
// [START sign_in_with_email] | |
mAuth.signInWithEmailAndPassword(email, password) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
// Sign in success, update UI with the signed-in user's information | |
Log.d(TAG, "signInWithEmail:success"); | |
FirebaseUser user = mAuth.getCurrentUser(); | |
updateUI(user); | |
Intent my = new Intent(Login.this, HomeActivity.class); | |
startActivity(my); | |
finish(); | |
Toast.makeText(Login.this, "Authentication Success.", | |
Toast.LENGTH_SHORT).show(); | |
} else { | |
// If sign in fails, display a message to the user. | |
Log.w(TAG, "signInWithEmail:failure", task.getException()); | |
Toast.makeText(Login.this, "Authentication failed.", | |
Toast.LENGTH_SHORT).show(); | |
updateUI(null); | |
} | |
// [START_EXCLUDE] | |
hideProgressDialog(); | |
// [END_EXCLUDE] | |
} | |
}); | |
// [END sign_in_with_email] | |
} | |
private boolean validateForm() { | |
boolean valid = true; | |
String email = mEmailField.getText().toString(); | |
if (TextUtils.isEmpty(email)) { | |
mEmailField.setError("Requise"); | |
valid = false; | |
} else { | |
mEmailField.setError(null); | |
} | |
String password = mPasswordField.getText().toString(); | |
if (TextUtils.isEmpty(password)) { | |
mPasswordField.setError("Requise"); | |
valid = false; | |
} else { | |
mPasswordField.setError(null); | |
} | |
return valid; | |
} | |
private void updateUI(FirebaseUser user) { | |
hideProgressDialog(); | |
if (user != null) { | |
user.getEmail(); | |
user.isEmailVerified(); | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
int i = v.getId(); | |
if (i == R.id.emailCreateAccountButton) { | |
Intent my = new Intent(this,Register.class); | |
startActivity(my); | |
} else if (i == R.id.emailSignInButton) { | |
signIn(mEmailField.getText().toString(), mPasswordField.getText().toString()); | |
} | |
} | |
} |
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
package com.classroom.app1; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
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.FirebaseUser; | |
public class Register extends BaseActivity implements | |
View.OnClickListener { | |
private static final String TAG = "EmailPassword"; | |
private EditText mNomField; | |
private EditText mPrenomField; | |
private EditText mEmailField; | |
private EditText mPasswordField; | |
private EditText mRepasswordField; | |
// [START declare_auth] | |
private FirebaseAuth mAuth; | |
// [END declare_auth] | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.register); | |
// Views | |
mNomField = findViewById(R.id.fieldNom); | |
mPrenomField = findViewById(R.id.fieldPrenom); | |
mEmailField = findViewById(R.id.fieldEmail); | |
mPasswordField = findViewById(R.id.fieldPassword); | |
mRepasswordField = findViewById(R.id.fieldrePassword); | |
// Buttons | |
findViewById(R.id.emailCreateAccountButton).setOnClickListener(this); | |
// [START initialize_auth] | |
// Initialize Firebase Auth | |
mAuth = FirebaseAuth.getInstance(); | |
// [END initialize_auth] | |
} | |
// [START on_start_check_user] | |
@Override | |
public void onStart() { | |
super.onStart(); | |
// Check if user is signed in (non-null) and update UI accordingly. | |
FirebaseUser currentUser = mAuth.getCurrentUser(); | |
updateUI(currentUser); | |
} | |
// [END on_start_check_user] | |
private void createAccount(String email, String password) { | |
Log.d(TAG, "createAccount:" + email); | |
if (!validateForm()) { | |
return; | |
} | |
showProgressDialog(); | |
// [START create_user_with_email] | |
mAuth.createUserWithEmailAndPassword(email, password) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
// Sign in success, update UI with the signed-in user's information | |
Log.d(TAG, "createUserWithEmail:success"); | |
FirebaseUser user = mAuth.getCurrentUser(); | |
updateUI(user); | |
} else { | |
// If sign in fails, display a message to the user. | |
Log.w(TAG, "createUserWithEmail:failure", task.getException()); | |
Toast.makeText(Register.this, "createUser failed.", | |
Toast.LENGTH_SHORT).show(); | |
updateUI(null); | |
} | |
// [START_EXCLUDE] | |
hideProgressDialog(); | |
// [END_EXCLUDE] | |
} | |
}); | |
// [END create_user_with_email] | |
} | |
private boolean validateForm() { | |
boolean valid = true; | |
String email = mEmailField.getText().toString(); | |
if (TextUtils.isEmpty(email)) { | |
mEmailField.setError("Required"); | |
valid = false; | |
} else { | |
mEmailField.setError(null); | |
} | |
String password = mPasswordField.getText().toString(); | |
if (TextUtils.isEmpty(password)) { | |
mPasswordField.setError("Required"); | |
valid = false; | |
} else { | |
mPasswordField.setError(null); | |
} | |
return valid; | |
} | |
private void updateUI(FirebaseUser user) { | |
hideProgressDialog(); | |
if (user != null) { | |
user.getEmail(); | |
user.isEmailVerified(); | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
int i = v.getId(); | |
if (i == R.id.emailCreateAccountButton) { | |
createAccount(mEmailField.getText().toString(), mPasswordField.getText().toString()); | |
} | |
if(i == R.id.signedInButton){ | |
Intent my = new Intent(this,Login.class); | |
startActivity(my); | |
finish(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment