@@ -2,6 +2,11 @@ import * as dotenv from 'dotenv';
22import request from 'request' ;
33import express from 'express' ;
44import bodyParser from 'body-parser' ;
5+ import cookieSession from 'cookie-session' ;
6+ import passport from 'passport' ;
7+ import mongoose from 'mongoose' ;
8+ import { compile } from 'ejs' ;
9+ import { serialize , deserialize , googleStrategy } from './config/passport-setup.js' ;
510import { encryptAES , decryptAES } from './crypto.js' ;
611
712// dev env
@@ -16,25 +21,27 @@ let WEATHERBIT_KEY;
1621let WEATHERBIT_URI = 'https://api.weatherbit.io/v2.0/' ;
1722let GoogleclientID ;
1823let GoogleclientSecret ;
24+ let MongoDBConString ;
25+ let CookieKey = 'c93da061ab7f984267e36c8431645035d611bc892c58f0e64614c68a4384a179126e7ed0b5829e460f292f72e9ef4facb68a0894cb2425ba046b82a3bae0b529' ;
26+
1927if ( process . env . NODE_ENV === "production" ) {
2028 EXCHANGE_RATE_APIKEY = process . env . EXCHANGE_RATE_APIKEY ;
2129 WEATHERBIT_KEY = process . env . WEATHERBIT_KEY ;
2230 GoogleclientID = process . env . GoogleclientID ;
2331 GoogleclientSecret = process . env . GoogleclientSecret ;
24-
32+ MongoDBConString = process . env . MONGODB_URI ;
2533} else {
2634 // dynamically importing keys.js using promise:
2735 import ( './config/keys.js' ) . then ( ( secrets ) => {
2836 EXCHANGE_RATE_APIKEY = secrets . keys . exchangerateapi . EXCHANGE_APIKEY ;
2937 WEATHERBIT_KEY = secrets . keys . weatherbitapi . WEATHERBIT_APIKEY ;
3038 GoogleclientID = secrets . keys . google . clientID ;
3139 GoogleclientSecret = secrets . keys . google . clientSecret ;
40+ MongoDBConString = secrets . keys . mongodb . connectionString ;
3241 } ) ;
33-
3442}
3543
3644
37-
3845// Create network routing
3946const app = express ( ) ;
4047
@@ -48,6 +55,8 @@ import { router as weatherbitRoutes } from './routes/weatherbit-routes.js';
4855// routes handler for OAuth20 methods
4956import { router as oauth20Routes } from './routes/auth-routes.js' ;
5057
58+ // routes handler for profile
59+ import { router as profileRoutes } from './routes/profile-routes.js' ;
5160
5261// EJS is accessed by default in the views directory.
5362app . set ( 'view engine' , 'ejs' ) ;
@@ -58,6 +67,29 @@ app.use(express.static('public'));
5867// Parse incoming request bodies in a middleware before your handlers, available under the `req.body` property.
5968app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
6069
70+
71+
72+ // cookie!
73+ app . use ( cookieSession ( {
74+ maxAge : 24 * 60 * 60 * 1000 ,
75+ keys : [ CookieKey ]
76+ } ) ) ;
77+
78+
79+ // Initialize passport
80+ app . use ( passport . initialize ( ) ) ;
81+ app . use ( passport . session ( ) ) ;
82+
83+
84+ // connect to mongodb
85+ mongoose . connect ( MongoDBConString , ( ) => {
86+ console . log ( 'Connected to MongoDB.' ) ;
87+ } ) ;
88+
89+
90+
91+
92+
6193// Set up all routes related to currency exchange rate methods
6294app . use ( '/exchange' , currencyExchangeRoutes ) ;
6395
@@ -67,14 +99,17 @@ app.use('/weatherbit', weatherbitRoutes);
6799// Set up all routes related to oauth2 methods
68100app . use ( '/auth' , oauth20Routes ) ;
69101
102+ // set up routes for user info after logged in
103+ app . use ( '/profile' , profileRoutes ) ;
104+
70105// Homepage: get the locale from the client-side via the ejs form
71106app . get ( '/' , ( req , res ) => {
72- res . render ( 'index' , { xkey : 'hey' } ) ;
107+ res . render ( 'index' , { user : req . user , xkey : 'hey' } ) ;
73108} )
74109
75110// about page
76111app . get ( '/about' , function ( req , res ) {
77- res . render ( 'pages/about' ) ;
112+ res . render ( 'pages/about' , { user : req . user } ) ;
78113} ) ;
79114
80115
0 commit comments