Created
April 6, 2016 14:10
-
-
Save edesilets/bfd95fab84fa913df5b1a50c37d897a6 to your computer and use it in GitHub Desktop.
Code as it currently stands. express/app/controllers/users.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// express/app/controllers/users.js | |
'use strict'; | |
const debug = require('debug')('thermal-monitor:users'); | |
const controller = require('express/lib/wiring/controller'); | |
const models = require('express/app/models'); | |
const User = models.user; | |
const crypto = require('crypto'); | |
const authenticate = require('./concerns/authenticate'); | |
const HttpError = require('express/lib/wiring/http-error'); | |
const getToken = () => | |
new Promise((resolve, reject) => | |
crypto.randomBytes(16, (err, data) => | |
err ? reject(err) : resolve(data.toString('base64')) | |
) | |
); | |
const userFilter = { passwordDigest: 0, token: 0 }; | |
const index = (req, res, next) => { | |
User.find({}, userFilter) | |
.then(users => res.json({ users })) | |
.catch(err => next(err)); | |
}; | |
const show = (req, res, next) => { | |
User.findById(req.params.id, userFilter) | |
.then(user => user ? res.json({ user }) : next()) | |
.catch(err => next(err)); | |
}; | |
const makeErrorHandler = (res, next) => | |
error => | |
error && error.name && error.name === 'ValidationError' ? | |
res.status(400).json({ error }) : | |
next(error); | |
const signup = (req, res, next) => { | |
let credentials = req.body.credentials; | |
let user = { | |
email: credentials.email, | |
password: credentials.password, | |
}; | |
getToken().then(token => | |
user.token = token | |
) | |
.then(() => { | |
return new User(user).save(); | |
}) | |
.then(newUser => { | |
let user = newUser.attributes; | |
delete user.passwordDigest; | |
res.json({ user }); | |
}) | |
.catch(makeErrorHandler(res, next)); | |
}; | |
const signin = (req, res, next) => { | |
let credentials = req.body.credentials; | |
let search = { email: credentials.email }; | |
new User(search).fetch() | |
.then(user => | |
user ? user.comparePassword(credentials.password) : Promise.reject(new HttpError(404)) | |
) | |
.then(user => { | |
getToken().then(token => { | |
user.token = token; | |
return new User(search).save(user, { patch: true }) | |
.then((userData) => { | |
let user = userData.attributes; | |
delete user.passwordDigest; | |
// console.log('yayaya user obj: \n', user); | |
res.json({ user }); | |
}); | |
}); | |
}) | |
.catch(makeErrorHandler(res, next)); | |
}; | |
const signout = (req, res, next) => { | |
getToken().then((token) => { | |
let findUser = { | |
id: req.params.id, | |
token: req.currentUser.token | |
}; | |
new User(findUser) | |
.save({ | |
token: token | |
},{ | |
patch: true | |
}); | |
}) | |
.then((user) => | |
user ? res.sendStatus(200) : next() | |
).catch(next); | |
}; | |
const changepw = (req, res, next) => { | |
debug('Changing password'); | |
let credentials = req.body.passwords; | |
let search = { | |
id: req.params.id, | |
token: req.currentUser.token, | |
}; | |
// console.log('search for who??: \n ', search); | |
//console.log('credentials.password: \n', credentials.new); | |
new User(search).fetch() | |
.then(user => | |
user ? user.comparePassword(credentials.old) : Promise.reject(new HttpError(404)) | |
) | |
.then((user) => { | |
console.log('user to query to set pw: \n', user, '\n'); | |
console.log('credentials.password.new: \n', credentials.new); | |
console.log('credentials.password.old: \n ', credentials.old); | |
new User(user).fetch().then(user => user.setPassword(credentials.new)).then(console.log)//.save(this, { patch: true }); | |
//new User(user).setPassword(req.body.passwords.new)//.save(user.passwordDigest, { patch: true }); | |
}) | |
.then((/* user */) => | |
res.sendStatus(200) | |
) | |
.catch(makeErrorHandler(res, next)); | |
}; | |
module.exports = controller({ | |
index, | |
show, | |
signup, | |
signin, | |
signout, | |
changepw, | |
}, { before: [ | |
{ method: authenticate, except: ['signup', 'signin'] }, | |
], }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment