スクリプトのテンプレートを削除する

削除スクリプトは、定義された関数を実行し、外部のデータベースから指定されたユーザーを削除します。この関数の名前はdeleteUserにすることをお勧めします。

このスクリプトはレガシー認証シナリオでのみ使用され、ユーザーをAuth0と外部のデータベースから同時に削除したい場合には必須です。

DeleteUser関数

deleteUser関数は以下を行います。

  • 外部のデータベースから指定されたユーザーを削除します。

  • 削除に成功した場合、null値を返します。

  • 削除が失敗した場合には、エラーを返します。

定義

deleteUser関数は2つのパラメーターを受け取り、callback関数を返します。

deleteUser(id, callback): function

Was this helpful?

/

パラメーター タイプ 説明
id 文字列 ユーザーのID、user_idauth0で始まりません。
callback 関数 パイプラインを通してエラーやプロファイルデータを渡すために使用されます。

これは疑似JavaScriptを使った例で、どのようにすればdeleteUser関数を実装できるかがわかります。言語固有の例については、「言語固有のスクリプトの例」をお読みください。

function deleteUser(id, callback) {
  // Send user identifier to external database API
  let options = {
    url: "https://example.com/api/deleteUser",
    body: {
      id: id
    }
  };

  send(options, (err, profileData) => {
    // Return error in callback if deletion failed
    if (err) {
      return callback(new Error("My custom error message."));
    }

    // Return null value in callback if deletion succeeded
    return callback(null);
  });
}

Was this helpful?

/

コールバック関数

callback関数は1つのパラメーターを受け取り、1つの関数を返します。

定義

callback(error): function

Was this helpful?

/

パラメーター タイプ 必須 説明
error オブジェクト 必須 エラーデータを含む。

成功の場合

外部データベースでユーザーの削除に成功した場合、callback関数を返し、errorパラメーターにはnull値を渡します。

return callback(null);

Was this helpful?

/

エラーの場合

エラーが発生した場合は、callback関数を返し、errorパラメーターに関連するエラー情報を渡します。

return callback(new Error("My custom error message."));

Was this helpful?

/

言語固有のスクリプトの例

Auth0は、以下の言語や技術で使用できるサンプルスクリプトを提供しています。

JavaScript

function remove (id, callback) {
  // This script remove a user from your existing database.
  // It is executed whenever a user is deleted from the Management API or Auth0 dashboard.
  //
  // There are two ways that this script can finish:
  // 1. The user was removed successfully:
  //     callback(null);
  // 2. Something went wrong while trying to reach your database:
  //     callback(new Error("my error message"));
  var msg = "Please implement the Delete script for this database " +
       "connection at https://manage.auth0.com/#/connections/database";
  return callback(new Error(msg));
}

Was this helpful?

/

ASP.NET Membership Provider(MVC3 - Universal Providers)

