ルールを使用してメタデータを管理
Auth0ルールを使用して、メタデータの読み取り、更新、削除を行うことができます。以降のセクションでは、ユーザーとその情報が次のJSONスニペットで表されるこの例について説明します。
{
"user_id": "jdoe",
"email": "john.doe@example.com",
"app_metadata": {
"roles": [ "writer" ]
},
"user_metadata": {
"preferences": {
"color": "blue"
}
}
}
Was this helpful?
メタデータの読み取り
Management APIでルールを使用してメタデータを読み取ることができます。次のように、user_metadata
でプロファイル関連の情報を検索することもできます。
名前
ニックネーム
given_name
family_name
特に設定を変更しない限り、Auth0以外のIDプロバイダー(Google、Facebook、Xなど)によって提供されるユーザープロファイル属性は、ユーザーがログインするたびにIDプロバイダーから更新されるため、直接編集することはできません。
正規化ユーザープロファイルのルート属性であるname
、nickname
、given_name
、family_name
、picture
を編集可能にするには、接続がAuth0と同期されるように構成して、ユーザー属性がユーザープロファイルの作成時のみIDプロバイダーに更新されるようにしなければなりません。そうすれば、これらのルート属性を個別に、または一括インポートを通じて編集できるようになります。
例として、電子メールアドレスがjane.doe@example.com
のユーザーに対して次のメタデータが保存されているとします。
{
"email": "jane.doe@example.com",
"user_metadata": {
"hobby": "surfing"
},
"app_metadata": {
"plan": "full"
}
}
Was this helpful?
上記のメタデータの例を使用すると、次のように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"
Was this helpful?
任意の有効な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
}
...
}
Was this helpful?
ユーザーメタデータの読み取り
色の好みなど、特定の設定に基づいて決定を下すことができます。
function(user, context, callback){
user.user_metadata = user.user_metadata || {};
if (user.user_metadata.preferences.color === 'black'){
// code to be executed
}
...
}
Was this helpful?
アプリケーションメタデータの読み取り(clientMetadata)
アプリケーションメタデータ(clientMetadata
)は、コンテキスト
オブジェクトのオプションの最上位プロパティです。既存のアプリケーションには、このプロパティの値はありません。
function(user, context, callback){
context.clientMetadata = context.clientMetadata || {};
if (context.clientMetadata.usersuppliedkey1 === 'black'){
// this code would not be executed for the user
}
...
}
Was this helpful?
メタデータの更新
アプリメタデータの更新
管理者ロールをユーザーに追加するには:
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);
});
}
Was this helpful?
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
"user_id": "jdoe",
"email": "john.doe@example.com",
"app_metadata": {
"roles": [ "writer", "administrator" ]
},
"user_metadata": {
"preferences": {
"color": "blue"
}
}
}
Was this helpful?
ユーザーメタデータの更新
ユーザーの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);
});
}
Was this helpful?
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
"user_id": "jdoe",
"email": "john.doe@example.com",
"app_metadata": {
"roles": [ "writer" ]
},
"user_metadata": {
"preferences": {
"color": "blue",
"fontSize": 12
}
}
}
Was this helpful?
アプリとユーザーのメタデータを同時に更新
ルールの処理時間を短縮するには、同じルールでapp_metadata
とuser_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);
});
}
Was this helpful?
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
"user_id": "jdoe",
"email": "john.doe@example.com",
"app_metadata": {
"roles": [ "writer", "admin" ]
},
"user_metadata": {
"preferences": {
"color": "blue",
"fontSize": 12
}
}
}
Was this helpful?
メタデータの削除
アプリメタデータのプロパティと値を削除します
プロパティを削除するには、プロパティの値を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);
});
}
Was this helpful?
これにより、ユーザープロファイルが次のJSON表記になります。
{
"user_id": "jdoe",
"email": "john.doe@example.com",
"app_metadata": { },
"user_metadata": {
"preferences": {
"color": "blue"
}
}
}
Was this helpful?
単一のプロパティ値の削除の例
プロパティの単一の値を削除するには、その特定の値を削除します。たとえば、ユーザープロファイルからライター
ロールを削除するには:
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);
});
}
Was this helpful?
これにより、ユーザープロファイルが次のJSON表記になります。
{
"user_id": "google-oauth2|1234",
"email": "john.doe@gmail.com",
"app_metadata": {
"roles": [ ]
},
"user_metadata": {
"preferences": {
"color": "blue"
}
}
}
Was this helpful?
ロール
プロパティはまだ存在しますが、値が含まれていないことに注意してください。
ユーザーメタデータのプロパティと値を削除
ユーザーの色の設定を削除するには:
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);
});
}
Was this helpful?
これにより、ユーザープロファイルの詳細が次のJSON表記になります。
{
"user_id": "jdoe",
"email": "john.doe@example.com",
"app_metadata": {
"roles": [ "writer" ]
},
"user_metadata": {
"preferences": { }
}
}
Was this helpful?