모바일 앱은 때때로 사용자와 상호 작용하고 이메일을 보내 특정 조치를 취하도록 프롬프트를 표시해야 합니다.
Firebase 클라이언트 SDK 는 비밀번호 재설정, 이메일 주소 확인, 이메일 기반 로그인에 사용할 수 있는 링크가 포함된 이메일을 사용자에게 보낼 수 있는 기능을 제공합니다. 이러한 템플릿 기반 이메일은 Google에서 전송하며 사용자 정의가 제한됩니다.
자체 이메일 템플릿과 자체 이메일 전송 서비스를 대신 사용하려는 경우 이 페이지에서는 Firebase Admin SDK 를 사용하여 사용자에게 보내는 이메일에 포함할 수 있는 위의 흐름에 대한 작업 링크를 프로그래밍 방식으로 생성하는 방법을 설명합니다.
다음과 같은 이점이 있습니다.
- 이메일 템플릿을 사용자 정의합니다. 여기에는 새로운 스타일 및 사용자 지정 브랜딩 추가, 문구 및 로고 변경, 전체 이름 대신 이름으로 사용자에게 주소 지정 등의 기능이 포함됩니다.
- 컨텍스트에 따라 다른 템플릿을 적용합니다. 예를 들어 사용자가 뉴스레터를 구독하기 위해 이메일을 확인하는 경우 이메일 콘텐츠에 컨텍스트를 제공해야 할 수 있습니다. 또 다른 예는 이메일 링크 로그인입니다. 한 시나리오에서 이는 동일한 사용자에 의해 트리거되거나 다른 사용자의 초대로 트리거될 수 있습니다. 컨텍스트는 이메일에 포함되어야 합니다.
- 맞춤형 이메일 템플릿을 현지화합니다.
- 보안 서버 환경에서 링크를 생성하는 기능.
- 모바일 앱이나 브라우저를 통해 링크를 여는 방법과 추가 상태 정보를 전달하는 방법 등을 사용자 정의하는 기능
- 이메일 작업 링크를 구성할 때 모바일 앱 흐름에 사용되는 동적 링크 도메인을 사용자 지정하고 컨텍스트 또는 모바일 앱에 따라 다른 동적 링크 도메인을 지정할 수도 있습니다.
ActionCodeSettings 초기화
이메일 작업 링크를 생성하기 전에 ActionCodeSettings
인스턴스를 초기화해야 할 수 있습니다.
ActionCodeSettings
를 사용하면 사용자가 이메일 링크를 클릭한 후 액세스할 수 있는 계속 URL을 통해 추가 상태를 전달할 수 있습니다. 이것은 또한 사용자가 작업이 완료된 후 앱으로 돌아갈 수 있는 기능을 제공합니다. 또한 모바일 애플리케이션이 설치된 경우 또는 브라우저에서 이메일 작업 링크를 직접 처리할지 여부를 지정할 수 있습니다.
모바일 앱을 통해 열리는 링크의 경우 Firebase 동적 링크를 활성화하고 모바일 앱에서 이러한 링크를 감지하는 몇 가지 작업을 수행해야 합니다. 이메일 작업에 대해 Firebase 동적 링크를 구성 하는 방법에 대한 지침을 참조하세요.
ActionCodeSettings
인스턴스를 초기화하려면 다음 데이터를 제공하세요.
매개변수 | 유형 | 설명 |
---|---|---|
url | 끈 | 컨텍스트에 따라 의미가 다른 링크(상태/계속 URL)를 설정합니다.
|
iOS | ({번들 ID: 문자열}|정의되지 않음) | 번들 ID를 설정합니다. Apple 앱이 설치된 경우 링크를 열려고 시도합니다. 콘솔에 앱을 등록해야 합니다. |
android | ({패키지 이름: 문자열, installApp:부울|정의되지 않음, 최소 버전: 문자열|정의되지 않음}|정의되지 않음) | Android 패키지 이름을 설정합니다. Android 앱이 설치된 경우 링크를 열려고 시도합니다. installApp 이 전달되면 기기가 Android 앱을 지원하고 앱이 아직 설치되지 않은 경우 Android 앱을 설치할지 여부를 지정합니다. 이 필드가 packageName 없이 제공되면 packageName 이 이 필드와 함께 제공되어야 함을 설명하는 오류가 발생합니다. minimumVersion 이 지정되고 이전 버전의 앱이 설치된 경우 사용자는 앱을 업그레이드하기 위해 Play 스토어로 이동합니다. Android 앱은 콘솔에 등록해야 합니다. |
handleCodeInApp | (부울|정의되지 않음) | 이메일 작업 링크가 모바일 앱에서 먼저 열릴지 아니면 웹 링크에서 먼저 열릴지 여부입니다. 기본값은 false입니다. true로 설정하면 액션 코드 링크가 유니버설 링크 또는 Android 앱 링크로 전송되고 앱이 설치된 경우 앱에서 열립니다. false의 경우 코드가 먼저 웹 위젯으로 전송된 다음 계속할 때 앱이 설치된 경우 앱으로 리디렉션됩니다. |
dynamicLinkDomain | (문자열|정의되지 않음) | Firebase 동적 링크를 사용하여 열려면 현재 링크에 사용할 동적 링크 도메인(또는 하위 도메인)을 설정합니다. 프로젝트당 여러 개의 동적 링크 도메인을 구성할 수 있으므로 이 필드는 명시적으로 하나를 선택할 수 있는 기능을 제공합니다. 아무것도 제공하지 않으면 기본적으로 가장 오래된 도메인이 사용됩니다. |
다음 예에서는 모바일 앱에서 먼저 열릴 이메일 확인 링크를 Firebase 동적 링크(Apple 앱 com.example.ios
또는 Android 앱 com.example.android
가 아직 설치되지 않은 경우 설치할 위치 및 최소 버전은 12)입니다. 딥 링크에는 계속 URL 페이로드 https://www.example.com/checkout?cartId=1234
가 포함됩니다. 사용된 동적 링크 도메인은 Firebase 동적 링크와 함께 사용하도록 구성해야 하는 coolapp.page.link
입니다.
노드.js
const 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/checkout?cartId=1234',
// This must be true for email link sign-in.
handleCodeInApp: true,
iOS: {
bundleId: 'com.example.ios',
},
android: {
packageName: 'com.example.android',
installApp: true,
minimumVersion: '12',
},
// FDL custom domain.
dynamicLinkDomain: 'coolapp.page.link',
};
자바
ActionCodeSettings actionCodeSettings = ActionCodeSettings.builder()
.setUrl("https://www.example.com/checkout?cartId=1234")
.setHandleCodeInApp(true)
.setIosBundleId("com.example.ios")
.setAndroidPackageName("com.example.android")
.setAndroidInstallApp(true)
.setAndroidMinimumVersion("12")
.setDynamicLinkDomain("coolapp.page.link")
.build();
파이썬
action_code_settings = auth.ActionCodeSettings(
url='https://www.example.com/checkout?cartId=1234',
handle_code_in_app=True,
ios_bundle_id='com.example.ios',
android_package_name='com.example.android',
android_install_app=True,
android_minimum_version='12',
dynamic_link_domain='coolapp.page.link',
)
가다
actionCodeSettings := &auth.ActionCodeSettings{
URL: "https://www.example.com/checkout?cartId=1234",
HandleCodeInApp: true,
IOSBundleID: "com.example.ios",
AndroidPackageName: "com.example.android",
AndroidInstallApp: true,
AndroidMinimumVersion: "12",
DynamicLinkDomain: "coolapp.page.link",
}
씨#
var actionCodeSettings = new ActionCodeSettings()
{
Url = "https://www.example.com/checkout?cartId=1234",
HandleCodeInApp = true,
IosBundleId = "com.example.ios",
AndroidPackageName = "com.example.android",
AndroidInstallApp = true,
AndroidMinimumVersion = "12",
DynamicLinkDomain = "coolapp.page.link",
};
자세한 내용 은 이메일 작업에서 상태 전달 을 참조하십시오.
비밀번호 재설정 이메일 링크 생성
비밀번호 재설정 링크를 생성하려면 기존 사용자의 이메일과 선택적 ActionCodeSettings
객체를 제공하십시오. 작업은 이메일 작업 링크로 해결됩니다. 사용된 이메일은 기존 사용자의 것이어야 합니다.
노드.js
// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
getAuth()
.generatePasswordResetLink(userEmail, actionCodeSettings)
.then((link) => {
// Construct password reset email template, embed the link and send
// using custom SMTP server.
return sendCustomPasswordResetEmail(userEmail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
자바
String email = "user@example.com";
try {
String link = FirebaseAuth.getInstance().generatePasswordResetLink(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
System.out.println("Error generating email link: " + e.getMessage());
}
파이썬
email = 'user@example.com'
link = auth.generate_password_reset_link(email, action_code_settings)
# Construct password reset email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
가다
email := "user@example.com"
link, err := client.PasswordResetLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
log.Fatalf("error generating email link: %v\n", err)
}
// Construct password reset template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)
씨#
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GeneratePasswordResetLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
링크가 생성된 후 사용자 정의 비밀번호 재설정 이메일에 삽입한 다음 사용자 정의 SMTP 서버를 사용하여 해당 사용자에게 이메일을 보낼 수 있습니다.
기본 비밀번호 재설정 랜딩 페이지를 사용하지 않고 고유한 사용자 지정 핸들러를 구축하는 경우 사용자 지정 이메일 작업 핸들러 만들기 를 참조하세요.
이메일 확인 링크 생성
이메일 확인 링크를 생성하려면 기존 사용자의 확인되지 않은 이메일과 선택적 ActionCodeSettings
객체를 제공하세요. 작업은 이메일 작업 링크로 해결됩니다. 사용된 이메일은 기존 사용자의 것이어야 합니다.
노드.js
// Admin SDK API to generate the email verification link.
const useremail = 'user@example.com';
getAuth()
.generateEmailVerificationLink(useremail, actionCodeSettings)
.then((link) => {
// Construct email verification template, embed the link and send
// using custom SMTP server.
return sendCustomVerificationEmail(useremail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
자바
String email = "user@example.com";
try {
String link = FirebaseAuth.getInstance().generateEmailVerificationLink(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
System.out.println("Error generating email link: " + e.getMessage());
}
파이썬
email = 'user@example.com'
link = auth.generate_email_verification_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
가다
email := "user@example.com"
link, err := client.EmailVerificationLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
log.Fatalf("error generating email link: %v\n", err)
}
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)
씨#
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateEmailVerificationLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
링크가 생성된 후 사용자 지정 확인 이메일에 삽입한 다음 사용자 지정 SMTP 서버를 사용하여 해당 사용자에게 전자 메일로 보낼 수 있습니다.
기본 이메일 확인 랜딩 페이지를 사용하지 않고 고유한 사용자 정의 핸들러를 구축하는 경우 사용자 정의 이메일 작업 핸들러 생성 을 참조하십시오.
로그인을 위한 이메일 링크 생성
이메일 링크 로그인으로 사용자를 인증하려면 먼저 Firebase 프로젝트 에 이메일 링크 로그인을 활성화 해야 합니다.
로그인 링크를 생성하려면 사용자의 이메일과 ActionCodeSettings
객체를 제공하세요. 이 경우 ActionCodeSettings
개체는 로그인 완료를 위해 링크를 클릭한 후 사용자를 반환할 위치에 대한 정보를 제공하는 데 필요합니다. 작업은 이메일 작업 링크로 해결됩니다.
비밀번호 재설정 및 이메일 확인과 달리 사용된 이메일은 반드시 기존 사용자의 것일 필요는 없습니다. 이 작업을 통해 이메일 링크를 통해 앱에 신규 사용자를 등록할 수 있기 때문입니다.
노드.js
// Admin SDK API to generate the sign in with email link.
const useremail = 'user@example.com';
getAuth()
.generateSignInWithEmailLink(useremail, actionCodeSettings)
.then((link) => {
// Construct sign-in with email link template, embed the link and
// send using custom SMTP server.
return sendSignInEmail(useremail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
자바
String email = "user@example.com";
try {
String link = FirebaseAuth.getInstance().generateSignInWithEmailLink(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
System.out.println("Error generating email link: " + e.getMessage());
}
파이썬
email = 'user@example.com'
link = auth.generate_sign_in_with_email_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
가다
email := "user@example.com"
link, err := client.EmailSignInLink(ctx, email, actionCodeSettings)
if err != nil {
log.Fatalf("error generating email link: %v\n", err)
}
// Construct sign-in with email link template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)
씨#
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateSignInWithEmailLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
링크가 생성된 후 사용자 지정 로그인 이메일에 삽입한 다음 사용자 지정 SMTP 서버를 사용하여 해당 사용자에게 전자 메일로 보낼 수 있습니다.
이메일 링크를 사용하여 Firebase로 사용자를 인증하는 방법에 대해 자세히 알아보세요. 이렇게 하면 사용자가 링크를 클릭하고 앱으로 다시 리디렉션된 후 로그인을 완료하는 방법에 대한 정보를 제공하는 데 도움이 됩니다.