31 lines
858 B
JavaScript
31 lines
858 B
JavaScript
import jwt from 'jsonwebtoken'
|
|
import { Router } from 'express'
|
|
import AUTH, { SECRET } from './auth.js'
|
|
|
|
const ROUTES = Router()
|
|
|
|
ROUTES.get('/', (_, res) => res.render("pages/landing.njk"))
|
|
ROUTES.get('/login', (_, res) => res.render("pages/login.njk"))
|
|
ROUTES.get('/register', (_, res) => res.render("pages/register.njk"))
|
|
ROUTES.get(
|
|
'/manager',
|
|
(req, res) => {
|
|
const apiToken = req.signedCookies['api-token'] ?? null
|
|
if (!apiToken)
|
|
return res.redirect('/login')
|
|
|
|
try {
|
|
if (!jwt.verify(apiToken, SECRET))
|
|
return res.redirect('/login')
|
|
|
|
const bearer = jwt.decode(apiToken)
|
|
|
|
res.render('pages/manager.njk', { user: bearer })
|
|
} catch (error) {
|
|
res.redirect('/login')
|
|
}
|
|
}
|
|
)
|
|
ROUTES.use('/auth', AUTH)
|
|
|
|
export default ROUTES |