Skip to content

Commit bc46ed6

Browse files
committed
modified: .gitignore
modified: README.md new file: TODOs.md new file: config/passport-setup.js modified: index.js new file: models/user-models.js modified: package-lock.json modified: package.json modified: public/README.md new file: routes/auth-routes.js new file: routes/profile-routes.js
1 parent db72750 commit bc46ed6

File tree

11 files changed

+10110
-6161
lines changed

11 files changed

+10110
-6161
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ index.copy.js
99
public/tests
1010
currencyRate.js
1111
views/index-temp.ejs
12+
config/keys.js

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ There are weather detection stations situated in and around you. The inner work
2424

2525
<br />
2626

27-
> Below is the template for a rudimentary completion of an example on interfacing with a weather data provider. <br /><span style="color:green">For indepth logics and real life data implementation, continue to</span> [here](public/README.md).
27+
> Below is the template for a rudimentary completion of an example on interfacing with a weather data provider. <br /><span style="color:green">For indepth logics and real life data implementation, continue to</span> [HERE!](public/README.md).
2828
2929
<br />
3030

@@ -49,7 +49,7 @@ First, you will need to change your `package.json` to include:
4949

5050
Then, use imports in your `index.js`. Here is an express start with dotenv:
5151

52-
```c
52+
```javascript
5353
import dotenv from "dotenv"
5454
import express from "express"
5555

