メインコンテンツへスキップ
RulesとHooksのサポート終了(EOL)日は2026年11月18日 であり、2023年10月16 日の時点で作成された新しいテナントは使用できなくなります。Hooksが有効な既存のテナントは、サポート終了までHooksを利用できます。今後はActionsに移行して、Auth0の機能を拡張することを強くお勧めします。Actionsを使用すると、豊富な情報やインラインドキュメント、パブリックnpmパッケージにアクセスして、外部統合を使って全体的な拡張エクスペリエンスを強化することができます。Actionsの詳細については、「Auth0 Actionsの仕組みを理解する」をお読みください。当社では、移行の参考資料として、RulesからActionsへの移行HooksからActionsへの移行に関するガイドを提供しています。また、専用の「Actionsへの移行」ページでは、機能の比較やActionsのデモ、その他のリソースを掲載して、円滑な移行をサポートしています。RulesとHooksの廃止の詳細については、当社のブログ記事「RulesとHooksの提供終了について」をお読みください。
Auth0ルールを使用して、メタデータの読み取り、更新、削除を行うことができます。以降のセクションでは、ユーザーとその情報が次のJSONスニペットで表されるこの例について説明します。
{
  "user_id": "jdoe",
  "email": "john.doe@example.com",
  "app_metadata": {
    "roles": [ "writer" ]
  },
  "user_metadata": {
    "preferences": {
      "color": "blue"
    }
  }
}

メタデータの読み取り

でルールを使用してメタデータを読み取ることができます。次のように、user_metadataでプロファイル関連の情報を検索することもできます。
  • 名前
  • ニックネーム
  • given_name
  • family_name
特に設定を変更しない限り、Auth0以外のIDプロバイダー(Google、Facebook、Xなど)によって提供されるユーザープロファイル属性は、ユーザーがログインするたびにIDプロバイダーから更新されるため、直接編集することはできません。 正規化ユーザープロファイルのルート属性であるnamenicknamegiven_namefamily_namepictureを編集可能にするには、接続がAuth0と同期されるように構成して、ユーザー属性がユーザープロファイルの作成時のみIDプロバイダーに更新されるようにしなければなりません。そうすれば、これらのルート属性を個別に、または一括インポートを通じて編集できるようになります。 例として、電子メールアドレスがjane.doe@example.comのユーザーに対して次のメタデータが保存されているとします。
{
    "email": "jane.doe@example.com",
    "user_metadata": {
        "hobby": "surfing"
    },
    "app_metadata": {
        "plan": "full"
    }
}
上記のメタデータの例を使用すると、次のようにAuth0ルールまたはManagement APIの呼び出しでデータセットから特定の項目を参照できます。
console.log(user.email); // "jane.doe@example.com"
console.log(user.user_metadata.hobby); // "surfing"
console.log(user.app_metadata.plan); // "full"
任意の有効なJSONスニペットをメタデータとして使用できますが、user.app_metadataは既定で未定義であることに注意してください。 使用可能なメタデータを読み取るには、適切なユーザープロパティにアクセスする必要があります。

アプリメタデータの読み取り

ユーザーのロールに基づいて決定を行うことができます。
function(user, context, callback){
  user.app_metadata = user.app_metadata || {};
  if (user.app_metadata.roles.indexOf('writer')){
    // code to be executed
  }
  ...
}

ユーザーメタデータの読み取り

色の好みなど、特定の設定に基づいて決定を下すことができます。
function(user, context, callback){
  user.user_metadata = user.user_metadata || {};
  if (user.user_metadata.preferences.color === 'black'){
    // code to be executed
  }
  ...
}

アプリケーションメタデータの読み取り(clientMetadata)

アプリケーションメタデータ(clientMetadata)は、コンテキストオブジェクトのオプションの最上位プロパティです。既存のアプリケーションには、このプロパティの値はありません。
function(user, context, callback){
  context.clientMetadata = context.clientMetadata || {};
  if (context.clientMetadata.usersuppliedkey1 === 'black'){
    // this code would not be executed for the user
  }
  ...
}

メタデータの更新

ルールを使用して、Auth0がIdPから受け取ったSAML属性をuser_metadataapp_metadataにマッピングします。

アプリメタデータの更新

管理者ロールをユーザーに追加するには:
function(user, context, callback){
  user.app_metadata = user.app_metadata || {};
  // update the app_metadata that will be part of the response
  user.app_metadata.roles = user.app_metadata.roles || [];
  user.app_metadata.roles.push('administrator');

  // persist the app_metadata update
  auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
}
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
  "user_id": "jdoe",
  "email": "john.doe@example.com",
  "app_metadata": {
    "roles": [ "writer", "administrator" ]
  },
  "user_metadata": {
    "preferences": {
      "color": "blue"
    }
  }
}

