MyKindlebox/mykindlebox/server/routes/auth.ts

27 lines
738 B
TypeScript

import { Router } from 'express';
import { authEnabled, isAuthenticated, login, logout } from '../auth';
const router = Router();
// Lets the client decide whether to show a login screen at all:
// `configured: false` means auth is disabled, so the app loads straight through.
router.get('/status', (req, res) => {
res.json({ configured: authEnabled, authenticated: isAuthenticated(req) });
});
router.post('/login', (req, res) => {
const ok = login(res, req.body?.password);
if (!ok) {
res.status(401).json({ error: 'Incorrect password' });
return;
}
res.json({ authenticated: true });
});
router.post('/logout', (req, res) => {
logout(req, res);
res.json({ authenticated: false });
});
export default router;