メインコンテンツへスキップ
Post Change Password トリガーは、ユーザーがパスワードをリセットまたは変更した後に実行されます。このトリガーを使用すると、パスワード変更後にユーザーへメールを送信したり、ユーザーのパスワードが変更されたことを別のシステムに通知したりできます。これにより、Auth0 で管理されていない他のセッションを無効化できます。
Actions の Post Change Password フローを示す図
このフローの Actions はノンブロッキング (非同期) です。つまり、Auth0 のパイプラインは Action の実行完了を待たずに処理を継続します。そのため、Action の実行結果は Auth0 のトランザクションに影響しません。

トリガー

Post Change Password

post-change-password トリガーは、データベース接続を使用するユーザーがパスワードをリセットまたは変更した後に実行されます。 このトリガーには複数の Actions を関連付けることができ、それらは順番に実行されます。ただし、これらの Actions は非同期で実行されるため、パスワードリセット処理を妨げることはありません。

リファレンス

一般的な使用例

別のシステムでユーザーのセッションを無効化する

パスワード変更後の Action を使用すると、別のシステムでユーザーのセッションを無効化できます。
const axios = require("axios");

/**
 * @param {Event} event - パスワードが変更されたユーザーの詳細。
 */
exports.onExecutePostChangePassword = async (event) => {
  axios.post("https://my-api.exampleco.com/revoke-session", { params: { email: event.user.email }});
};
axios のような npm ライブラリを使用するには、そのライブラリを依存関係として Action に追加する必要があります。詳しくは、Write Your First Action の「Add a dependency」セクションを参照してください。

ユーザーがパスワードを変更した後、メールを送信する

const axios = require("axios");

exports.onExecutePostChangePassword = async (event) => {
  try {
    // https://sendgrid.api-docs.io/v3.0/mail-send
    axios.post('https://api.sendgrid.com/v3/mail/send',
      {
        personalizations: [{
          to: [{ email: event.user.email }]
        }],
        from: { email: 'admin@exampleco.com' },
        subject: 'Your password was changed',
        content: [{
          type: 'text/plain',
          value: 'The password for your ' + event.connection.name + ' account ' + event.user.email + ' was recently changed.'
        }]
      },
      {
        headers: {
          'Authorization': 'Bearer ' + event.secrets.SENDGRID_API_KEY
        },
      }
    );
  } catch (err) {
    console.log(`Error sending email to ${event.user.email}:`, err.message)
  }
};