@@ -8,8 +8,9 @@ var mysql = require('mysql');
8
8
var bcrypt = require ( 'bcrypt-nodejs' ) ;
9
9
var dbconfig = require ( './database' ) ;
10
10
var connection = mysql . createConnection ( dbconfig . connection ) ;
11
-
11
+ connection . connect ( ) ;
12
12
connection . query ( 'USE ' + dbconfig . database ) ;
13
+ connection . end ( ) ;
13
14
// expose this function to our app using module.exports
14
15
module . exports = function ( passport ) {
15
16
@@ -26,9 +27,11 @@ module.exports = function(passport) {
26
27
27
28
// used to deserialize the user
28
29
passport . deserializeUser ( function ( id , done ) {
30
+ connection . connect ( ) ;
29
31
connection . query ( "SELECT * FROM users WHERE id = ? " , [ id ] , function ( err , rows ) {
30
32
done ( err , rows [ 0 ] ) ;
31
33
} ) ;
34
+ connection . end ( ) ;
32
35
} ) ;
33
36
34
37
// =========================================================================
@@ -48,6 +51,7 @@ module.exports = function(passport) {
48
51
function ( req , username , password , done ) {
49
52
// find a user whose email is the same as the forms email
50
53
// we are checking to see if the user trying to login already exists
54
+ connection . connect ( ) ;
51
55
connection . query ( "SELECT * FROM users WHERE username = ?" , [ username ] , function ( err , rows ) {
52
56
if ( err )
53
57
return done ( err ) ;
@@ -62,14 +66,16 @@ module.exports = function(passport) {
62
66
} ;
63
67
64
68
var insertQuery = "INSERT INTO users ( username, password ) values (?,?)" ;
65
-
69
+ connection . connect ( ) ;
66
70
connection . query ( insertQuery , [ newUserMysql . username , newUserMysql . password ] , function ( err , rows ) {
67
71
newUserMysql . id = rows . insertId ;
68
72
69
73
return done ( null , newUserMysql ) ;
70
74
} ) ;
75
+ connection . end ( ) ;
71
76
}
72
77
} ) ;
78
+ connection . end ( ) ;
73
79
} )
74
80
) ;
75
81
@@ -88,6 +94,7 @@ module.exports = function(passport) {
88
94
passReqToCallback : true // allows us to pass back the entire request to the callback
89
95
} ,
90
96
function ( req , username , password , done ) { // callback with email and password from our form
97
+ connection . connect ( ) ;
91
98
connection . query ( "SELECT * FROM users WHERE username = ?" , [ username ] , function ( err , rows ) {
92
99
if ( err )
93
100
return done ( err ) ;
@@ -102,6 +109,7 @@ module.exports = function(passport) {
102
109
// all is well, return successful user
103
110
return done ( null , rows [ 0 ] ) ;
104
111
} ) ;
112
+ connection . end ( ) ;
105
113
} )
106
114
) ;
107
115
} ;
0 commit comments