Android에서 이메일 링크를 사용하여 Firebase에 인증

Firebase 인증을 사용하면 로그인 링크를 이메일로 전송해서 사용자가 바로 로그인하게 할 수 있습니다. 이 과정에서 사용자의 이메일 주소도 확인됩니다.

이메일로 로그인하는 경우 다음과 같은 많은 이점이 있습니다.

  • 편리한 가입 및 로그인
  • 여러 애플리케이션에서 비밀번호 재사용에 따른 위험이 적음(재사용하면 아무리 보안등급이 높은 비밀번호라 해도 보안이 취약해질 수 있음)
  • 사용자를 인증하는 동시에 사용자가 이메일 주소의 합법적인 소유자인지 확인 가능
  • 액세스 가능한 이메일 계정만 있으면 로그인 가능 전화번호 또는 소셜 미디어 계정 소유를 필요로 하지 않음
  • 사용자가 모바일 기기에서 번거롭게 비밀번호를 입력하거나 기억할 필요 없이 안전하게 로그인 가능
  • 이전에 이메일 식별자(비밀번호 또는 제휴)로 로그인한 기존 사용자는 이메일만 사용하여 로그인하도록 업그레이드 가능. 일례로 사용자가 비밀번호를 기억하지 못하더라도 비밀번호를 재설정하지 않고 계속 로그인할 수 있습니다.

시작하기 전에

Android 프로젝트 설정

  1. 아직 추가하지 않았다면 Android 프로젝트에 Firebase를 추가합니다.

  2. 모듈(앱 수준) Gradle 파일(일반적으로 <project>/<app-module>/build.gradle.kts 또는 <project>/<app-module>/build.gradle)에서 Firebase 인증 Android 라이브러리의 종속 항목을 추가합니다. 라이브러리 버전 관리 제어에는 Firebase Android BoM을 사용하는 것이 좋습니다.

    또한 Firebase 인증을 설정하는 과정에서 앱에 Google Play 서비스 SDK를 추가해야 합니다.

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // Add the dependency for the Firebase Authentication library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth-ktx")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0")
    }

    Firebase Android BoM을 사용하면 앱에서 항상 호환되는 Firebase Android 라이브러리 버전만 사용합니다.

    (대안) BoM을 사용하지 않고 Firebase 라이브러리 종속 항목을 추가합니다.

    Firebase BoM을 사용하지 않도록 선택한 경우에는 종속 항목 줄에 각 Firebase 라이브러리 버전을 지정해야 합니다.

    앱에서 여러 Firebase 라이브러리를 사용하는 경우 모든 버전이 호환되도록 BoM을 사용하여 라이브러리 버전을 관리하는 것이 좋습니다.

    dependencies {
        // Add the dependency for the Firebase Authentication library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth-ktx:22.1.2")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0")
    }

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // Add the dependency for the Firebase Authentication library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0")
    }

    Firebase Android BoM을 사용하면 앱에서 항상 호환되는 Firebase Android 라이브러리 버전만 사용합니다.

    (대안) BoM을 사용하지 않고 Firebase 라이브러리 종속 항목을 추가합니다.

    Firebase BoM을 사용하지 않도록 선택한 경우에는 종속 항목 줄에 각 Firebase 라이브러리 버전을 지정해야 합니다.

    앱에서 여러 Firebase 라이브러리를 사용하는 경우 모든 버전이 호환되도록 BoM을 사용하여 라이브러리 버전을 관리하는 것이 좋습니다.

    dependencies {
        // Add the dependency for the Firebase Authentication library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-auth:22.1.2")
    // Also add the dependency for the Google Play services library and specify its version implementation("com.google.android.gms:play-services-auth:20.7.0")
    }

이메일 링크로 사용자를 로그인 처리하려면 우선 Firebase 프로젝트에서 이메일 제공업체 및 이메일 링크 로그인 방법을 사용 설정해야 합니다.

  1. Firebase Console에서 인증 섹션을 엽니다.
  2. 로그인 방법 탭에서 이메일/비밀번호 제공업체를 사용 설정합니다. 이메일 링크 로그인을 사용하려면 이메일/비밀번호 로그인이 사용 설정되어야 합니다.
  3. 같은 섹션에서 이메일 링크(비밀번호가 없는 로그인) 로그인을 사용 설정합니다.
  4. 저장을 클릭합니다.

