2017-01-15 4 views
0

Kontext: Benutzer öffnet App -> Klicks auf Registrieren -> E-Mail ausfüllen & Passwort -> Konto erstellt und kehrt zum Hauptbildschirm zurück.Konto registrieren Firebase über Android App

Benutzer kann sich erfolgreich anmelden, aber ich muss manuell einen Account in Firebase Browser erstellen, um dies zu tun. Ich möchte dies stattdessen über die App tun.

Hinweis: Ich bin ein Amateur in Android-Programmierung.

Aktivität Haupt

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:baselineAligned="false"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="163dp" 
     app:srcCompat="@drawable/snetech" 
     android:id="@+id/imageView" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textEmailAddress" 
     android:ems="10" 
     android:id="@+id/emailField" 
     android:hint="Email" 
     android:paddingTop="20dp" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:id="@+id/passwordField" 
     android:hint="Password" 
     android:fontFamily="sans-serif" 
     android:paddingTop="20dp" /> 

    <Button 
     android:text="Login" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/loginBtn" 
     android:paddingTop="20dp" /> 

    <Button 
     android:text="Register" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/registerBtn" /> 
</LinearLayout> 

MainActivity (Hauptbildschirm)

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AppCompatActivity; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
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; 

public class MainActivity extends AppCompatActivity { 

private EditText mEmailField; 
private EditText mPasswordField; 

private Button mLoginBtn; 

private FirebaseAuth mAuth; 

private FirebaseAuth.AuthStateListener mAuthListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mAuth = FirebaseAuth.getInstance(); 

    mEmailField = (EditText) findViewById(R.id.emailField); 
    mPasswordField = (EditText) findViewById(R.id.passwordField); 

    mLoginBtn = (Button) findViewById(R.id.loginBtn); 

    mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 

      if(firebaseAuth.getCurrentUser() != null) { 

       startActivity(new Intent(MainActivity.this, AccountActivity.class)); 

      } 
     } 
    }; 

    mLoginBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      startSignIn(); 

     } 
    }); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 

    mAuth.addAuthStateListener(mAuthListener); 
} 

private void startSignIn() { 

    String email = mEmailField.getText().toString(); 
    String password = mPasswordField.getText().toString(); 

    if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) { 

     Toast.makeText(MainActivity.this, "Fields are empty!", Toast.LENGTH_LONG).show(); 

    } else { 
     mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 

       if(!task.isSuccessful()){ 

        Toast.makeText(MainActivity.this, "Sign In Problem!", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
    } 



} 

}

FireApp

Register XML (Seite wo Benutzer erstellt Konto)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:baselineAligned="false" 
android:weightSum="1"> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="163dp" 
    app:srcCompat="@drawable/snetech" 
    android:id="@+id/imageView" /> 

<TextView 
    android:text="Register" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textRegister" 
    android:textAlignment="center" 
    android:textStyle="normal|bold" 
    android:typeface="normal" 
    android:textAppearance="@style/TextAppearance.AppCompat" 
    android:textSize="36sp" 
    android:layout_weight="0.08" /> 

<Space 
    android:layout_width="match_parent" 
    android:layout_height="64dp" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textEmailAddress" 
    android:ems="10" 
    android:id="@+id/emailAccount" 
    android:hint="Email" 
    android:paddingTop="20dp" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:ems="10" 
    android:id="@+id/passwordAccount" 
    android:hint="Password" 
    android:fontFamily="sans-serif" 
    android:paddingTop="20dp" /> 

<Button 
    android:text="Create Account" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/registerAccount" /> 

Wie sieht es jetzt:

Main Screen

Registration Page

Ich weiß, dass ich onCllockerer einige Tools von Firebase verwenden muss, aber ich weiß nicht, sie auf eine logische Weise zu codieren.

+0

Haben Sie die Firebase-Authentifizierungsdokumentation gelesen? –

+0

Ich versuche es zu verstehen. –

Antwort

0

Schauen Sie die offizielle Dokumentation auf: https://firebase.google.com/docs/auth/android/start/

mAuth.createUserWithEmailAndPassword(email, password) 
     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 
       Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()); 

       // 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(EmailPasswordActivity.this, R.string.auth_failed, 
          Toast.LENGTH_SHORT).show(); 
       } 

       // ... 
      } 
     }); 

Falls Sie sich fragen, auch nach der E-Mail, die isEmailVerified() false zurück bestätigt, es aufgrund eines Fehlers ist. Sie müssen den Benutzer entweder erneut authentifizieren oder das Firebase-SDK herunterstufen.

+0

Das beantwortet nicht, wie man einen Benutzer –

+0

@ cricket_007 anmeldet Ich bezog mich auf die offizielle Dokumentation, die tut. –

+0

Links zur Dokumentation sind keine Antworten. Bitte geben Sie den entsprechenden Code für die Frage an –

Verwandte Themen