-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (120 loc) · 4.12 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const port = process.env.PORT || 5000;
const mongodbURL = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.btdla2l.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
const cors = require('cors');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const store = new MongoDBStore({
uri: mongodbURL,
collection: 'sessions',
});
const app = express();
// Set trust proxy to configure Express.js
// to trust the proxy server that your app is running behind
app.set('trust proxy', 1);
// Set up Cross-Origin Resource Sharing (CORS) middleware
app.use(
cors({
// Allow requests from http://localhost:3000
// origin: 'http://localhost:3000',
origin: [
'https://charitee-rj-tw.netlify.app',
'https://charitee-fe.vercel.app',
],
// Allow POST, PUT, GET, OPTIONS, and HEAD methods
methods: ['POST', 'PUT', 'GET', 'OPTIONS', 'HEAD', 'DELETE'],
// Allow credentials to be passed with requests
credentials: true,
})
);
// Set up session management middleware
app.use(
session({
secret: 'my secret',
resave: false,
saveUninitialized: false,
cookie: { sameSite: 'none', secure: true, maxAge: 1000 * 60 * 60 },
store: store,
})
);
// app.use(
// session({
// // Secret used to sign the session ID cookie
// secret: 'my secret',
// // Disable automatic session storage when changes aren't made
// resave: false,
// // Prevent creating a new session if no changes are made
// saveUninitialized: false,
// // Set options object for the session middleware cookie
// cookie: {
// // Set sameSite attribute to lax to allow cross-site requests
// sameSite: 'lax',
// // Disable HTTPS requirement
// secure: false,
// // Set maximum age to 1 hour
// maxAge: 1000 * 60 * 60,
// },
// // Store session data in external store
// store: store,
// })
// );
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', (req, res, next) => {
return res.send('Connected');
});
const accountController = require('./controller/accountController');
const causeController = require('./controller/causeController');
const donationController = require('./controller/donationController');
const causeRouter = express.Router();
const accountRouter = express.Router();
// Cause Routes
causeRouter
.route('/')
.get(causeController.getCause)
.post(accountController.checkAdmin, causeController.addCause)
.put(accountController.checkAdmin, causeController.updateCause);
causeRouter
.route('/delete')
.post(accountController.checkAdmin, causeController.deleteCause);
// Account Routes
accountRouter
.route('/')
.get(accountController.getAccount)
.post(accountController.addAccount);
accountRouter
.route('/delete')
.post(accountController.checkAdmin, accountController.deleteAccount);
accountRouter.route('/:id').put(accountController.updateAccount);
accountRouter
.route('/confirm/:accountID')
.post(accountController.verifyAccount);
accountRouter
.route('/request-reset-password')
.post(accountController.requestResetPassword);
accountRouter
.route('/reset-password')
.post(accountController.verifyResetPassword);
accountRouter
.route('/change-role')
.post(accountController.checkAdmin, accountController.changeRole);
accountRouter.route('/check-session').get(accountController.checkSession);
accountRouter.route('/login').post(accountController.login);
accountRouter.route('/logout').get(accountController.logout);
// Mount the routers onto the app
app.use('/cause', causeRouter);
app.use('/account', accountRouter);
//GET DONATION DETAIL
app.get('/donations', donationController.getDonation);
app.post('/payment/create-payment', donationController.createPayment);
app.post('/payment/execute-payment', donationController.executePayment);
mongoose
.connect(mongodbURL)
.then((result) => {
app.listen(port);
})
.catch((err) => {
console.log(err);
});