const express = require('express');
const router = express.Router();
const passport = require('passport');

const { checkAuthenticated: isLogged, checkNotAuthenticated: isNotLogged } = require('../controller/auth/loggedAuth');

//主頁
router.get('/', isLogged, (req, res) => {
    res.render('index', { user: req.user });
});
//新增會議記錄
router.get('/create', isLogged, (req, res) => {
    res.render('create');
});
//會議記錄管理
router.get('/minutes', isLogged, (req, res) => {
    res.render('minutes');
});
//使用者註冊
router.get('/register', isLogged, (req, res) => {
    res.render('register');
});
//使用者管理
router.get('/admin', isLogged, (req, res) => {
    res.render('admin');
});
//學校網頁
router.get('/public', (req, res) => {
    res.render('public');
});

//登入
router
    .route('/login')
    .all(isNotLogged)
    .get((req, res) => {
        res.render('login');
    })
    .post(
        passport.authenticate('local', {
            successRedirect: './',
            failureRedirect: './login',
            failureFlash: true,
        })
    );

//登出
router.get('/logout', isLogged, (req, res) => {
    req.logOut();
    res.redirect('login');
});

module.exports = router;
