2016-12-25 3 views
-1

データベースとしてcloudantを使用しているループバックnode.jsアプリケーションのデフォルトで、デフォルト管理者を作成しようとしています。しかし、私がアプリケーションを起動するたびに、デフォルトのadminが作成されてログインが失敗します。データがクラウドデータベースなどに保存されていないため、コードに何か問題がありますか?ログインが失敗し、ループバックでRESTによる認証エラーが発生する

また、手動で管理者を作成すると、承認されたRESTタスクは機能しません。代わりに401認可要求のエラーがスローされます。

共通/モデル/ user.json次のとおりです。次のように

{ 
     "name": "user", 
     "base": "User", 
     "idInjection": true, 
     "options": { 
     "validateUpsert": true 
     }, 
     "properties": { 
     "name": { 
      "type": "string", 
      "required": true 
     }, 
     "image": { 
      "type": "string", 
      "required": true 
     }, 
     "country": { 
      "type": "string", 
      "required": true 
     }, 
     "phone": { 
      "type": "string", 
      "required": true 
     } 
     }, 
     "validations": [], 
     "relations": { 
     "comments": { 
      "type": "hasMany", 
      "model": "comments", 
      "foreignKey": "userId" 
     }, 
     "watchLists": { 
      "type": "hasMany", 
      "model": "watchList", 
      "foreignKey": "userId" 
     } 
     }, 
     "acls": [ 
     { 
      "accessType": "*", 
      "principalType": "ROLE", 
      "principalId": "$everyone", 
      "permission": "DENY" 
     }, 
     { 
      "accessType": "READ", 
      "principalType": "ROLE", 
      "principalId": "admin", 
      "permission": "ALLOW" 
     }, 
     { 
      "accessType": "READ", 
      "principalType": "ROLE", 
      "principalId": "$owner", 
      "permission": "ALLOW" 
     }, 
     { 
      "accessType": "EXECUTE", 
      "principalType": "ROLE", 
      "principalId": "admin", 
      "permission": "ALLOW", 
      "property": "create" 
     }, 
     { 
      "accessType": "WRITE", 
      "principalType": "ROLE", 
      "principalId": "admin", 
      "permission": "ALLOW" 
     } 
     ], 
     "methods": {} 
    } 

サーバーの/ boot/script.jsは次のとおりです。次のように

module.exports = function (app) { 
var cloudantDB = app.dataSources.cloudant; 
cloudantDB.automigrate('user', function (err) { 
if (err) throw (err); 
var user = app.models.user; 

user.find({ where: { username: 'Admin' }, limit: 1 }, function (err, users) { 

    if (!users) { 
    user.create([ 
     { username: 'Admin', email: '[email protected]', password: 'abcdef' } 
    ], function (err, users) { 
     if (err) return debug(err); 

     var Role = app.models.Role; 
     var RoleMapping = app.models.RoleMapping; 

     Role.destroyAll(); 
     RoleMapping.destroyAll(); 

     //create the admin role 
     Role.find({ 
     name: 'admin' 
     }, function (err, results) { 
     if (err) return debug(err); 

     //make Admin an admin 
     if (results.length < 1) { 
     //create the admin role 
     Role.create({ 
      name: 'admin' 
     }, function(err, role) { 
      if (err) throw (err); 
      //make admin 
      role.principals.create({ 
      principalType: RoleMapping.USER, 
      principalId: users[0].id 
      }, function(err, principal) { 
      if (err) throw (err); 
      }); 
     }); 
     } 
    }); 
    }) 
} 
else { 

} 

}); 
}); 
}; 

サーバー/ datasources.jsonは次のとおりです。

{ 
    "db": { 
    "name": "db", 
    "connector": "memory" 
}, 
"cloudant": { 
    "host": "de945f4f-c2d2-41d8-ab3c-925e3e1f8e15-bluemix.cloudant.com", 
    "port": 443, 
    "database": "show-guide-database", 
    "username": "de945f4f-c2d2-41d8-ab3c-925e3e1f8e15-bluemix", 
    "password": "55862b46942ecd959092648262b18c3ac6f7439b7025e8bdd5a4e303779f1641", 
    "name": "cloudant", 
    "connector": "cloudant" 
}, 
"images": { 
    "name": "images", 
    "connector": "loopback-component-storage", 
    "provider": "filesystem", 
    "root": "./client/" 
} 
} 

サーバー/モデル-config.jsonファイルは次のとおりです。

{ 
    "_meta": { 
"sources": [ 
    "loopback/common/models", 
    "loopback/server/models", 
    "../common/models", 
    "./models" 
], 
"mixins": [ 
    "loopback/common/mixins", 
    "loopback/server/mixins", 
    "../common/mixins", 
    "./mixins" 
] 
}, 
"User": { 
    "dataSource": "db" 
}, 
"AccessToken": { 
    "dataSource": "db", 
    "public": false 
}, 
"ACL": { 
    "dataSource": "cloudant", 
    "public": false 
}, 
"RoleMapping": { 
    "dataSource": "cloudant", 
    "public": false 
}, 
"Role": { 
    "dataSource": "cloudant", 
    "public": false 
}, 
"shows": { 
    "dataSource": "cloudant", 
    "public": true, 
    "$promise": {}, 
    "$resolved": true 
}, 
"comments": { 
    "dataSource": "cloudant", 
    "public": true 
}, 
"user": { 
    "dataSource": "cloudant", 
    "public": true, 
    "$promise": {}, 
    "$resolved": true 
}, 
"watchList": { 
    "dataSource": "cloudant", 
    "public": true 
}, 
"series": { 
    "dataSource": "cloudant", 
    "public": true 
}, 
"container": { 
    "dataSource": "images", 
    "public": true 
} 
} 
+0

非同期呼び出しメソッドがありません。 'destroyAll'、' find'は非同期で、あなたはsyncメソッドのように呼び出します。 'async'モジュールを使用してそれらのすべてを同期させると、 –

+0

を呼び出す間に注文を保つことができますか? –

答えて

0

管理者アカウントの存在のみを確認する必要があります。何かを破壊する必要はありません。

//server/boot/admin.js 

'use strict'; 

var async = require('async'); 

module.exports = function(app, cb) { 
    /* 
    * Adding necessary admin `User`s to database. 
    */ 

    var User = app.models.User; 
    var Role = app.models.Role; 
    var RoleMapping = app.models.RoleMapping; 

    var admin = { 
    username: 'admin', 
    email: '[email protected]', 
    password: '123456' 
    }; 

    User.count({email: admin.email}, function(err, count) { 
    if (err) return cb(err); 
    if (count > 0) return cb(null); 
    User.create(admin, adminHandler); 
    }); 

    var adminHandler = function(err, user) { 
    if (err) return cb(err); 

    async.waterfall([ 
     function(callback) { 
     Role.findOne({where: {name: 'ADMIN'}}, function(err, role) { 
      if (err) return callback(err); 
      if (role) return callback(null, role); 
      Role.create({name: 'ADMIN'}, callback); 
     }); 
     }, 
     function(role, callback) { 
     role.principals.create({ 
      principalType: RoleMapping.User, 
      principalId: user.id 
     }, callback); 
     } 
    ], function(err, result) { 
     if (err) return cb(err); 
     cb(null); 
    }); 
    }; 
}; 
+0

は完了しましたが、まだ動作していません。私は分かりませんが、どういうわけかboot.jsファイルは起動時には起動されません。 –

関連する問題