AndroidのFirestoreでユーザー名とパスワードのみを使用して認証する

タシフ:

私はAndroidとFirebaseの新人です。ユーザーがサインインまたはサインアップしてクイズを開始するクイズアプリを作成しています。最後に、ユーザーと他のすべてのスコアが昇順に表示されます。ユーザーにサインアップして、ユーザー名とパスワードのみを使用してサインインさせたい。プロジェクトはfirebaseに接続されています。現時点では、サインインページとサインアップページの両方のユーザー資格情報は、文字列変数にのみ格納されます。カスタム認証とファイアストアを使用したい。公式ドキュメントfirebase.google.com/docs/auth/android/custom-auth?authuser=0に従っていますFirebaseで認証のステップ3と混同しています。サインアップフォームから資格情報を取得して、AuthOなどの認証サーバーに送信すると、トークンが提供されますか?資格情報を受け取った直後に、何をすべきかを段階的に教えてもらえますか?allow read, write: if request.auth.uid != null;firestore のセキュリティルール設定しました最初にそれをファイアストアに接続して認証プロセスの資格情報を保存し、最後にそれを使用する必要がありますか?

コードスニペットと役立つドキュメントへのリンクが提供されていると、非常に役立ちます。

サインインおよびサインアップページのJavaファイルを以下に示します。

ありがとうございました。

サインインページ

package com.guesstasif.guesswhat;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends AppCompatActivity {

public static String name;
public static String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    EditText nameText = findViewById(R.id.nameText);

    nameText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView nameText, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                name = nameText.getText().toString();
                handled = true;
            }
            return handled;
        }
    });

    EditText passwordText = findViewById(R.id.passwordText);

    passwordText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView passwordText, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                password = passwordText.getText().toString();
                handled = true;
            }
            return handled;
        }
    });

}

//OnClick of Start button
public void startQuizz(View view){
    Intent p1 = new Intent(this, qPage1.class);
    startActivity(p1);
}

//OnClick to register
public void register(View view){
    Intent signup = new Intent(this, Signup.class);
    startActivity(signup);
}

}

サインインページのxmlファイル

<?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:background="@drawable/sign_in"
tools:context=".MainActivity">

<EditText
    android:id="@+id/nameText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="181dp"
    android:width="300dp"
    android:backgroundTint="@color/colorPrimaryDark"
    android:hint="@string/name_input"
    android:imeActionId="10"
    android:imeOptions="actionSend"
    android:inputType="text"
    android:textColorLink="@color/colorPrimaryDark" />

<EditText
    android:id="@+id/passwordText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="255dp"
    android:width="300dp"
    android:backgroundTint="@color/colorPrimaryDark"
    android:hint="Enter your password"
    android:imeActionId="10"
    android:imeOptions="actionSend"
    android:inputType="textPassword"
    android:textColorLink="@color/colorPrimaryDark" />

<Button
    android:id="@+id/start_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="45dp"
    android:background="@color/colorPrimary"
    android:onClick="startQuizz"
    android:text="START"
    android:textColor="@android:color/background_light" />

<TextView
    android:id="@+id/registerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="140dp"
    android:clickable="true"
    android:onClick="register"
    android:text="Did not register?"
    android:textSize="24sp" />

サインアップページ

package com.guesstasif.guesswhat;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;


public class Signup extends AppCompatActivity {


    public static String signup_name;
    public static String signup_password;
    public static String signup_confirm_password;


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

        EditText sign_up_nameText = findViewById(R.id.sign_up_nameText);

        sign_up_nameText.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView sign_up_nameText, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == android.view.KeyEvent.KEYCODE_ENTER && event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
                    signup_name = sign_up_nameText.getText().toString();
                    handled = true;
                }
                return handled;
            }
        });


        EditText sign_up_passwordText = findViewById(R.id.sign_up_passwordText);

        sign_up_passwordText.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView sign_up_passwordText, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    signup_password = sign_up_passwordText.getText().toString();
                    handled = true;
                }
                return handled;
            }
        });

        EditText sign_up_confirm_passwordText = findViewById(R.id.sign_up_confirm_passwordText);

        sign_up_confirm_passwordText.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView sign_up_confirm_passwordText, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    signup_confirm_password = sign_up_confirm_passwordText.getText().toString();
                    handled = true;
                }
                return handled;
            }
        });

    }

    public void beginQuizz(View view){
        Intent p1 = new Intent(this, qPage1.class);
        startActivity(p1);
    }
}

