Skip to content

Commit

Permalink
change mail options to OAuth and update database url
Browse files Browse the repository at this point in the history
  • Loading branch information
apanjain committed Dec 4, 2020
1 parent 2c95395 commit 374d621
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
17 changes: 12 additions & 5 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
PORT =
IP =
DATABASEURL =
GMAILID =
GMAILPW =
PORT=3000

DB_HOST=
DB_NAME=
DB_USERNAME=
DB_PASSWORD=
DB_PORT=

OAUTH_CLIENT_ID=
OAUTH_CLIENT_SECRET=
OAUTH_CLIENT_REFRESH_TOKEN=
OAUTH_CLIENT_EMAIL=
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# sparkTest
# Authentication Portal Sample

## Setup

- Clone this repository

- Install [MongoDb](https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04)
- Create Role
```sh
db.createRole({ createRole: <Role Name>, privileges: [ {
resource: { db: <Database Name>, collection: "" },
actions: [ "find","insert","update","createIndex","createCollection","remove" ]}
], roles: [{ role: "read", db: <Database Name>}] })
```
- Create User
```sh
- db.createUser({"user" : <User name>,pwd: <Password>, "roles" : [{"role" : <Role Name Created>, "db" : <Database Name>}]})
```
- Install npm packages
```sh
- npm install
```
- Make a .env file with the same fields as .env.sample
```sh
- cp .env.sample .env
```
- Fill the .env file accordingly
### For OAuth Credentials
- Use your oauth secrets by registering [here](https://console.developers.google.com/)
## Using the app
- Run the app
```sh
- npm start
```
- Visit the url as mentioned in the terminal, usually http://localhost:3000
A working model for this website is available at https://spark-sampl.herokuapp.com/
22 changes: 18 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ var express = require("express"),

require('dotenv').config();

var url = process.env.DATABASEURL || 'mongodb://localhost:27017/sparkTest'

mongoose.connect(url, {useNewUrlParser: true});
// mongoose setup

var dbHost = process.env.DB_HOST || 'localhost';
var dbName = process.env.DB_NAME;
var dbUser = process.env.DB_USERNAME;
var dbPass = process.env.DB_PASSWORD;
var dbPort = process.env.DB_PORT || "27017";

var url = "mongodb://" + dbUser + ":" + dbPass + "@" + dbHost + ":" + dbPort + "/" + dbName;

mongoose
.connect(url, {useNewUrlParser: true})
.then(() => console.log("Connected To Database"))
.catch(err => console.error(err));

mongoose.set('useFindAndModify', false);
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(__dirname+"/public"));
Expand Down Expand Up @@ -52,6 +65,7 @@ app.use("/", userRoutes);
app.use("/", forgotRoutes);

//Server
app.listen(process.env.PORT, process.env.IP,function(){
console.log(`Server started on PORT ${process.env.PORT}`);
app.listen(process.env.PORT,function(){
console.log(`Server started on PORT ${process.env.PORT}`);
console.log(`Visit http://localhost:${process.env.PORT}`);
});
14 changes: 11 additions & 3 deletions routes/forgot.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ router.post('/forgot',middleware.isLoggedOut, function(req, res, next) {
});
},
function(token, user, done) {
var clientId = process.env.OAUTH_CLIENT_ID
var clientSecret = process.env.OAUTH_CLIENT_SECRET
var refreshToken = process.env.OAUTH_CLIENT_REFRESH_TOKEN
var userMailID = process.env.OAUTH_CLIENT_EMAIL

var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: "smtp.gmail.com",
auth: {
user: process.env.GMAILID,
pass: process.env.GMAILPW,
type: "OAuth2",
user: userMailID,
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken
}
});
var mailOptions = {
Expand Down

0 comments on commit 374d621

Please sign in to comment.