SHOW:
|
|
- or go back to the newest paste.
| 1 | - | var mongoose = require("mongoose");
|
| 1 | + | //includes express, mongoose, User, constants. this part is ok. |
| 2 | - | var Schema = mongoose.Schema; |
| 2 | + | |
| 3 | - | var constants = require("../config/constants");
|
| 3 | + | /** |
| 4 | - | var bcrypt = require("bcrypt-nodejs");
|
| 4 | + | * Authenticates a user post request |
| 5 | * | |
| 6 | * @request email string | |
| 7 | - | var UserSchema = new Schema({
|
| 7 | + | * @request password string |
| 8 | - | name: String, |
| 8 | + | * |
| 9 | - | email: String, |
| 9 | + | */ |
| 10 | - | password: String, |
| 10 | + | router.post("/", (req, res) => {
|
| 11 | - | authorization: |
| 11 | + | |
| 12 | * Retrieve the user | |
| 13 | - | type: Number, |
| 13 | + | |
| 14 | - | default: constants.authorization.default |
| 14 | + | User.find( |
| 15 | - | } |
| 15 | + | |
| 16 | email: req.body.email | |
| 17 | }, | |
| 18 | - | UserSchema.pre("save", (next) => {
|
| 18 | + | (err, user) => |
| 19 | - | var user = this; |
| 19 | + | |
| 20 | /** | |
| 21 | * An error occurred | |
| 22 | - | * Only hash the password when it's been modified or if new. |
| 22 | + | */ |
| 23 | if (err) | |
| 24 | - | if (!user.isModified("password"))
|
| 24 | + | |
| 25 | res.json({
| |
| 26 | - | return next(); |
| 26 | + | success: false, |
| 27 | - | } |
| 27 | + | message: "An mongodb error occurred.", |
| 28 | error: err | |
| 29 | }); | |
| 30 | - | * hash password |
| 30 | + | |
| 31 | ||
| 32 | - | bcrypt.hash(user.password, null, null, (err, hash) => {
|
| 32 | + | /** |
| 33 | * Check for problems with the email or password. | |
| 34 | * Or an error might have happened. | |
| 35 | - | return next(err); |
| 35 | + | */ |
| 36 | if (!user || (user && !user.verifyPassword(req.body.password, (err, match) => match))) | |
| 37 | // the code breaks here, telling me that user.verify... is not a function | |
| 38 | - | user.password = hash; |
| 38 | + | |
| 39 | - | return next(); |
| 39 | + | res.json({
|
| 40 | success: false, | |
| 41 | message: "Email or password was incorrect." | |
| 42 | }); | |
| 43 | - | UserSchema.methods.verifyPassword = (reqPassword, callback) => {
|
| 43 | + | |
| 44 | - | bcrypt.compare(reqPassword, this.password, (err, match) => {
|
| 44 | + | |
| 45 | - | var e = null; |
| 45 | + | /** |
| 46 | - | var m = match; |
| 46 | + | * User authenticated! |
| 47 | */ | |
| 48 | req.session.user = user; | |
| 49 | res.json({
| |
| 50 | - | e = err; |
| 50 | + | success: true, |
| 51 | - | m = false; |
| 51 | + | message: "Successfully authenticated." |
| 52 | }); | |
| 53 | }); | |
| 54 | - | return callback(e, m); |
| 54 | + | |
| 55 | ||
| 56 | - | }; |
| 56 | + | router.get("/", (req, res, next) => {
|
| 57 | var admin = new User({
| |
| 58 | name: "admin", | |
| 59 | email: "[email protected]", | |
| 60 | - | module.exports = mongoose.model("User", UserSchema); |
| 60 | + | password: "admin", |
| 61 | authorization: constants.authorization.admin | |
| 62 | }); | |
| 63 | ||
| 64 | // the code throws an error here, saying that isModified(...) is not a function | |
| 65 | admin.save(function(err) {
| |
| 66 | if (err) | |
| 67 | {
| |
| 68 | console.log(err); | |
| 69 | } | |
| 70 | ||
| 71 | console.log('User saved successfully');
| |
| 72 | res.json({ success: true });
| |
| 73 | }); | |
| 74 | }); |