@@ -8,8 +8,9 @@ var mysql = require('mysql');
88var bcrypt = require ( 'bcrypt-nodejs' ) ;
99var dbconfig = require ( './database' ) ;
1010var connection = mysql . createConnection ( dbconfig . connection ) ;
11-
11+ connection . connect ( ) ;
1212connection . query ( 'USE ' + dbconfig . database ) ;
13+ connection . end ( ) ;
1314// expose this function to our app using module.exports
1415module . exports = function ( passport ) {
1516
@@ -26,9 +27,11 @@ module.exports = function(passport) {
2627
2728 // used to deserialize the user
2829 passport . deserializeUser ( function ( id , done ) {
30+ connection . connect ( ) ;
2931 connection . query ( "SELECT * FROM users WHERE id = ? " , [ id ] , function ( err , rows ) {
3032 done ( err , rows [ 0 ] ) ;
3133 } ) ;
34+ connection . end ( ) ;
3235 } ) ;
3336
3437 // =========================================================================
@@ -48,6 +51,7 @@ module.exports = function(passport) {
4851 function ( req , username , password , done ) {
4952 // find a user whose email is the same as the forms email
5053 // we are checking to see if the user trying to login already exists
54+ connection . connect ( ) ;
5155 connection . query ( "SELECT * FROM users WHERE username = ?" , [ username ] , function ( err , rows ) {
5256 if ( err )
5357 return done ( err ) ;
@@ -62,14 +66,16 @@ module.exports = function(passport) {
6266 } ;
6367
6468 var insertQuery = "INSERT INTO users ( username, password ) values (?,?)" ;
65-
69+ connection . connect ( ) ;
6670 connection . query ( insertQuery , [ newUserMysql . username , newUserMysql . password ] , function ( err , rows ) {
6771 newUserMysql . id = rows . insertId ;
6872
6973 return done ( null , newUserMysql ) ;
7074 } ) ;
75+ connection . end ( ) ;
7176 }
7277 } ) ;
78+ connection . end ( ) ;
7379 } )
7480 ) ;
7581
@@ -88,6 +94,7 @@ module.exports = function(passport) {
8894 passReqToCallback : true // allows us to pass back the entire request to the callback
8995 } ,
9096 function ( req , username , password , done ) { // callback with email and password from our form
97+ connection . connect ( ) ;
9198 connection . query ( "SELECT * FROM users WHERE username = ?" , [ username ] , function ( err , rows ) {
9299 if ( err )
93100 return done ( err ) ;
@@ -102,6 +109,7 @@ module.exports = function(passport) {
102109 // all is well, return successful user
103110 return done ( null , rows [ 0 ] ) ;
104111 } ) ;
112+ connection . end ( ) ;
105113 } )
106114 ) ;
107115} ;
0 commit comments