サインアップ用のxmlファイル

<?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:background="@drawable/sign_in"
    tools:context=".Signup">

    <EditText
        android:id="@+id/sign_up_nameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="181dp"
        android:width="300dp"
        android:backgroundTint="@color/colorPrimaryDark"
        android:hint="@string/name_input"
        android:imeActionId="10"
        android:imeOptions="actionSend"
        android:inputType="text"
        android:textColorLink="@color/colorPrimaryDark" />

    <EditText
        android:id="@+id/sign_up_passwordText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="251dp"
        android:width="300dp"
        android:backgroundTint="@color/colorPrimaryDark"
        android:hint="Enter new password"
        android:imeActionId="10"
        android:imeOptions="actionSend"
        android:inputType="textPassword"
        android:textColorLink="@color/colorPrimaryDark" />

    <EditText
        android:id="@+id/sign_up_confirm_passwordText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="325dp"
        android:width="300dp"
        android:backgroundTint="@color/colorPrimaryDark"
        android:hint="Confirm password"
        android:imeActionId="10"
        android:imeOptions="actionSend"
        android:inputType="textPassword"
        android:textColorLink="@color/colorPrimaryDark" />

    <Button
        android:id="@+id/Done_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="57dp"
        android:onClick="beginQuizz"
        android:text="Start" />

</RelativeLayout>
PradyumanDixit:

ユーザーを登録するコードスニペットでは、Firebaseデータベースに詳細も保存する必要があります。これは、独自のカスタムユーザー名とパスワード認証アプリを作成するための最初のステップです。

このようなコードを使用して、Firebaseデータベース内にユーザー名とパスワードを保存できます。

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

ref.child("details").child("username").setValue(username);
ref.child("details").child("password").setValue(password);

これは、の値が保存されますusernamepassword、ユーザーがちょうど子供の下であなたのFirebaseデータベースにすると決めたことをdetails

これらの値を取得して、それらが正しいかどうか、つまりユーザーのログインアクセスを許可するかどうかを確認するには、次のようなコードを使用してユーザー名とパスワードを確認します。

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users");
                    databaseReference.orderByChild("username").equalTo(userNameEntered).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if(dataSnapshot.exists())
                                Toast.makeText(Main3Activity.this, "Username exists", Toast.LENGTH_SHORT).show();

                                //check your password in the same way and grant access if it exists too
                            else
                                // wrong details entered/ user does not exist

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });

詳細については、これらのリンクをご覧ください。

AndroidアプリケーションのFirebaseでメールではなくユーザー名を使用してログインする方法

メールの代わりにユーザー名認証

編集: FrankvanPuffelenが言ったように、パスワードをデータベースのように緩く保存するべきではありません。これは、そのようなアプローチを実装する方法を開始するための単なるデモコードです。

Firebaseデータベースに関連するそのようなセキュリティ問題の詳細については、こちらをご覧ください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

JDBCを使用してユーザー名とパスワードの認証を実装する

分類Dev

Spring Securityを使用して、ユーザー名とパスワードではなくIDとパスワードでユーザーを認証する方法

分類Dev

pkgcloudを使用してOpenStackでユーザー名とパスワードを認証する

分類Dev

ユーザー名とパスワードの基本認証でswagger-codegenを使用する

分類Dev

Spring Security認証は1つのユーザー名とパスワードでのみ機能します

分類Dev

シンボルを使用してAndroidでユーザー名とパスワードを検証する方法は?

分類Dev

安全でないmd5md5を使用して認証用のユーザー名とパスワードを取得する方法

分類Dev

ユーザー名とパスワードを使用して Google 認証トークンを取得する

分類Dev

root権限なしでPAMを使用してユーザー名/パスワードを認証する方法

分類Dev

Javaアプリからのユーザー名とパスワードの認証を使用してツイートを投稿する

分類Dev

ユーザー名とパスワードが必要なWebServiceを使用して、SpringSecurityでユーザーを認証する

分類Dev

特定のパスでユーザー名/パスワード認証を使用する

分類Dev

基本認証(ユーザー名とパスワード)を使用する場合のVSTS RESTAPIエラー

分類Dev

Laravel5.6-カスタムテーブルで組み込みの認証を使用する-ユーザー/パスワードを認証しない

分類Dev

AzureFunctionsを介してAzureADでユーザーを認証する(ユーザー名とパスワードを検証する)

分類Dev

kubernetes apiserviceを開始(再起動)し、ユーザー名とパスワードの認証を追加する方法