ユーザーメタデータの更新

ユーザーのfontSize設定をユーザープロファイルに追加するには:
function(user, context, callback){
  user.user_metadata = user.user_metadata || {};
  // update the user_metadata that will be part of the response
  user.user_metadata.preferences = user.user_metadata.preferences || {};
  user.user_metadata.preferences.fontSize = 12;

  // persist the user_metadata update
  auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
}
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
  "user_id": "jdoe",
  "email": "john.doe@example.com",
  "app_metadata": {
    "roles": [ "writer" ]
  },
  "user_metadata": {
    "preferences": {
      "color": "blue",
      "fontSize": 12
    }
  }
}

アプリとユーザーのメタデータを同時に更新

ルールの処理時間を短縮するには、同じルールでapp_metadatauser_metadataの両方を更新します。
function(user, context, callback){

  var q = require('q');

  user.app_metadata = user.app_metadata || {};
  user.user_metadata = user.user_metadata || {};
  // update the user_metadata that will be part of the response
  user.user_metadata.preferences = user.user_metadata.preferences || {};
  user.user_metadata.preferences.fontSize = 12;

  // update the app_metadata that will be part of the response
  user.app_metadata.roles = user.app_metadata.roles || [];
  user.app_metadata.roles.push('admin');

  // persist the app_metadata update
  var appMetadataPromise  = auth0.users.updateAppMetadata(user.user_id, user.app_metadata);

  // persist the user_metadata update
  var userMetadataPromise = auth0.users.updateUserMetadata(user.user_id, user.user_metadata);

  // using q library to wait for all promises to complete
  q.all([userMetadataPromise, appMetadataPromise])
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
}
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
  "user_id": "jdoe",
  "email": "john.doe@example.com",
  "app_metadata": {
    "roles": [ "writer", "admin" ]
  },
  "user_metadata": {
    "preferences": {
      "color": "blue",
      "fontSize": 12
    }
  }
}

メタデータの削除

アプリメタデータのプロパティと値を削除します

プロパティを削除するには、プロパティの値をnullに設定します。

ユーザーのロールの削除の例

ユーザーのロールを削除するには、次のサンプルルールを使用します。
function(user, context, callback){
  user.app_metadata = user.app_metadata || {};
  // update the app_metadata that will be part of the response
  user.app_metadata.roles = null;

  // persist the app_metadata update
  auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
}
これにより、ユーザープロファイルが次のJSON表記になります。
{
  "user_id": "jdoe",
  "email": "john.doe@example.com",
  "app_metadata": { },
  "user_metadata": {
    "preferences": {
      "color": "blue"
    }
  }
}

単一のプロパティ値の削除の例

プロパティの単一の値を削除するには、その特定の値を削除します。たとえば、ユーザープロファイルからライターロールを削除するには:
function(user, context, callback){
  user.app_metadata = user.app_metadata || {};
  user.app_metadata.roles = user.app_metadata.roles || [];

  var index = user.app_metadata.roles.indexOf('writer');

  if (index !== -1){
    // update the app_metadata that will be part of the response
    user.app_metadata.roles.splice(index, 1);
  }

  // persist the app_metadata update
  auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
    });
}
これにより、ユーザープロファイルが次のJSON表記になります。
{
  "user_id": "google-oauth2|1234",
  "email": "john.doe@gmail.com",
  "app_metadata": {
    "roles": [ ]
  },
  "user_metadata": {
    "preferences": {
      "color": "blue"
    }
  }
}
ロールプロパティはまだ存在しますが、値が含まれていないことに注意してください。

ユーザーメタデータのプロパティと値を削除

ユーザーの色の設定を削除するには:
function(user, context, callback){
  user.user_metadata = user.user_metadata || {};
  // update the user_metadata that will be part of the response
  user.user_metadata.preferences = user.user_metadata.preferences || {};
  delete user.user_metadata.preferences.color;

  // persist the user_metadata update
  auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
    .then(function(){
      callback(null, user, context);
    })
    .catch(function(err){
      callback(err);
  });
}
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
  "user_id": "jdoe",
  "email": "john.doe@example.com",
  "app_metadata": {
    "roles": [ "writer" ]
  },
  "user_metadata": {
    "preferences": { }
  }
}

もっと詳しく

I