Vik*_*Vik 2 google-app-engine nosql node.js express google-cloud-datastore
我正在编写一个使用Google Datastore的Nodejs应用程序.我只想知道如何使用google数据存储区为身份验证过程设置架构.基本上,我如何使用Google Datastore 执行以下代码:
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
}
});
// generating a hash
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
我刚刚开始使用Nodejs和Google Datastore,所以如果这个问题看起来很基本,我很抱歉.
您使用gstore-node库的示例与Mongoose几乎相同:
// The connection should probably done in your server.js start script
// ------------------------------------------------------------------
var ds = require('@google-cloud/datastore')
var gstore = require('gstore-node');
gstore.connect(ds);
// In your user.model.js file
// --------------------------
var gstore = require('gstore-node');
var bcrypt = require('bcrypt-nodejs');
var Schema = gstore.Schema;
var userSchema = new Schema({
email : {type: 'string', validate:'isEmail'},
password : {type: 'string'},
});
// custom methods (https://github.com/sebelga/gstore-node#custom-methods)
// generating a hash
userSchema.methods.texts = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.get('password'));
};
// create the model for users and expose it to our app
module.exports = gstore.model('User', userSchema);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
812 次 |
最近记录: |