分類Dev

GitHub APIでの基本認証には、ユーザー名とパスワードを指定してInvoke-WebRequestを使用します

分類Dev

GitHub APIでの基本認証には、ユーザー名とパスワードを指定してInvoke-WebRequestを使用します

分類Dev

SpringSecurity認証ログインでユーザーが入力したユーザー名とパスワードの値を取得する方法

分類Dev

Cassandraでのユーザー名とパスワードによる認証

分類Dev

ユーザー名とパスワードのみを使用してGitHubにプッシュする

分類Dev

C ++用のユーザー名/パスワード認証を使用したThriftsasl

分類Dev

c#を使用してAzureADに対してユーザーのパスワードを認証する方法

分類Dev

Pythonを使用してSeleniumでユーザー名とパスワードを使用してプロキシを認証する方法

分類Dev

(ユーザーとパスワードの両方ではなく)Webページ認証に1つのフィールドのみを使用する方法

分類Dev

node.jsでユーザー名/パスワード認証を必要とするRESTAPIを使用する方法

分類Dev

Laravel認証で列のカスタム名、ユーザー名、パスワードを処理する方法

分類Dev

Angularでhttp $を使用する基本認証-ユーザー名/パスワードとgrant_typeを渡す

分類Dev

LDAPユーザーのユーザー名とパスワードを手動で確認する方法

Related 関連記事

  1. 1

    JDBCを使用してユーザー名とパスワードの認証を実装する

  2. 2

    Spring Securityを使用して、ユーザー名とパスワードではなくIDとパスワードでユーザーを認証する方法

  3. 3

    pkgcloudを使用してOpenStackでユーザー名とパスワードを認証する

  4. 4

    ユーザー名とパスワードの基本認証でswagger-codegenを使用する

  5. 5

    Spring Security認証は1つのユーザー名とパスワードでのみ機能します

  6. 6

    シンボルを使用してAndroidでユーザー名とパスワードを検証する方法は?

  7. 7

    安全でないmd5md5を使用して認証用のユーザー名とパスワードを取得する方法

  8. 8

    ユーザー名とパスワードを使用して Google 認証トークンを取得する

  9. 9

    root権限なしでPAMを使用してユーザー名/パスワードを認証する方法

  10. 10

    Javaアプリからのユーザー名とパスワードの認証を使用してツイートを投稿する

  11. 11

    ユーザー名とパスワードが必要なWebServiceを使用して、SpringSecurityでユーザーを認証する

  12. 12

    特定のパスでユーザー名/パスワード認証を使用する

  13. 13

    基本認証(ユーザー名とパスワード)を使用する場合のVSTS RESTAPIエラー

  14. 14

    Laravel5.6-カスタムテーブルで組み込みの認証を使用する-ユーザー/パスワードを認証しない

  15. 15

    AzureFunctionsを介してAzureADでユーザーを認証する(ユーザー名とパスワードを検証する)

  16. 16

    kubernetes apiserviceを開始(再起動)し、ユーザー名とパスワードの認証を追加する方法

  17. 17

    GitHub APIでの基本認証には、ユーザー名とパスワードを指定してInvoke-WebRequestを使用します

  18. 18

    GitHub APIでの基本認証には、ユーザー名とパスワードを指定してInvoke-WebRequestを使用します

  19. 19

    SpringSecurity認証ログインでユーザーが入力したユーザー名とパスワードの値を取得する方法

  20. 20

    Cassandraでのユーザー名とパスワードによる認証

  21. 21

    ユーザー名とパスワードのみを使用してGitHubにプッシュする

  22. 22

    C ++用のユーザー名/パスワード認証を使用したThriftsasl

  23. 23

    c#を使用してAzureADに対してユーザーのパスワードを認証する方法

  24. 24

    Pythonを使用してSeleniumでユーザー名とパスワードを使用してプロキシを認証する方法

  25. 25

    (ユーザーとパスワードの両方ではなく)Webページ認証に1つのフィールドのみを使用する方法

  26. 26

    node.jsでユーザー名/パスワード認証を必要とするRESTAPIを使用する方法

  27. 27

    Laravel認証で列のカスタム名、ユーザー名、パスワードを処理する方法

  28. 28

    Angularでhttp $を使用する基本認証-ユーザー名/パスワードとgrant_typeを渡す

  29. 29

    LDAPユーザーのユーザー名とパスワードを手動で確認する方法

ホットタグ

アーカイブ