sh [SailsJS设置] #sailsjs测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh [SailsJS设置] #sailsjs测试相关的知识,希望对你有一定的参考价值。
// api/models test
import bcrypt from 'bcrypt';
module.exports = {
attributes: {
username: {
type: 'string',
required: true,
unique: true,
},
email: {
type: 'string',
required: true,
unique: true,
isEmail: true,
},
password: {
type: 'string',
required: true,
},
},
customToJSON: function() {
return _.omit(this, ['password']);
},
// Lifecycle
beforeCreate: function(values, next) {
if (values.password) {
bcrypt.genSalt(10, function (err, salt) {
if (err) return next(err);
bcrypt.hash(values.password, salt, function (err, hash) {
if (err) return next(err);
values.password = hash;
next();
});
});
} else {
next();
}
},
};
// api/controllers
import bcrypt from 'bcrypt';
module.exports = {
login: function (req, res) {
const { username, password } = req.body;
if (!password) {
return res.badRequest('PasswordMissing');
}
Users.findOne({
where: {
or: [{ username: username }, { email: username }],
},
}).exec((error, user) => {
if (error) {
return res.badRequest(error);
}
if (!user) {
return res.badRequest('UserNotFound');
}
bcrypt.compare(password, user.password, (error, response) => {
if (error) {
return res.badRequest(error);
}
if (!response) {
return res.badRequest('WrongPassword');
}
res.json(user);
});
});
},
};
// add this at first in the app.js file
require('babel-core/register');
require('babel-polyfill');
// config
module.exports.datastores = {
'mongoServer': {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'base__name'
}
};
// config/env
module.exports = { models: { migrate: 'alter', }, };
// config
module.exports.models = {
// schema: true,
// migrate: 'alter',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
},
dataEncryptionKeys: {
default: 'encryption__keys'
},
cascadeOnDestroy: true,
datastore: 'mongoServer',
};
yarn add axios bcrypt moviedb sails-hook-grunt sails-hook-orm sails-hook-slugs sails-hook-sockets sails-mongo
yarn add -D babel-cli babel-eslint babel-plugin-transform-runtime babel-preset-es2017 babel-preset-stage-2 babel-regenerator-runtime sails-hook-babel
以上是关于sh [SailsJS设置] #sailsjs测试的主要内容,如果未能解决你的问题,请参考以下文章
Sailsjs 套接字 IO
Sailsjs:在 Sails 0.11 中,如何不使用带有套接字的 onConnect 生命周期回调
如何登录 SailsJS
SailsJS 到 Phonegap?
无法理解 Sailsjs 期望啥创建适配器
Vscode无法使用Sailsjs Services和打字稿