-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
146 lines (112 loc) · 3.85 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
global.Promise = require('bluebird')
const path = require('path')
const express = require('express')
const session = require('express-session')
const favicon = require('serve-favicon')
const logger = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const flash = require('connect-flash')
const config = require('/usr/etc/grants/config.js')
const passport = require('./passport.js')
const k = require('./constants')
const User = require('./models/user.js')
// Setup application
const app = express()
// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
// Passport setup
app.use(session({ secret: 'SECRET', resave: false, saveUninitialized: true }))
app.use(passport.initialize())
app.use(passport.session()) // persistent login sessions
app.use(flash()) // use connect-flash for flash messages stored in session
// API
app.use('/api/is-logged-in', require('./routes/is-logged-in'))
app.use('/api/settings', apiProtected, require('./routes/settings'))
app.use('/api/user', apiProtected, require('./routes/user'))
app.use('/api/applicant', apiProtected, require('./routes/applicant'))
app.use('/api/grant', apiProtected, require('./routes/grant'))
app.use('/api/funding', apiProtected, require('./routes/funding'))
app.use('/api/category', apiProtected, require('./routes/category'))
app.use('/api/history', apiProtected, require('./routes/history'))
app.use('/api', (req, res) => {
res.status(404)
res.json({ ok: false, message: '404', url: req.originalUrl })
res.end()
})
function apiProtected(req, res, next) {
if (req.isAuthenticated())
return next()
const username = req.get('x-username')
const password = req.get('x-password')
if (username === 'system' && password) {
return User.findByName(username)
.then(user => user.password === password ? Promise.resolve(user) : Promise.reject())
.then(user => {
req.user = user
return next()
})
.catch(() => {
res.json({ ok: false, message: 'Not authenticated' })
res.end()
})
}
if (process.env.NODE_ENV === 'development') {
req.user = {
id: 2,
googleID: '113897916442927912291',
token: 'asldfhaosidhfaouifhnewofuiqnwerocfnq9wejfoqwuie',
name: 'Rom Grk',
email: 'rom7011@gmail.com',
password: null
}
return next()
}
res.json({ ok: false, message: 'Not authenticated' })
res.end()
}
// Authentication
app.get('/auth/google', passport.authenticate('google', {
scope: ['profile', 'email'],
callbackURL: config.google.callbackURL,
}))
app.get('/auth/google/callback', passport.authenticate('google', {
successRedirect: '/auth/done',
failureRedirect: '/auth/done',
}))
app.get('/auth/logout', (req, res) => {
req.logout()
res.redirect('/auth/done')
})
app.get('/auth/done', (req, res) => {
res.render('oauth.ejs', {
user: req.user // get the user out of session and pass to template
})
})
/*
* Redirect handler
* We need to redirect all other routes to the app, e.g. /samples, /settings, etc.
*/
app.use((req, res, next) => {
res.redirect('/')
})
// Error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
if (err.type === k.ACCOUNT_NOT_FOUND)
req.logout()
// render the error page
res.status(err.status || 500)
res.render('error')
})
module.exports = app