-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (108 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const express = require("express");
const mongoose = require("mongoose");
const JwtStrategy = require("passport-jwt").Strategy,
ExtractJwt = require("passport-jwt").ExtractJwt;
const passport = require("passport");
const User = require("./models/User");
const authRoutes = require("./routes/auth");
const songRoutes = require("./routes/song");
const playlistRoutes = require("./routes/playlist");
const userRoutes = require("./routes/user")
require("dotenv").config();
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 8000;
app.use(cors({
origin: 'https://mern-app-frontend-phi.vercel.app',
credentials: true
}));
app.use(express.json());
// connect mongodb to our node app.
// mongoose.connect() takes 2 arguments : 1. Which db to connect to (db url), 2. 2. Connection options
mongoose
.connect(
"mongodb+srv://vanditdubeywork:" +
process.env.MONGO_PASSWORD +
"@cluster0.gibrwqu.mongodb.net/?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
//this is some new standard which we follow during creating projects
)
.then((x) => {
console.log("Connected to Database");
})
.catch((err) => {
console.log("Error connecting to Mongo !!!");
});
// setup passport-jwt
// let opts = {};
// opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
// opts.secretOrKey = process.env.PASSPORT_JWT_SECRETKEY;
// passport.use(
// new JwtStrategy(opts, function (jwt_payload, done) {
// User.findOne({_id: jwt_payload.identifier}, function (err, user) {
// // done(error, doesTheUserExist)
// if (err) {
// return done(err, false);
// }
// if (user) {
// return done(null, user);
// } else {
// return done(null, false);
// // or you could create a new account
// }
// });
// })
// );
// let opts = {};
// opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
// opts.secretOrKey = process.env.PASSPORT_JWT_SECRETKEY;
// passport.use(
// new JwtStrategy(opts, async (jwt_payload, done) => {
// try {
// const user = await User.findOne({ _id: jwt_payload.identifier });
// console.log("passport.use() activated");zz
// if (user) {
// return done(null, user);
// } else {
// console.log("inside passport.use() user not found");
// return done(null, false);
// // or you could create a new account
// }
// } catch (err) {
// return done(err, false);
// }
// })
// );
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = process.env.PASSPORT_JWT_SECRETKEY;
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findOne({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
// or you could create a new account
}
});
}));
// API : GET type : / : return text "Hello world"
app.get("/", (req, res) => {
// req contains all data for the request
// res contains all data for the response
res.send("Hello World");
});
app.use("/auth", authRoutes);
app.use("/song", songRoutes);
app.use("/playlist", playlistRoutes);
app.use("/user",userRoutes)
// Now we want to tell express that our server will run on localhost:8000
app.listen(PORT, () => {
console.log("App is running on port " + PORT);
});