이 인증 과정을 시작하려면 사용자에게 이메일 주소를 제공하도록 요청하는 인터페이스를 제시하고 sendSignInLinkToEmail을 호출하여 Firebase가 사용자의 이메일에 인증 링크를 전송하도록 요청합니다.

  1. Firebase에 이메일 링크를 만드는 방법에 대한 안내를 제공하는 ActionCodeSettings 객체를 만듭니다. 다음 필드를 설정합니다.

    • url: 삽입할 딥 링크 및 함께 전달할 추가 상태입니다. 승인된 도메인의 Firebase Console 목록에서 링크의 도메인을 허용 목록에 추가해야 하며 로그인 방법 탭(인증 -> 로그인 방법)으로 이동하여 확인할 수 있습니다. 사용자 기기에 앱이 설치되어 있지 않고 앱을 설치할 수 없는 경우에 인증 링크는 사용자를 이 URL로 리디렉션합니다.
    • androidPackageNameIOSBundleId: Android 또는 Apple 기기에서 로그인 링크를 열 때 사용하는 앱입니다. 모바일 앱을 통해 이메일 작업 링크를 열기 위해 Firebase 동적 링크를 구성하는 방법에 대해 자세히 알아보세요.
    • handleCodeInApp: true로 설정합니다. 다른 대역 외 이메일 작업(비밀번호 재설정 및 이메일 인증)과 달리 이 로그인 작업은 항상 앱에서 완료해야 합니다. 그 이유는 인증 과정 마지막에 사용자가 로그인하고 사용자의 인증 상태를 앱에서 유지해야 하기 때문입니다.
    • dynamicLinkDomain: 프로젝트 하나에 커스텀 동적 링크 도메인이 여러 개 정의된 경우 지정된 모바일 앱을 통해 링크를 열 때 사용할 도메인을 지정합니다(예: example.page.link). 지정하지 않으면 첫 번째 도메인이 자동으로 선택됩니다.

    Kotlin+KTX

    val actionCodeSettings = actionCodeSettings {
        // URL you want to redirect back to. The domain (www.example.com) for this
        // URL must be whitelisted in the Firebase Console.
        url = "https://www.example.com/finishSignUp?cartId=1234"
        // This must be true
        handleCodeInApp = true
        setIOSBundleId("com.example.ios")
        setAndroidPackageName(
            "com.example.android",
            true, // installIfNotAvailable
            "12", // minimumVersion
        )
    }

    Java

    ActionCodeSettings actionCodeSettings =
            ActionCodeSettings.newBuilder()
                    // URL you want to redirect back to. The domain (www.example.com) for this
                    // URL must be whitelisted in the Firebase Console.
                    .setUrl("https://www.example.com/finishSignUp?cartId=1234")
                    // This must be true
                    .setHandleCodeInApp(true)
                    .setIOSBundleId("com.example.ios")
                    .setAndroidPackageName(
                            "com.example.android",
                            true, /* installIfNotAvailable */
                            "12"    /* minimumVersion */)
                    .build();

    ActionCodeSettings에 대해 자세히 알아보려면 이메일 작업의 상태 전달 섹션을 참조하세요.

  2. 사용자에게 이메일 주소 입력을 요청합니다.

  3. 사용자의 이메일에 인증 링크를 전송하고 사용자가 같은 기기에서 이메일 로그인을 완료할 경우를 대비해 사용자의 이메일을 저장합니다.

    Kotlin+KTX

    Firebase.auth.sendSignInLinkToEmail(email, actionCodeSettings)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Log.d(TAG, "Email sent.")
            }
        }

    Java

    FirebaseAuth auth = FirebaseAuth.getInstance();
    auth.sendSignInLinkToEmail(email, actionCodeSettings)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Email sent.");
                    }
                }
            });

보안 문제

의도하지 않은 사용자나 기기를 통해 로그인 링크가 사용되는 것을 방지하기 위해 Firebase 인증에서는 로그인 과정을 완료할 때 사용자의 이메일 주소를 입력해야 합니다. 로그인하려면 이 이메일 주소가 처음에 로그인 링크를 보낸 주소와 일치해야 합니다.

