メール変更スクリプトのテンプレート
メール変更スクリプトは、ユーザーのメールアドレスやその検証ステータスが変わった際に、定義済みの関数を実行します。この関数の名前をchangeEmail
にすることをお勧めします。
このスクリプトはレガシー認証シナリオ でのみ使用され、ユーザーのメールアドレス(もしくはメールアドレス検証ステータス)をAuth0と外部データベースで同時に更新したい場合には必須です。
メール変更スクリプトは、Auth0 Dashboardでは構成できません。このスクリプトを管理するには、Auth0 Management APIの接続の作成または接続の更新エンドポイント、またはAuth0 Deploy CLIを使用する必要があります。
ChangeEmail関数
changeEmail関数は以下を行います。
外部データベースでユーザーのメールアドレスを更新します。
処理の失敗やエラーが発生した場合にはエラーを返します。
定義
changeEmail
関数は、4つのパラメーターを受け取り、コールバック
関数を返します。
changeEmail(email, newEmail, verified, callback): function
Was this helpful?
パラメーター | タイプ | 説明 |
---|---|---|
email |
文字列 | ユーザーの現在のメールアドレス。 |
newEmail |
文字列 | 外部データベースに設定するユーザーの新しいメールアドレスの値。 |
verified |
ブール値 | 新しいメールアドレスの確認状況。 |
callback |
関数 | エラーデータをパイプライン経由で渡すために使用。 |
例
これは疑似JavaScriptを使った例で、どのようにすればchangeEmail
関数を実装できるかがわかります。
function (email, newEmail, verified, callback) {
// Prepare the API call
let options = {
url: "https://example.com/api/users",
action: "PATCH",
body: {
email: email,
new_email: newEmail,
email_verified: verified
}
};
// Call the API
send(options, err => {
// Return `false` value in callback if operation failed
if (err && err.id == "FAIL_CHANGE_EMAIL") {
return callback(null, false);
} else if (err) {
// Return error in callback if unspecified error occurred
return callback(new Error("My custom error message."));
}
// Return `true` value in callback if operation succeeded
return callback(null, true);
});
Was this helpful?
コールバック関数
コールバック
関数は2つのパラメーターを受け取り、1つの関数を返します。
定義
callback(error, operationResult): function
Was this helpful?
パラメーター | タイプ | 必須 | 説明 |
---|---|---|---|
error |
オブジェクト | 必須 | エラーデータを含む。 |
operationResult |
ブール値 | 任意 | メール変更操作の結果を示す。 |
成功の場合
メールアドレス変更操作が成功した場合、コールバック
関数を返し、エラー
パラメーターにはnull
を、operationResult
パラメーターにはtrue
を渡します。
return callback(null, true);
Was this helpful?
失敗の場合
メールアドレス変更操作が失敗した場合、コールバック
関数を返し、error
パラメーターにはnull
を、operationResult
パラメーターにはfalse
を渡します。
return callback(null, false);
Was this helpful?
エラーの場合
エラーが発生した場合は、コールバック
関数を返し、エラー
パラメーターに関連するエラー情報を渡します。
return callback(new Error("My custom error message."));
Was this helpful?