63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
const {checkAdmin, checkUsers, registerUsers, getAllUsersToJson, VIEWS_USERS_PATH} = require('../../services/users');
|
|
const path = require('path');
|
|
const auth = require('../../utils/auth_utils');
|
|
// <reference types="express"/>
|
|
// const { Request, Response } = require('express');
|
|
|
|
function login(req, res, next) {
|
|
const data = req.body;
|
|
const isAdmin = req.query.admin !== undefined; // 检查查询参数
|
|
if (isAdmin) {
|
|
res.json(checkAdmin(data.username, data.password));
|
|
} else {
|
|
res.json(checkUsers(data.username, data.password));
|
|
}
|
|
};
|
|
|
|
function getJson(req, res, next) {
|
|
const filename = req.params.filename;
|
|
if (filename === 'users_cards.json') {
|
|
return res.json(getAllUsersToJson(token));
|
|
}
|
|
}
|
|
|
|
function register(req, res, next) {
|
|
const data = req.body;
|
|
res.json(registerUsers(data.username, data.password));
|
|
};
|
|
|
|
//------------------------------------//
|
|
|
|
function getLogin(req, res, next) {
|
|
res.sendFile(path.join(VIEWS_USERS_PATH, 'login.html'));
|
|
}
|
|
|
|
function getRoot(req, res, next) {
|
|
token = req.cookies.token;
|
|
if (typeof token === 'string') {
|
|
return auth.verifyToken(token, (err, data) => {
|
|
if (err) {
|
|
res.redirect('/users/login');
|
|
return;
|
|
}
|
|
res.sendFile(path.join(VIEWS_USERS_PATH, 'users.html'));
|
|
})
|
|
}
|
|
res.redirect('/users/login');
|
|
}
|
|
|
|
function getAdmin(req, res, next) {
|
|
token = req.cookies.token;
|
|
if (typeof token === 'string') {
|
|
return auth.verifyToken(token, (err, data) => {
|
|
if (err) {
|
|
res.redirect('/users/login?admin');
|
|
return;
|
|
}
|
|
res.sendFile(path.join(VIEWS_USERS_PATH, 'admin.html'));
|
|
})
|
|
}
|
|
res.redirect('/users/login?admin');
|
|
}
|
|
|
|
module.exports = { getRoot, getAdmin, getLogin, login, register, getJson }; |