2017-04-22 12:56:28 +00:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const session = require('express-session')
|
|
|
|
const FileStore = require('session-file-store')(session)
|
|
|
|
const next = require('next')
|
|
|
|
const admin = require('firebase-admin')
|
|
|
|
|
2017-08-10 18:15:46 +00:00
|
|
|
const port = parseInt(process.env.PORT, 10) || 3000
|
2017-04-22 12:56:28 +00:00
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
|
|
|
const app = next({ dev })
|
|
|
|
const handle = app.getRequestHandler()
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
const firebase = admin.initializeApp(
|
|
|
|
{
|
|
|
|
credential: admin.credential.cert(require('./credentials/server')),
|
|
|
|
databaseURL: '' // TODO database URL goes here
|
|
|
|
},
|
|
|
|
'server'
|
|
|
|
)
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
app.prepare().then(() => {
|
|
|
|
const server = express()
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.use(bodyParser.json())
|
|
|
|
server.use(
|
|
|
|
session({
|
2018-03-27 18:11:03 +00:00
|
|
|
secret: 'geheimnis',
|
|
|
|
saveUninitialized: true,
|
2018-12-17 16:34:32 +00:00
|
|
|
store: new FileStore({ path: '/tmp/sessions', secret: 'geheimnis' }),
|
2018-03-27 18:11:03 +00:00
|
|
|
resave: false,
|
|
|
|
rolling: true,
|
|
|
|
httpOnly: true,
|
|
|
|
cookie: { maxAge: 604800000 } // week
|
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
)
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.use((req, res, next) => {
|
|
|
|
req.firebaseServer = firebase
|
|
|
|
next()
|
|
|
|
})
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.post('/api/login', (req, res) => {
|
|
|
|
if (!req.body) return res.sendStatus(400)
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
const token = req.body.token
|
|
|
|
firebase
|
|
|
|
.auth()
|
|
|
|
.verifyIdToken(token)
|
|
|
|
.then(decodedToken => {
|
|
|
|
req.session.decodedToken = decodedToken
|
|
|
|
return decodedToken
|
|
|
|
})
|
|
|
|
.then(decodedToken => res.json({ status: true, decodedToken }))
|
|
|
|
.catch(error => res.json({ error }))
|
|
|
|
})
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.post('/api/logout', (req, res) => {
|
|
|
|
req.session.decodedToken = null
|
|
|
|
res.json({ status: true })
|
|
|
|
})
|
2017-04-22 12:56:28 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
server.get('*', (req, res) => {
|
|
|
|
return handle(req, res)
|
|
|
|
})
|
|
|
|
|
|
|
|
server.listen(port, err => {
|
|
|
|
if (err) throw err
|
|
|
|
console.log(`> Ready on http://localhost:${port}`)
|
2017-04-22 12:56:28 +00:00
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
})
|