@@ -218,7 +218,7 @@ We will choose [Selenium IDE chrome extension](https://chrome.google.com/webstor
218218

219219
4. Install [`selenium-side-runner`](https://www.selenium.dev/selenium-ide/docs/en/introduction/command-line-runner), then run the test script above from the command console.
220220

221-
```c
221+
```c#
222222
StagingProjs> selenium-side-runner .\public\tests\SeleniumIDE_ChromeTestWeatherBitApp.side -c "browserName=chrome"
223223

224224
info: Running test UITests

TODOs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# TODOs
2+
3+
1. Clean up preparing for passport integration
4+
5+
2. Setup OAuth routes
6+
7+
3. Configure Passport, setup routes
8+
9+
4. Set up strategy: { Google+ API, LocalStrategy}
10+
11+
5. setup user model integrating with mongodb

config/passport-setup.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const passport = require('passport');
2+
const GoogleStrategy = require('passport-google-oauth20');
3+
const User = require('../models/user-models');
4+
const keys = require ('./keys');
5+
6+
// serialize user structure in the mongodb
7+
passport.serializeUser((user, done) => {
8+
done(null, user.id);
9+
});
10+
11+
passport.deserializeUser((id, done) => {
12+
try {
13+
User.findById(id).then((user) => {
14+
done(null, user);
15+
})
16+
} catch (err) {
17+
console.log(err);
18+
}
19+
});
20+
21+
22+
/*
23+
* Note: Passport google strategy required clientID and clientSecret
24+
* which can be obtained from registering within your google account.
25+
* There is a configuration file `keys.js` containing the above info code,
26+
* which is not part of this source code.
27+
*
28+
* Go to https://console.cloud.google.com/ to create your own clientID and clientSecret.
29+
*/
30+
passport.use(new GoogleStrategy({
31+
// options for google strategy
32+
callbackURL: '/auth/google/redirect',
33+
clientID: keys.google.clientID,
34+
clientSecret: keys.google.clientSecret
35+
}, (accessToken, refreshToken, profile, done) => {
36+
// passport callback function
37+
// console.log('accessToken: ' + accessToken);
38+
39+
// console.log(profile);
40+
41+
// now that the google profile is here, let's store it in database. However, to avoid duplicates in mongoDB collection, we perform dbase checking first.
42+
43+
//1. Check if user exists in mongoDb
44+
User.findOne({googleId: profile.id}).then((currentUser) => {
45+
if (currentUser) {
46+
// found user already saved previously, proceed.
47+
// console.log('user is: ' + currentUser);
48+
done(null, currentUser); // trigger serializer
49+
} else {
50+
// if not, this is then the first time this user logon
51+
new User({
52+
username: profile.displayName,
53+
googleId: profile.id,
54+
thumbnail: profile._json.picture
55+
}).save().then((newUser) => {
56+
console.log('New user created: ' + newUser);
57+
done(null, newUser); // trigger the serializer method
58+
});
59+
}
60+
})
61+
62+
}));

index.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import * as dotenv from 'dotenv';
2-
let WEATHERBIT_KEY = "";
3-
let WEATHERBIT_URI = "";
4-
5-
dotenv.config();
6-
7-
// dotenv.config()
82
import request from 'request';
93
import express from 'express';
104
import bodyParser from 'body-parser';
115
import {encryptAES, decryptAES} from './crypto.js';
12-
136
import { data as currencyCodes } from 'currency-codes';
147
import awssdk from 'aws-sdk';
158

9+
// application secrets
10+
import { keys } from './config/keys.js';
1611

12+
let WEATHERBIT_KEY = "";
13+
let WEATHERBIT_URI = "";
1714

15+
dotenv.config();
1816

1917
if (process.env.NODE_ENV === 'awsdeploy') {
2018
WEATHERBIT_URI="https://api.weatherbit.io/v2.0/";
2119
WEATHERBIT_KEY="U2FsdGVkX18HMV5UUT9rJN76hOtIHDw1bH0beQYWH8a6E7uzKqskdgHvc6Nq2lO6O+GAb2vrcL+X8ZDqcGPuLw==";
22-
console.log("AWSDEPLOY mode!");
20+
console.log("AWSDEPLOY mode!");
2321
} else {
2422
// Code for Development Mode
25-
WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
26-
WEATHERBIT_URI = process.env.WEATHERBIT_URI;
23+
// WEATHERBIT_KEY = process.env.WEATHERBIT_KEY;
24+
// WEATHERBIT_URI = process.env.WEATHERBIT_URI;
25+
26+
WEATHERBIT_KEY = keys.weatherbitapi.APIKEY;
27+
WEATHERBIT_URI = keys.weatherbitapi.URI;
2728
}
2829

2930

31+
3032
// Create network routing
3133
const app = express();
3234

@@ -36,12 +38,11 @@ app.set('view engine', 'ejs');
3638
// Allow access to 'public' folder where resources are available to this app
3739
app.use(express.static('public'));
3840

41+
// Parse incoming request bodies in a middleware before your handlers, available under the `req.body` property.
3942
app.use(bodyParser.urlencoded({ extended: true }));
4043

4144
// get the locale from the client-side via the ejs form
4245
app.get('/', (req, res) => {
43-
// console.dir(req.params);
44-
// console.dir(req.body);
4546
let apikey = encryptAES(WEATHERBIT_KEY);
4647
res.render('index', {xkey: apikey});
4748
})
@@ -454,6 +455,13 @@ let port = process.env.PORT || 3000;
454455

455456
// creating a server that is listening on ${port} for connections.
456457
app.listen(port, () => {
457-
console.log(`TechRolEmi weather report is listening on port ${port}`);
458+
console.log(`TechRolEmi is listening on port ${port}`);
459+
}).on('error', (e) => {
460+
console.log('Error happened: ', e.message);
461+
// try different port
462+
port = process.env.PORT2;
463+
app.listen(port, () => {
464+
console.log(`TechRolEmi is listening on port ${port}`);
465+
}
466+
)
458467
});
459-

models/user-models.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const mongoose = require('mongoose');
2+
const Schema = mongoose.Schema;
3+
4+
const userSchema = new Schema({
5+
username: String,
6+
googleId: String,
7+
thumbnail: String
8+
});
9+
10+
const User = mongoose.model('user', userSchema);
11+
12+
module.exports = User;

0 commit comments

Comments
 (0)