링크를 요청한 기기와 같은 기기에서 로그인 링크를 여는 사용자를 위해 이 과정을 간소화할 수 있습니다. 예를 들어 로그인 이메일을 보낼 때 SharedPreferences를 사용하여 사용자의 이메일 주소를 로컬에 저장하면 됩니다. 그런 다음 이 이메일 주소를 사용하여 이 과정을 완료합니다. 세션 인젝션의 위험이 있으므로 사용자의 이메일을 리디렉션 URL 매개변수에서 전달해서는 안 되며 재사용해서도 안 됩니다.

로그인이 완료되면 확인되지 않은 이전 로그인 메커니즘은 사용자에게서 모두 삭제되고 기존 세션은 무효화됩니다. 예를 들어 누군가가 이전에 같은 이메일과 비밀번호로 확인되지 않은 계정을 만든 경우 이 사용자의 비밀번호는 삭제됩니다. 이는 명의를 도용해 소유권을 주장하고 확인되지 않은 계정을 만들었던 사람이 이 이메일과 비밀번호로 다시 로그인하는 것을 방지하기 위해서입니다.

또한 중개 서버에서 링크를 가로채지 않도록 프로덕션 단계에서 HTTPS URL을 사용해야 합니다.

Android 앱에서 로그인 완료

Firebase 인증에서는 Firebase 동적 링크를 사용하여 모바일 기기로 이메일 링크를 보냅니다. 모바일 애플리케이션을 통해 로그인을 완료하는 경우 애플리케이션에서 수신 애플리케이션 링크를 감지하고 이 링크에 포함된 딥 링크를 파싱한 다음 로그인을 완료하도록 구성해야 합니다.

Firebase 인증은 모바일 애플리케이션에서 열릴 링크를 보낼 때 Firebase 동적 링크를 사용합니다. 이 기능을 사용하려면 Firebase Console에서 동적 링크를 구성해야 합니다.

  1. Firebase 동적 링크를 사용 설정합니다.

    1. Firebase Console에서 동적 링크 섹션을 엽니다.
    2. 동적 링크 약관에 아직 동의하지 않았으며 동적 링크 도메인도 만들지 않았다면 지금 동의하고 만듭니다.

      동적 링크 도메인을 이미 만들었다면 기록해 둡니다. 동적 링크 도메인의 형식은 일반적으로 다음 예와 같습니다.

      example.page.link

      수신 링크를 가로채도록 Apple 또는 Android 앱을 구성할 때 이 값이 필요합니다.

  2. Android 애플리케이션을 구성합니다.

    1. Android 애플리케이션에서 이 링크를 처리하려면 Firebase Console 프로젝트 설정에서 Android 패키지 이름을 지정해야 합니다. 또한 애플리케이션 인증서의 SHA-1 및 SHA-256을 제공해야 합니다.
    2. 이제 동적 링크 도메인을 추가하고 Android 앱이 올바르게 구성되었으므로 동적 링크가 런처 활동에서 시작하여 애플리케이션으로 리디렉션됩니다.
    3. 동적 링크를 특정 활동으로 리디렉션하려면 AndroidManifest.xml 파일에 인텐트 필터를 구성해야 합니다. 인텐트 필터에서 동적 링크 도메인 또는 이메일 작업 핸들러를 지정하여 이 작업을 수행할 수 있습니다. 기본적으로 이메일 작업 핸들러는 다음 예시와 같이 도메인에 호스팅됩니다.
      PROJECT_ID.firebaseapp.com/
    4. 주의사항:
      1. 인텐트 필터에서 actionCodeSettings에 설정한 URL을 지정하지 마세요.
      2. 동적 링크 도메인을 만들 때 단축 URL 링크를 만들었더라도 이 단축 URL은 전달되지 않습니다. android:pathPrefix 속성으로 이 단축 URL을 포착하도록 인텐트 필드를 구성하지 마세요. 즉, 애플리케이션의 다른 부분에서 다른 동적 링크를 포착할 수 없습니다. 하지만 링크에서 mode 쿼리 매개변수를 확인하여 수행하려는 작업을 알아보거나 isSignInWithEmailLink와 같은 SDK 메서드를 사용하여 앱에서 수신한 링크가 원하는 작업을 수행하는지 알아보는 것은 가능합니다.
    5. 동적 링크 수신에 대한 자세한 내용은 Android 동적 링크 수신 안내를 참조하세요.

