{"size": 1564, "ext": "js", "max_stars_repo_path": "admin/routes/routes.js", "max_stars_repo_name": "Pyncil/Pyncil", "max_stars_repo_stars_event_min_datetime": "2017-07-30T06:26:52.000Z", "max_stars_repo_stars_event_max_datetime": "2017-07-30T06:26:52.000Z", "max_issues_repo_path": "admin/routes/routes.js", "max_issues_repo_name": "Pyncil/Pyncil", "max_issues_repo_issues_event_min_datetime": "2017-04-13T20:14:02.000Z", "max_issues_repo_issues_event_max_datetime": "2020-09-04T02:46:14.000Z", "max_forks_repo_path": "admin/routes/routes.js", "max_forks_repo_name": "Pyncil/Pyncil", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 7, "max_forks_count": null, "avg_line_length": 26.0666666667, "max_line_length": 105, "alphanum_fraction": 0.6304347826}
'use strict'

var express = require('express'),
    path = require('path'),
    passport = require('passport'),
    csrf = require('csurf'),
    admin = require('./admin'),
    router = express.Router(),
    bodyParser = require('body-parser'),
    engines = require('consolidate'),
    authControl = require('connect-ensure-login'),
    ensureLoggedIn = authControl.ensureLoggedIn,
    ensureLoggedOut = authControl.ensureLoggedOut

/**
 * Authentication Routes
 */

router.post('/login', 
          bodyParser.urlencoded({ extended: false }), csrf(),
          passport.authenticate('local', {
            successRedirect: '/admin',
            failureRedirect: '/login',
            failureFlash: true
          }))

router.get('/login', csrf(), ensureLoggedOut('/admin'), (req, res) => {
  engines.ejs(path.resolve(__dirname, '../dist/views/login.ejs'), {
    csrf: req.csrfToken(),
    message: req.flash('message')
  }, (err, str) => {
    if (err) return res.send(err)
    res.send(str)
  })
})

router.get('/logout', ensureLoggedIn('/'), (req, res) => {
  req.session.destroy((err) => {
    if (err) return res.send(err)
    res.redirect('/')
  })
})


/**
 * Admin Routes
 */

// unauthenticated static files for admin backend
router.use('/admin/static', express.static(path.resolve(__dirname, '../static')))

// authenticated static files for admin backend
router.use('/admin/static', ensureLoggedIn('/login'), express.static(path.resolve(__dirname, '../dist')))

router.use('/admin', ensureLoggedIn('/login'), admin)


// Export
module.exports = router
