Implement Passport.js authentication with Sails.js 1.0

第一步:安装依赖

在开始之前,我们要确保以下依赖都安装到了Sails.js

npm i --save bcrypt-nodejs
npm i --save passport
npm i --save passport-local
npm i --save jsonwebtoken

第二步:生成用户模型

通过以下命令可以自动生成用户模型 User.js 到api/models目录下

sails generate model user

在User.js文件内,我们这样配置

const bcrypt = require('bcrypt-nodejs');
module.exports = {
attributes: {
    email: {
      type: 'email',
      required: true,
      unique: true
    },
    username: {
      type: 'string',
      required: true,
      unique: true
    },
    password: {
      type: 'string',
      required: true
    }
  },
  customToJSON: function() {
     return _.omit(this, ['password'])
  },
  beforeCreate: function(user, cb){
    bcrypt.genSalt(10, function(err, salt){
      bcrypt.hash(user.password, salt, null, function(err, hash){
        if(err) return cb(err);
        user.password = hash;
        return cb();
      });
    });
  }
};

查看原文:https://medium.com/@greg.hesp/implement-passport-js-authentication-with-sails-js-1-0-50888265fb83

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,540评论 25 708
  • 前言 入手Node.js半年,从用Express开发自己的博客到用Sails开发公司项目,深深被Sails震撼了。...
    JC_Huang阅读 13,491评论 4 34
  • 自由书写五月第十五篇 初中的时候,情窦初开,彼时男女之间的界限很是森严,女生默默地关注喜欢的男生,偶尔几个女生围在...
    Vanillawei阅读 174评论 0 1
  • 今天读到笑来老师专栏-尊重资本量级的差异,我理解这真的不只是金钱的差异,而且能力、时间、观念等等的差异。 在我呆的...
    Sarah555阅读 208评论 0 0