Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const bcrypt = require('bcrypt');
  4. const Knex = require("knex")({
  5. client: "pg",
  6. connection: {
  7. host: "localhost",
  8. user: "pi",
  9. password: "password",
  10. database: "homestatus"
  11. }
  12. });
  13. const bookshelf = require("bookshelf")(Knex);
  14.  
  15. exports.up = function(knex, Promise) {
  16. return knex.schema.createTableIfNotExists('users', function(table) {
  17. table.increments('id').notNullable().primary();
  18. table.string('email').notNullable().unique();
  19. table.string('token').unique();
  20. table.string('passwordDigest').notNullable();
  21. table.timestamps();
  22. }).createTableIfNotExists('temperatures', function(table) {
  23. table.increments('id').notNullable().primary();
  24. table.string('main_topic');
  25. table.string('data_topic');
  26. table.float('data');
  27. table.timestamps();
  28. });
  29. };
  30.  
  31. exports.up(Knex)
  32. .then(() => {
  33. console.log('Table created sucessfully');
  34. })
  35. .catch((err) => { console.log('\n opps little error keep going. \n\n\n\n\n\n\n');});
  36.  
  37. let User = bookshelf.Model.extend({
  38. tableName: 'users',
  39. initialize: function() {
  40. this.on('creating', this.setPassword, this);
  41. //this.on('updating', this.setPassword, this);
  42. },
  43. setPassword: function (password) {
  44. // hmmm something like this might be needed
  45. // password ? password.attributes.password || password;
  46. console.log('password: \n ', password, '\n');
  47. console.log('this: \n ', this, '\n');
  48. var _this = this;
  49. return new Promise((resolve, reject) =>
  50. bcrypt.genSalt(null, (err, salt) =>
  51. err ? reject(err) : resolve(salt))
  52. )
  53. .then((salt) =>
  54. new Promise((resolve, reject) =>
  55. // bcrypt.hash(password.attributes.password, salt, (err, data) =>
  56. bcrypt.hash(password, salt, (err, data) =>
  57. err ? reject(err) : resolve(data)))
  58. )
  59. .then((digest) => {
  60. _this.attributes.passwordDigest = digest;
  61. delete _this.attributes.password;
  62. console.log('setPassword Save: \n ',_this.attributes);
  63. return _this.attributes;
  64. });
  65. },
  66. comparePassword: function (password) {
  67. var _this = this;
  68.  
  69. return new Promise((resolve, reject) =>
  70. bcrypt.genSalt(null, (err, salt) =>
  71. err ? reject(err) : resolve(salt))
  72. ).then((salt) =>
  73. new Promise((resolve, reject) =>
  74. bcrypt.hash(password, salt, (err, data) =>
  75. err ? reject(err) : resolve(data)))
  76. ).then((digest) => {
  77. _this.attributes.passwordDigest = digest;
  78. //console.log('Compare Password this: \n', this , '\n');
  79. return _this.attributes;
  80. });
  81. },
  82. });
  83.  
  84. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement