-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.ts
76 lines (66 loc) · 2.29 KB
/
server.ts
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
require('dotenv').config();
import cors from 'cors';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
const server = require('express')();
import routes from './routes';
const port = process.env.PORT;
import AdminJS from 'adminjs';
import AdminJSMongoose from '@adminjs/mongoose';
import AdminJSExpress from '@adminjs/express';
import { userSchema, sessionSchema, savedRequestSchema, profileSchema, requestSchema, configSchema } from './schema';
import session from 'express-session';
mongoose.connect(process.env.MONGO_DB_STRING!);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error: '));
db.once('open', async () => {
await db.collection('requests').createIndex({ location: '2dsphere' });
console.log('DB connected successfully');
const adminJsOptions = {
resources: [
userSchema,
sessionSchema,
profileSchema,
requestSchema,
savedRequestSchema,
configSchema
],
rootPath: '/admin',
branding: {
companyName: 'BloodLine Admin',
},
};
AdminJS.registerAdapter({
Resource: AdminJSMongoose.Resource,
Database: AdminJSMongoose.Database,
});
const admin = new AdminJS(adminJsOptions);
admin.watch()
server.use(cors({
origin: true,
credentials: true,
methods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
preflightContinue: true,
}));
server.use(session({
secret: process.env.JWT_ACCESS_TOKEN_SECRET as string, //this
}));
server.use(admin.options.rootPath, AdminJSExpress.buildAuthenticatedRouter(admin, {
authenticate: async (email: string, password: string) => {
if (email === process.env.ADMIN_EMAIL && password === process.env.ADMIN_PASSWORD) {
return {
email: process.env.ADMIN_EMAIL,
};
}
return null;
},
cookiePassword: '' //dont know
}));
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.use(routes);
server.listen(port, () => {
console.log(`Server ready at http://localhost:${port}`);
});
});