위에 설명된 대로 링크를 수신하면 이메일 링크 인증을 위한 링크인지 확인하고 로그인을 완료합니다.

Kotlin+KTX

val auth = Firebase.auth
val intent = intent
val emailLink = intent.data.toString()

// Confirm the link is a sign-in with email link.
if (auth.isSignInWithEmailLink(emailLink)) {
    // Retrieve this from wherever you stored it
    val email = "someemail@domain.com"

    // The client SDK will parse the code from the link for you.
    auth.signInWithEmailLink(email, emailLink)
        .addOnCompleteListener { task ->
            if (task.isSuccessful) {
                Log.d(TAG, "Successfully signed in with email link!")
                val result = task.result
                // You can access the new user via result.getUser()
                // Additional user info profile *not* available via:
                // result.getAdditionalUserInfo().getProfile() == null
                // You can check if the user is new or existing:
                // result.getAdditionalUserInfo().isNewUser()
            } else {
                Log.e(TAG, "Error signing in with email link", task.exception)
            }
        }
}

Java

FirebaseAuth auth = FirebaseAuth.getInstance();
Intent intent = getIntent();
String emailLink = intent.getData().toString();

// Confirm the link is a sign-in with email link.
if (auth.isSignInWithEmailLink(emailLink)) {
    // Retrieve this from wherever you stored it
    String email = "someemail@domain.com";

    // The client SDK will parse the code from the link for you.
    auth.signInWithEmailLink(email, emailLink)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Successfully signed in with email link!");
                        AuthResult result = task.getResult();
                        // You can access the new user via result.getUser()
                        // Additional user info profile *not* available via:
                        // result.getAdditionalUserInfo().getProfile() == null
                        // You can check if the user is new or existing:
                        // result.getAdditionalUserInfo().isNewUser()
                    } else {
                        Log.e(TAG, "Error signing in with email link", task.getException());
                    }
                }
            });
}

Apple 애플리케이션에서 이메일 링크를 사용한 로그인 처리 방법을 자세히 알아보려면 Apple 플랫폼 가이드를 참조하세요.

웹 애플리케이션에서 이메일 링크를 사용한 로그인 처리 방법을 알아보려면 웹 가이드를 참조하세요.

기존 사용자에게도 이 인증 방법을 연결할 수 있습니다. 예를 들어 이전에 전화번호 등 다른 제공업체로 인증된 사용자의 경우 기존 사용자 계정에 이 로그인 방법을 추가할 수 있습니다.

이 경우 작업 뒷부분이 달라집니다.

Kotlin+KTX

// Construct the email link credential from the current URL.
val credential = EmailAuthProvider.getCredentialWithLink(email, emailLink)

// Link the credential to the current user.
Firebase.auth.currentUser!!.linkWithCredential(credential)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG, "Successfully linked emailLink credential!")
            val result = task.result
            // You can access the new user via result.getUser()
            // Additional user info profile *not* available via:
            // result.getAdditionalUserInfo().getProfile() == null
            // You can check if the user is new or existing:
            // result.getAdditionalUserInfo().isNewUser()
        } else {
            Log.e(TAG, "Error linking emailLink credential", task.exception)
        }
    }

Java

// Construct the email link credential from the current URL.
AuthCredential credential =
        EmailAuthProvider.getCredentialWithLink(email, emailLink);

// Link the credential to the current user.
auth.getCurrentUser().linkWithCredential(credential)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "Successfully linked emailLink credential!");
                    AuthResult result = task.getResult();
                    // You can access the new user via result.getUser()
                    // Additional user info profile *not* available via:
                    // result.getAdditionalUserInfo().getProfile() == null
                    // You can check if the user is new or existing:
                    // result.getAdditionalUserInfo().isNewUser()
                } else {
                    Log.e(TAG, "Error linking emailLink credential", task.getException());
                }
            }
        });

민감한 작업을 실행하기 전에 이메일 링크 사용자를 재인증하는 경우에도 사용할 수 있습니다.

