Skip to content

Commit 4d66d76

Browse files
Add db url
1 parent 39bd446 commit 4d66d76

File tree

7 files changed

+24
-26
lines changed

7 files changed

+24
-26
lines changed

.env.example

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Authentication
22

33
JWT_SECRET=
4-
JWT_EXPIRATION_DAYS=7
4+
JWT_EXPIRATION_DAYS=7
5+
6+
#DATABASE
7+
DB_URL=
8+
ALLOWED_ORIGIN=http://localhost:3000

app.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const express = require("express");
22
const bodyParser = require("body-parser");
33
const cors = require("cors");
4-
const routes = require('./routes');
4+
const routes = require("./routes");
55

66
const app = express();
77

8-
var corsOptions = {
9-
origin: "http://localhost:8081"
10-
};
8+
const config = require("./app/config/enviroment");
119

12-
app.use(cors(corsOptions));
10+
app.use(cors({ origin: new RegExp(config.ALLOWED_ORIGIN + ".*") }));
1311

1412
app.use(bodyParser.json());
1513

@@ -19,22 +17,18 @@ const db = require("./app/models");
1917
db.mongoose
2018
.connect(db.url, {
2119
useNewUrlParser: true,
22-
useUnifiedTopology: true
20+
useUnifiedTopology: true,
2321
})
2422
.then(() => {
2523
console.log("Connected to the database!");
2624
})
27-
.catch(err => {
25+
.catch((err) => {
2826
console.log("Cannot connect to the database!", err);
2927
process.exit();
3028
});
31-
3229
// Routes
3330
app.use(routes);
3431

35-
app.get("/", (req, res) => {
36-
res.json({ message: "Welcome to Bubbles!" });
37-
});
3832

3933
// set port, listen for requests
4034
const PORT = process.env.PORT || 8080;

app/config/db.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const config = require('./enviroment');
2+
13
module.exports = {
2-
url: "mongodb://localhost:27017/bubbles_db"
4+
url: config.DB_URL
35
};

app/config/enviroment.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ const environment = {
55
JWT: {
66
secret: process.env.JWT_SECRET,
77
expiration_days: process.env.JWT_EXPIRATION_DAYS
8-
}
8+
},
9+
DB_URL: process.env.DB_URL,
10+
ALLOWED_ORIGIN: process.env.ALLOWED_ORIGIN
911
},
1012

1113
staging: {
1214
JWT: {
1315
secret: process.env.JWT_SECRET,
1416
expiration_days: process.env.JWT_EXPIRATION_DAYS
15-
}
17+
},
18+
DB_URL: process.env.DB_URL,
19+
ALLOWED_ORIGIN: process.env.ALLOWED_ORIGIN
1620
},
1721

1822
production: {
1923
JWT: {
2024
secret: process.env.JWT_SECRET,
2125
expiration_days: process.env.JWT_EXPIRATION_DAYS
22-
}
26+
},
27+
DB_URL: process.env.DB_URL,
28+
ALLOWED_ORIGIN: process.env.ALLOWED_ORIGIN
2329
}
2430
}
2531

app/models/user.model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ UserSchema.pre('save', async function save(next) {
2525
});
2626

2727
UserSchema.methods.validatePassword = async function validatePassword(data) {
28-
return await bcrypt.compareSync(data, this.password);
28+
return bcrypt.compareSync(data, this.password);
2929
};
3030

3131
UserSchema.methods.generateAuthToken = function () {

app/services/auth.service.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ async function login(mail, password) {
99
if (!user){
1010
return null;
1111
}
12-
12+
1313
const validPassword = await user.validatePassword(password);
14-
console.log(validPassword);
1514
if (!validPassword){
1615
return null;
1716
}

routes.js

-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@ const authMiddleware = require('./app/middlewares/auth');
66
const auth = require('./app/routes/auth.route');
77
const user = require('./app/routes/user.route');
88

9-
router.get('/', (req, res) => {
10-
return res.send('Bubbles API');
11-
});
12-
139
router.use('/user', user);
1410
router.use('/auth', auth);
1511
// Private routes
1612
router.use(authMiddleware);
1713

18-
19-
20-
2114
module.exports = router;

0 commit comments

Comments
 (0)