Skip to content

Commit a98ed30

Browse files
author
Ian Redpath
committed
new try with different bcrypt lib
1 parent 6ea6fdf commit a98ed30

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"dependencies": {
2626
"async": "^1.5.2",
27-
"bcrypt": "^0.8.5",
27+
"bcrypt-nodejs": "0.0.3",
2828
"body-parser": "^1.15.0",
2929
"cookie-parser": "^1.4.1",
3030
"express": "^4.13.4",
@@ -43,7 +43,6 @@
4343
"gulp-sourcemaps": "^1.6.0",
4444
"gulp-typescript": "^2.11.0",
4545
"gulpclass": "0.0.8",
46-
"node-gyp": "^3.3.1",
4746
"run-sequence": "^1.1.5",
4847
"rxjs": "5.0.0-beta.2",
4948
"tsc": "^1.20150623.0",

server/models/user.model.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as mongoose from 'mongoose'
22
import * as Q from 'q'
33
import * as _ from 'lodash'
4-
import * as bcrypt from 'bcrypt'
4+
import * as bcrypt from 'bcrypt-nodejs'
55

66
import UserSchema from './user.schema.server'
77
export default class UserModel {
@@ -18,7 +18,8 @@ export default class UserModel {
1818
create(newUser) {
1919
let deferred = Q.defer()
2020
// encrypt password before creating
21-
bcrypt.hash(newUser.password, 10, (err, hash) => {
21+
22+
bcrypt.hash(newUser.password, null, null, (err, hash) => {
2223
if (err) {
2324
deferred.reject(err)
2425
} else {
@@ -54,7 +55,7 @@ export default class UserModel {
5455
update(id: number, what) {
5556
let deferred = Q.defer()
5657
// encrypt password before creating
57-
bcrypt.hash(what.password, 10, (err, hash) => {
58+
bcrypt.hash(what.password, null, null, (err, hash) => {
5859
if (err) {
5960
deferred.reject(err)
6061
} else {
@@ -97,20 +98,26 @@ export default class UserModel {
9798

9899
findUserByCredentials(username: string, password: string) {
99100
let deferred = Q.defer()
100-
console.log(`${username}, ${password}`)
101101
this.findUserByUsername(username)
102102
.then(user => {
103-
console.log(user)
103+
if (!user) {
104+
deferred.reject('invalid username')
105+
}
104106
bcrypt.compare(password, (<any>user).password, (err, res) => {
105107
if (err) {
106108
deferred.reject(err)
107109
} else if (!res) {
108-
deferred.reject({ message: 'invalid password' })
110+
// we may be comparing two encrypted passwords
111+
if (password === (<any>user).password) {
112+
deferred.resolve(user)
113+
} else {
114+
deferred.reject('invalid password')
115+
}
109116
} else {
110117
deferred.resolve(user)
111118
}
112119
})
113-
})
120+
}, error => { deferred.reject(error.message) })
114121
return deferred.promise
115122
}
116123
}

server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import PosterEndpoints from './project/endpoints/posterEndpoints'
3333

3434
const ipaddress:string = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
3535
const port:number = process.env.OPENSHIFT_NODEJS_PORT || 3000;
36-
let mongoConnectionString: string = "mongodb://127.0.0.1:27017/webdevevelopment3"
36+
let mongoConnectionString: string = "mongodb://127.0.0.1:27017/webdevevelopment4"
3737
if (process.env.OPENSHIFT_MONGODB_DB_PASSWORD) {
3838
const DB_USERNAME = process.env.OPENSHIFT_MONGODB_DB_USERNAME
3939
const DB_PASSWORD = process.env.OPENSHIFT_MONGODB_DB_PASSWORD

server/services/user.service.server.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ export default function Userendpoints(app, userModel: UserModel) {
66

77
// local strategy
88
const localStrat = (username, password, done) => {
9-
if (password.indexOf('$2a$10') == 0) { // encrypted check hack
10-
userModel.findUserByEncryptedCredentials(username, password)
9+
userModel.findUserByCredentials(username, password)
1110
.then(user => { return done(null, user ? user : false) },
1211
err => { return done(err) })
13-
} else {
14-
userModel.findUserByCredentials(username, password)
15-
.then(user => { return done(null, user ? user : false) },
16-
err => { return done(err) })
17-
}
1812
}
1913
passport.use('assignment', new local.Strategy(localStrat))
2014

typings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"ambientDependencies": {
33
"async": "github:DefinitelyTyped/DefinitelyTyped/async/async.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
4-
"bcrypt": "github:DefinitelyTyped/DefinitelyTyped/bcrypt/bcrypt.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
4+
"bcrypt-nodejs": "github:DefinitelyTyped/DefinitelyTyped/bcrypt-nodejs/bcrypt-nodejs.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
55
"body-parser": "github:DefinitelyTyped/DefinitelyTyped/body-parser/body-parser.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
66
"cookie-parser": "github:DefinitelyTyped/DefinitelyTyped/cookie-parser/cookie-parser.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
77
"del": "github:DefinitelyTyped/DefinitelyTyped/del/del.d.ts#9027703c0bd831319dcdf7f3169f7a468537f448",

0 commit comments

Comments
 (0)