forked from compsoc-edinburgh/email-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
151 lines (124 loc) · 3.51 KB
/
server.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
149
150
// import environment variables from file
require('dotenv').config()
// base
const express = require('express')
const debug = require('debug')('app:*')
const open_db = require('./db')
// authentication
const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20').Strategy
const session = require('express-session')
/* --- FRONTEND --- */
const app = express()
app.set('view engine', 'ejs')
app.use('/static', express.static('static'))
app.use(session({
secret: process.env['SESSION_SECRET'] ?? 'anything',
resave: false,
saveUninitialized: false
}))
// google auth plumbing
app.use(passport.initialize())
app.use(passport.session());
passport.use(
new GoogleStrategy({
clientID: process.env['GOOGLE_CLIENT_ID'],
clientSecret: process.env['GOOGLE_CLIENT_SECRET'],
callbackURL: process.env['GOOGLE_CALLBACK_URL']
},
(access_token, refresh_token, profile, cb) => {
debug('successfully authenticated with google!')
// simplify profile response
let lensed = profile => ({
id: profile.id,
name: profile.displayName,
email: profile.emails[0].value,
email_verified: profile.emails[0].verified,
photo: profile.photos[0]?.value
})
cb(null, lensed(profile))
})
)
// serialize the profile directly into the session (d i r t y)
passport.serializeUser((user, done) => done(null, JSON.stringify(user)))
passport.deserializeUser((user, done) => done(null, JSON.parse(user)))
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/callback',
passport.authenticate('google', {failureRedirect: '/auth/failed'}),
(req, res) => {
// successful authentication
res.redirect('/dashboard')
}
)
app.get('/auth/failed', (req, res) => {
res.send('auth failed!')
})
app.get('/auth/logout', (req, res) => {
req.logout()
res.redirect('/')
})
const login_guard = (req, res, next) => {
// short circuit
if (!req.user) {
res.redirect('/')
} else {
/*
req.user = {
name: 'Patrick Kage',
email: '[email protected]',
photo: 'https://placekitten.com/256/256'
}*/
next()
}
}
// homepage
app.get('/', (req, res) => {
res.render('index')
})
const {mail_app, MailDB} = require('./email')
app.use(login_guard, mail_app)
const {assets_app, AssetsDB} = require('./assets')
app.use('/assets', login_guard, assets_app)
// dashboard
app.get('/dashboard',
login_guard,
async (req, res) => {
const db = await open_db()
const mail = new MailDB(db)
const assets = new AssetsDB(db)
emails = await mail.getAllEmails()
res.render('dashboard', {
user: req.user,
emails: emails,
templates: emails.filter(e => e.is_template),
assets: await assets.listAssets()
})
}
)
app.get('/docs',
login_guard,
async (req, res) => {
res.render('docs', {
user: req.user
})
}
)
app.get('/404',
login_guard,
async (req, res) => {
res.render('404', {
user: req.user
})
}
)
app.get('/*',
login_guard,
async (req, res) => {
res.render('404', {
user: req.user
})
}
)
// launch the app!
app.listen(process.env['APP_PORT'], () => debug(`committee sso template listening on port ${process.env['APP_PORT']}!`))