1
1
let path = require ( 'path' ) ;
2
2
let bodyparserencoder = require ( 'body-parser' ) ;
3
- //adding session
4
-
3
+ //connecting to db client
4
+ let dbconnect = require ( '../dbconnect/connect' ) ;
5
+ const MongoClient = dbconnect . MongoClient ;
6
+ const uri = dbconnect . uri ;
7
+ const client = dbconnect . client ;
8
+ dbconnect . main ( client ) ;
9
+ //adding session authentication
10
+ const loginauth = ( req , res , next ) => {
11
+ if ( req . session . loggedIn ) {
12
+ next ( ) ;
13
+ }
14
+ else {
15
+ res . redirect ( '/' ) ;
16
+ console . log ( 'looks like youve been logged out of the session login to continue' ) ;
17
+ }
18
+ } ;
5
19
bodyparserencoder = bodyparserencoder . urlencoded ( { extended : false } )
6
20
module . exports = function ( app ) {
7
21
app . get ( '/' , function ( req , res ) {
@@ -12,18 +26,18 @@ module.exports=function(app){
12
26
app . post ( '/login' , bodyparserencoder , function ( req , res ) {
13
27
console . log ( 'login requested' ) ;
14
28
let query = require ( '../' + 'dbconnect/' + 'login.js' ) ;
15
- let exists = query . login ( { username :req . body . Username , password :req . body . Password } , res , req ) ;
29
+ let exists = query . login ( { username :req . body . User , password :req . body . Pass } , res , req , client ) ;
16
30
//console.log('everything done, exists= ' + exists);
17
31
req . session . loggedIn = true ;
18
- console . log ( req . session ) ;
32
+ // console.log(req.session);
19
33
} ) ;
20
34
//request handling when signup is requested
21
35
app . post ( '/signup' , bodyparserencoder , function ( req , res ) {
22
36
console . log ( 'signup requested' ) ;
23
37
//if validated sucessfully then fire insert query in database
24
38
let query = require ( '../' + 'dbconnect/' + 'newsignin.js' ) ;
25
39
//console.log(req.body);
26
- query ( req . body . Username , req . body . Password , req . body . Fname , req . body . Lname , req . body . DOB , req . body . Email ) ;
40
+ query ( req . body . Username , req . body . Password , req . body . Fname , req . body . Lname , req . body . DOB , req . body . Email , client ) ;
27
41
//window.alert('signed up successfully ,now you can login');
28
42
//now logging in
29
43
query = require ( '../' + 'dbconnect/' + 'login.js' ) ;
@@ -34,30 +48,34 @@ module.exports=function(app){
34
48
/////////////////////////////////////////////// session pages ////////////////////////////////////////
35
49
//request handling when logout is requested
36
50
app . get ( '/logout' , function ( req , res ) {
37
- console . log ( req . session ) ;
51
+ // console.log(req.session);
38
52
if ( req . session . loggedIn ) {
39
- console . log ( 'logout requested' ) ;
40
- res . render ( 'Addmie.ejs' ) ; }
53
+ req . session . destroy ( ) ;
54
+ }
41
55
else {
42
- console . log ( 'login to continue' ) ;
56
+ // console.log('login to continue');
43
57
}
58
+ res . redirect ( '/' ) ;
59
+
44
60
} ) ;
45
61
//request handling when user posts
46
- app . post ( '/sendpost/:username' , bodyparserencoder , function ( req , res ) {
62
+ app . post ( '/sendpost/:username' , bodyparserencoder , loginauth , function ( req , res ) {
47
63
console . log ( 'post has been recived' ) ;
48
64
var query = require ( '../dbconnect/insertpost' ) ;
49
- query ( req ) ;
65
+ query ( req , client ) ;
50
66
res . redirect ( '/profile/' + req . params . username ) ;
51
67
} ) ;
52
- app . get ( '/profile/:username' , function ( req , res ) {
68
+ app . get ( '/profile/:username' , loginauth , function ( req , res ) {
53
69
console . log ( 'get request to profile' ) ;
54
70
var userobj = {
55
71
username :req . params . username ,
56
72
} ;
57
73
let fetch = require ( '../dbconnect/fetch.js' ) ;
58
74
const fname = null ;
59
- fetch ( req . params . username , res ) ;
75
+ fetch ( req . params . username , res , client ) ;
60
76
console . log ( 'profile loaded' ) ;
61
77
62
78
} ) ;
79
+ console . log ( 'closing connection to db' ) ;
80
+ client . close ( ) ;
63
81
}
0 commit comments