Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const bcrypt = require('bcrypt');
- const Knex = require("knex")({
- client: "pg",
- connection: {
- host: "localhost",
- user: "pi",
- password: "password",
- database: "homestatus"
- }
- });
- const bookshelf = require("bookshelf")(Knex);
- exports.up = function(knex, Promise) {
- return knex.schema.createTableIfNotExists('users', function(table) {
- table.increments('id').notNullable().primary();
- table.string('email').notNullable().unique();
- table.string('token').unique();
- table.string('passwordDigest').notNullable();
- table.timestamps();
- }).createTableIfNotExists('temperatures', function(table) {
- table.increments('id').notNullable().primary();
- table.string('main_topic');
- table.string('data_topic');
- table.float('data');
- table.timestamps();
- });
- };
- exports.up(Knex)
- .then(() => {
- console.log('Table created sucessfully');
- })
- .catch((err) => { console.log('\n opps little error keep going. \n\n\n\n\n\n\n');});
- let User = bookshelf.Model.extend({
- tableName: 'users',
- initialize: function() {
- this.on('creating', this.setPassword, this);
- //this.on('updating', this.setPassword, this);
- },
- setPassword: function (password) {
- // hmmm something like this might be needed
- // password ? password.attributes.password || password;
- console.log('password: \n ', password, '\n');
- console.log('this: \n ', this, '\n');
- var _this = this;
- return new Promise((resolve, reject) =>
- bcrypt.genSalt(null, (err, salt) =>
- err ? reject(err) : resolve(salt))
- )
- .then((salt) =>
- new Promise((resolve, reject) =>
- // bcrypt.hash(password.attributes.password, salt, (err, data) =>
- bcrypt.hash(password, salt, (err, data) =>
- err ? reject(err) : resolve(data)))
- )
- .then((digest) => {
- _this.attributes.passwordDigest = digest;
- delete _this.attributes.password;
- console.log('setPassword Save: \n ',_this.attributes);
- return _this.attributes;
- });
- },
- comparePassword: function (password) {
- var _this = this;
- return new Promise((resolve, reject) =>
- bcrypt.genSalt(null, (err, salt) =>
- err ? reject(err) : resolve(salt))
- ).then((salt) =>
- new Promise((resolve, reject) =>
- bcrypt.hash(password, salt, (err, data) =>
- err ? reject(err) : resolve(data)))
- ).then((digest) => {
- _this.attributes.passwordDigest = digest;
- //console.log('Compare Password this: \n', this , '\n');
- return _this.attributes;
- });
- },
- });
- module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement