Firebase SDK(使用电子邮件/密码进行身份验证)

【Firebase SDK(使用电子邮件/密码进行身份验证)】在前面的部分中, 我们了解了如何使用Firebase UI对电子邮件/密码或电子邮件链接/无密码进行身份验证。在本部分中, 我们将学习如何使用Firebase SDK进行身份验证。 Firebase SDK与Firebase UI略有相同。让我们看看使用Firebase SDK实施电子邮件/密码身份验证的步骤。
步骤1:
在第一步中, 我们必须执行以下准备步骤:

  1. 创建一个Android项目。
  2. 创建Firebase项目。
  3. 手动或通过Firebase协助将Firebase添加到Android项目或应用程序。
  4. 添加所需的库和JSON文件。
  5. 在Firebase控制台中启用电子邮件/密码登录方法。
第2步:
完成准备工作后, 我们将创建注册, 登录和注销活动。
Firebase SDK(使用电子邮件/密码进行身份验证)

文章图片
第三步:
在下一步中, 我们将为注册活动进行编码。代码如下:
寄存器
public class MainActivity extends AppCompatActivity {//Initialization sectionEditText email, pass, username; ProgressBar progressBar; String userid, Email; FirebaseAuth auth; //FirebaseAuth Instance@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); email=(EditText)findViewById(R.id.email); pass=(EditText)findViewById(R.id.password); username=(EditText)findViewById(R.id.username); progressBar=(ProgressBar)findViewById(R.id.progressBar); auth=FirebaseAuth.getInstance(); //Getting instance of FirebaseAuth}// Checking the current all state@Overridepublic void onStart() {super.onStart(); // if user logged in, go to sign-in screenif (auth.getCurrentUser() != null) {startActivity(new Intent(this, Sign_out.class)); finish(); }}@Overrideprotected void onResume(){super.onResume(); progressBar.setVisibility(View.GONE); }// Register button click public void onRegisterClicked(View view) {//Fetching dataString emailInput = email.getText().toString().trim(); String password = pass.getText().toString().trim(); final String user = username.getText().toString().trim(); //Validation check if (TextUtils.isEmpty(user)) {Toast.makeText(getApplicationContext(), "Enter username!", Toast.LENGTH_SHORT).show(); return; }if (TextUtils.isEmpty(emailInput)) {Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show(); return; }if (TextUtils.isEmpty(password)) {Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show(); return; }if (password.length() < 6) {Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show(); return; }progressBar.setVisibility(View.VISIBLE); //create user with email/password by adding complete listenerauth.createUserWithEmailAndPassword(emailInput, password).addOnCompleteListener(MainActivity.this, new OnCompleteListener< AuthResult> () {@Overridepublic void onComplete(@NonNull Task< AuthResult> task) {Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); // If sign-in fails, display a message to the user. If sign-in succeeds// the auth state listener will be notified and logic to handle the// signed in user can be handled in the listener.if (!task.isSuccessful()) {Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(), Toast.LENGTH_LONG).show(); Log.e("MyTag", task.getException().toString()); } else {startActivity(new Intent(MainActivity.this, Sign_out.class)); finish(); }}}); }//Login button click public void onLoginClicked(View view) {startActivity(new Intent(this, login.class)); }}

步骤4:
之后, 我们将分别为登录和注销活动进行编码。代码如下
登录
public class login extends AppCompatActivity {//Declaration sectionEditText email, password; ProgressBar progressBar; FirebaseAuth auth; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); email = (EditText) findViewById(R.id.email); password = (EditText) findViewById(R.id.password); progressBar = (ProgressBar) findViewById(R.id.progressBar); auth = FirebaseAuth.getInstance(); //Getting instances of FirebaseAuth }//Checking current user is logging or not @Overridepublic void onStart() {super.onStart(); // If user logged in, go to sign-in screenif (auth.getCurrentUser() != null) {startActivity(new Intent(this, Sign_out.class)); finish(); }}@Overrideprotected void onResume() {super.onResume(); progressBar.setVisibility(View.GONE); }//Login button clickpublic void loginButtonClicked(View view) {String Email = email.getText().toString(); final String pass = password.getText().toString(); //Validation sectionif (TextUtils.isEmpty(Email)) {Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show(); return; }if (TextUtils.isEmpty(pass)) {Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show(); return; }progressBar.setVisibility(View.VISIBLE); if (password.length() < 6) {password.setError("Should be greater than 6"); }//authenticate user with email/password by adding complete listener auth.signInWithEmailAndPassword(Email, pass).addOnCompleteListener(login.this, new OnCompleteListener< AuthResult> () {@Overridepublic void onComplete(@NonNull Task< AuthResult> task) {if (!task.isSuccessful()) {// there was an errorToast.makeText(login.this, "Authentication failed." + task.getException(), Toast.LENGTH_LONG).show(); Log.e("MyTag", task.getException().toString()); } else {Intent intent = new Intent(login.this, Sign_out.class); startActivity(intent); finish(); }}}); }}

登出
public class Sign_out extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_out); }public void onLogout(View view){FirebaseAuth.getInstance().signOut(); startActivity(new Intent(this, MainActivity.class)); }}

输出:
Firebase SDK(使用电子邮件/密码进行身份验证)

文章图片
Firebase SDK(使用电子邮件/密码进行身份验证)

文章图片

    推荐阅读