简体   繁体   中英

create user with email and password in firebase

I am going to create a chat application.So for example i have two EditText ,which take email and password.I want to create user authentication with email and password on Google Firebase . My code does not work.I can not find my error.Please give me a solution.

activity_main

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.atik_faysal.test.MainActivity">

    <EditText
        android:id="@+id/e_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="52dp"
        android:layout_marginStart="52dp"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/e_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:ems="10"
        android:inputType="textPassword"
        android:layout_below="@+id/e_email"
        android:layout_alignLeft="@+id/e_email"
        android:layout_alignStart="@+id/e_email" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="signIn"
        android:layout_marginTop="16dp"
        android:text="Log in"
        android:layout_below="@+id/e_password"
        android:layout_alignLeft="@+id/e_password"
        android:layout_alignStart="@+id/e_password" />

    <Button
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="registerUser"
        android:text="registerUser"
        android:layout_alignBaseline="@+id/login"
        android:layout_alignBottom="@+id/login"
        android:layout_toRightOf="@+id/login"
        android:layout_toEndOf="@+id/login"
        android:layout_marginLeft="27dp"
        android:layout_marginStart="27dp" />
</RelativeLayout>

MainActivity.java

    package com.example.atik_faysal.test;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.firebase.client.Firebase;
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 Activity {

    private EditText editTextEmail;
    private EditText editTextPassword;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firebaseAuth = FirebaseAuth.getInstance();
        editTextEmail = (EditText) findViewById(R.id.e_email);
        editTextPassword = (EditText) findViewById(R.id.e_password);
    }

    public void registerUser(View view){
        String email = editTextEmail.getText().toString().trim();
        String password  = editTextPassword.getText().toString().trim();
        if(TextUtils.isEmpty(email)){
            Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
            return;
        }
        if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
            return;
        }
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(MainActivity.this,"Registration Complete",Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
                        }
                    }
                });

    }

}

App level dependecy

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
    defaultConfig {
        applicationId "com.example.atik_faysal.test"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/mail.jar')
    compile 'com.google.firebase:firebase-database:11.4.0'
    compile 'com.google.firebase:firebase-auth:11.4.0'
    compile 'com.google.firebase:firebase-crash:11.4.0'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.firebase:firebase-client-android:2.3.1'
    compile 'com.cuboid:cuboidcirclebutton:1.0.3'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
    testCompile 'junit:junit:4.12'
    debugCompile 'com.android.support.constraint:constraint-layout:1.0.2'
}
apply plugin: 'com.google.gms.google-services'

How about you make your app accepts specific links, eg

<data android:pathPrefix="www.example.com" android:scheme="http"/>

Then you send a link, eg http://www.example.com/abcd ,to the email and the user should open it with the app, at the end the app will check if the link "opened link" is the same as the "sent link".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM