您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页Implement Passport.js authentica

Implement Passport.js authentica

来源:二三娱乐

第一步:安装依赖

在开始之前,我们要确保以下依赖都安装到了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();
      });
    });
  }
};

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务