function remove(id, callback) {
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;
  const connection = new Connection({
    userName: 'the username',
    password: 'the password',
    server: 'the server',
    options: {
      database: 'the db name',
      encrypt: true,
      // Required to retrieve userId needed for Membership entity creation
      rowCollectionOnRequestCompletion: true
    }
  });
  connection.on('debug', function(text) {
    // if you have connection issues, uncomment this to get more detailed info
    // console.log(text);
  }).on('errorMessage', function(text) {
    // this will show any errors when connecting to the SQL database or with the SQL statements
    console.log(JSON.stringify(text));
  });
  connection.on('connect', function(err) {
    if (err) return callback(err);
    executeDelete(['Memberships', 'Users'], function(err) {
      if (err) return callback(err);
      callback(null);
    });
  });
  function executeDelete(tables, callback) {
    const query = tables.map(function(table) {
      return 'DELETE FROM ' + table + ' WHERE UserId = @UserId';
    }).join(';');
    const request = new Request(query, function(err) {
      if (err) return callback(err);
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  }
}

Was this helpful?

/

ASP.NET Membership Provider(MVC4 - Simple Membership)

function remove(id, callback) {
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;
  const connection = new Connection({
    userName: 'the username',
    password: 'the password',
    server: 'the server',
    options: {
      database: 'the db name',
      encrypt: true,
      // Required to retrieve userId needed for Membership entity creation
      rowCollectionOnRequestCompletion: true
    }
  });
  connection.on('debug', function (text) {
    // if you have connection issues, uncomment this to get more detailed info
    // console.log(text);
  }).on('errorMessage', function (text) {
    // this will show any errors when connecting to the SQL database or with the SQL statements
    console.log(JSON.stringify(text));
  });
  connection.on('connect', function (err) {
    if (err) return callback(err);
    executeDelete(['webpages_Membership', 'UserProfile'], function (err) {
      if (err) return callback(err);
      callback(null);
    });
  });
  function executeDelete(tables, callback) {
    const query = tables.map(function (table) {
      return 'DELETE FROM ' + table + ' WHERE UserId = @UserId';
    }).join(';');
    const request = new Request(query, function (err) {
      if (err) return callback(err);
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  }
}

Was this helpful?

/

MongoDB

function remove(id, callback) {
  const MongoClient = require('mongodb@3.1.4').MongoClient;
  const client = new MongoClient('mongodb://user:pass@mymongoserver.com');
  client.connect(function (err) {
    if (err) return callback(err);
    const db = client.db('db-name');
    const users = db.collection('users');
    users.remove({ _id: id }, function (err) {
      client.close();
      if (err) return callback(err);
      callback(null);
    });
  });
}

Was this helpful?

/

MySQL

function remove(id, callback) {
  const mysql = require('mysql');
  const connection = mysql({
    host: 'localhost',
    user: 'me',
    password: 'secret',
    database: 'mydb'
  });
  connection.connect();
  const query = 'DELETE FROM users WHERE id = ?';
  connection.query(query, [ id ], function(err) {
    if (err) return callback(err);
    callback(null);
  });
}

Was this helpful?

/

PostgreSQL

function remove(id, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres
  const postgres = require('pg');
  const conString = 'postgres://user:pass@localhost/mydb';
  postgres.connect(conString, function (err, client, done) {
    if (err) return callback(err);
    const query = 'DELETE FROM users WHERE id = $1';
    client.query(query, [id], function (err) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      done();
      return callback(err);
    });
  });
}

Was this helpful?

/

SQL Server

function remove(id, callback) {
  // this example uses the "tedious" library
  // more info here: http://pekim.github.io/tedious/index.html
  const sqlserver = require('tedious@1.11.0');
  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;
  const connection = new Connection({
    userName:  'test',
    password:  'test',
    server:    'localhost',
    options:  {
      database: 'mydb'
    }
  });
  const query = 'DELETE FROM dbo.Users WHERE id = @UserId';
  connection.on('debug', function (text) {
    console.log(text);
  }).on('errorMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  }).on('infoMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  });
  connection.on('connect', function (err) {
    if (err) return callback(err);
    const request = new Request(query, function (err) {
      if (err) return callback(err);
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  });
}

Was this helpful?

/

Windows Azure SQL Database

function remove (id, callback) {
  // this example uses the "tedious" library
  // more info here: http://pekim.github.io/tedious/index.html
  var Connection = require('tedious@1.11.0').Connection;
  var Request = require('tedious@1.11.0').Request;
  var TYPES = require('tedious@1.11.0').TYPES;
  var connection = new Connection({
    userName: 'your-user@your-server-id.database.windows.net',
    password: 'the-password',
    server: 'your-server-id.database.windows.net',
    options: {
      database: 'mydb',
      encrypt: true
    }
  });
  connection.on('debug', function (text) {
    console.log(text);
  }).on('errorMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  }).on('infoMessage', function (text) {
    console.log(JSON.stringify(text, null, 2));
  });
  connection.on('connect', function (err) {
    if (err) { return callback(err); }
    var query = 'DELETE FROM users WHERE id = @UserId';
    var request = new Request(query, function (err) {
      if (err) { return callback(err); }
      callback(null);
    });
    request.addParameter('UserId', TYPES.VarChar, id);
    connection.execSql(request);
  });
}

Was this helpful?

/

もっと詳しく