Kotlin+KTX

// Construct the email link credential from the current URL.
val credential = EmailAuthProvider.getCredentialWithLink(email, emailLink)

// Re-authenticate the user with this credential.
Firebase.auth.currentUser!!.reauthenticateAndRetrieveData(credential)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            // User is now successfully reauthenticated
        } else {
            Log.e(TAG, "Error reauthenticating", task.exception)
        }
    }

Java

// Construct the email link credential from the current URL.
AuthCredential credential =
        EmailAuthProvider.getCredentialWithLink(email, emailLink);

// Re-authenticate the user with this credential.
auth.getCurrentUser().reauthenticateAndRetrieveData(credential)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // User is now successfully reauthenticated
                } else {
                    Log.e(TAG, "Error reauthenticating", task.getException());
                }
            }
        });

하지만 원래의 사용자가 로그인하지 않은 다른 기기에서 인증 과정이 종료되면 이 인증 과정이 완료되지 않을 수 있습니다. 이러한 경우 같은 기기에서 링크를 열도록 사용자에게 오류를 표시할 수 있습니다. 링크에 일부 상태를 전달하여 작업 유형 및 사용자 UID에 대한 정보를 표시할 수 있습니다.

이메일/비밀번호 로그인과 이메일 링크 기반 로그인을 모두 지원하는 경우 비밀번호 또는 링크 사용자에 맞춰 로그인 방법을 구별하려면 fetchSignInMethodsForEmail을 사용합니다. 이는 사용자에게 먼저 이메일 입력을 요청한 다음 로그인 방법을 제시하는 다음과 같은 식별자 우선 인증 과정에 유용합니다.

Kotlin+KTX

Firebase.auth.fetchSignInMethodsForEmail(email)
    .addOnSuccessListener { result ->
        val signInMethods = result.signInMethods!!
        if (signInMethods.contains(EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD)) {
            // User can sign in with email/password
        } else if (signInMethods.contains(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD)) {
            // User can sign in with email/link
        }
    }
    .addOnFailureListener { exception ->
        Log.e(TAG, "Error getting sign in methods for user", exception)
    }

Java

auth.fetchSignInMethodsForEmail(email)
        .addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
            @Override
            public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
                if (task.isSuccessful()) {
                    SignInMethodQueryResult result = task.getResult();
                    List<String> signInMethods = result.getSignInMethods();
                    if (signInMethods.contains(EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD)) {
                        // User can sign in with email/password
                    } else if (signInMethods.contains(EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD)) {
                        // User can sign in with email/link
                    }
                } else {
                    Log.e(TAG, "Error getting sign in methods for user", task.getException());
                }
            }
        });

위에 설명된 대로 이메일/비밀번호 및 이메일/링크는 서로 다른 로그인 방법을 사용하지만 같은 EmailAuthProvider(같은 PROVIDER_ID)로 간주됩니다.

다음 단계

사용자가 처음으로 로그인하면 신규 사용자 계정이 생성되고 사용자가 로그인할 때 사용한 사용자 인증 정보(사용자 이름과 비밀번호, 전화번호 또는 인증 제공업체 정보)에 연결됩니다. 이 신규 계정은 Firebase 프로젝트의 일부로 저장되며 사용자의 로그인 방법에 관계없이 프로젝트 내 모든 앱에서 사용자를 식별하는 데 사용될 수 있습니다.

  • 앱의 FirebaseUser 객체에서 사용자의 기본 프로필 정보를 가져올 수 있습니다. 사용자 관리를 참조하세요.

  • Firebase 실시간 데이터베이스와 Cloud Storage 보안 규칙auth 변수에서 로그인한 사용자의 고유 사용자 ID를 가져온 후 이 ID를 통해 사용자가 액세스할 수 있는 데이터를 관리할 수 있습니다.

인증 제공업체의 사용자 인증 정보를 기존 사용자 계정에 연결하면 사용자가 여러 인증 제공업체를 통해 앱에 로그인할 수 있습니다.

사용자를 로그아웃시키려면 signOut을 호출합니다.

Kotlin+KTX

Firebase.auth.signOut()

Java

FirebaseAuth.getInstance().signOut();