-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (81 loc) · 2.46 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
require('dotenv');
const passport = require('passport'), GoogleStrategy = require('passport-google-oauth20')
const app = express();
const port = process.env.PORT;
const errorHandler = require('./middleware/errorHandler');
const public = require('path').join(__dirname, 'resources');
const cron = require('node-cron');
const notificationCron = require('./helpers/cron');
const db = require('./sequelize/models');
const {socialLogin} = require("./services/auth");
const {OAuth2Client} = require('google-auth-library')
const client = new OAuth2Client(process.env.CLIENT_ID)
const corsOptions = {
origin: "*",
credentials: true,
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use('/avatarImages', express.static('avatarImages'))
app.use(bodyParser.urlencoded({
extended: true,
parameterLimit: 100000,
limit: '50mb',}))
app.use(errorHandler);
app.use(passport.initialize())
passport.serializeUser((user, cb) => {
return cb(null, user.id)
})
passport.deserializeUser((user, cb) => {
return cb(null, user.id)
})
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/login/google-auth"
},
(accessToken, refreshToken, profile, cb) => {
return cb(null, profile)
}
))
app.post('/api/google', async (req, res, next) => {
const {tokenId} = req.body
const ticket = await client.verifyIdToken({
idToken: tokenId,
audience: process.env.GOOGLE_CLIENT_ID
});
const {name, email, picture} = ticket.getPayload();
socialLogin(email)
.then((data) => res.status(200).json({data, message: "Logged in successfully"}))
.catch(next)
;
})
const Role = db.role;
function initial() {
Role.create({
id: 1,
name: "user"
});
Role.create({
id: 2,
name: "admin"
});
Role.create({
id: 3,
name: "company"
})
}
app.use(express.static(public));
app.use('/api/auth', require('./controllers/auth'));
app.use('/api/company', require('./controllers/company'));
app.use('/api/event', require('./controllers/event'));
app.use('/api/user', require('./controllers/user'));
cron.schedule('* * * * *', async function () {
await notificationCron();
});
app.listen(port, () => {
console.log("App is listening on port " + port);
})