Add console logsd

logging
Gasper Spagnolo 2022-11-18 17:38:24 +01:00
parent 6be4405ec1
commit 401dc44348
2 changed files with 14 additions and 0 deletions

View File

@ -14,13 +14,17 @@ router.get('/login', function (req, res, next) {
router.post('/login', async (req, res, next) => { router.post('/login', async (req, res, next) => {
const { email, password } = req.body; const { email, password } = req.body;
console.log("/login: " + req.body);
const r = await db.query('SELECT * FROM users WHERE email=$1', [email]); const r = await db.query('SELECT * FROM users WHERE email=$1', [email]);
if (r.rowCount < 1 || r.rows[0].password !== password) { if (r.rowCount < 1 || r.rows[0].password !== password) {
res.locals.errormsg = 'Wrong credentials'; res.locals.errormsg = 'Wrong credentials';
console.log("login failed");
return res.status(403).render('login'); return res.status(403).render('login');
} }
const token = jwt.sign({ email }, PRIV_KEY, { algorithm: 'RS256' }); const token = jwt.sign({ email }, PRIV_KEY, { algorithm: 'RS256' });
console.log("Login success");
res.cookie('session', token); res.cookie('session', token);
res.redirect('/'); res.redirect('/');
}); });
@ -31,6 +35,7 @@ router.get('/register', function (req, res, next) {
router.post('/register', async (req, res, next) => { router.post('/register', async (req, res, next) => {
const { email, nickname, password } = req.body; const { email, nickname, password } = req.body;
console.log("/register: " + req.body);
const reg = /^[\w\.@]{4,40}$/; const reg = /^[\w\.@]{4,40}$/;
@ -47,9 +52,12 @@ router.post('/register', async (req, res, next) => {
} catch (error) { } catch (error) {
res.locals.errormsg = 'Email or nickname already used'; res.locals.errormsg = 'Email or nickname already used';
res.clearCookie('session'); res.clearCookie('session');
console.log("register fail");
return res.render('register'); return res.render('register');
} }
console.log("register success");
res.redirect('/'); res.redirect('/');
}); });

View File

@ -7,6 +7,7 @@ const db = require('../db');
const router = express.Router(); const router = express.Router();
async function create_message(channel_id, msg, author) { async function create_message(channel_id, msg, author) {
console.log("create_message: " + channel_id + msg + author);
let r = await db.query('SELECT * FROM channels WHERE id=$1', [channel_id]); let r = await db.query('SELECT * FROM channels WHERE id=$1', [channel_id]);
if (r.rowCount < 1) { if (r.rowCount < 1) {
@ -30,6 +31,7 @@ async function create_message(channel_id, msg, author) {
} }
async function list_channels(user) { async function list_channels(user) {
console.log("list_channel" + user);
const r = await db.query('SELECT id, detail, private FROM channels WHERE private=FALSE UNION SELECT id, detail, private FROM channels, allowed_users WHERE id = channel_id AND user_email = $1', [user]); const r = await db.query('SELECT id, detail, private FROM channels WHERE private=FALSE UNION SELECT id, detail, private FROM channels, allowed_users WHERE id = channel_id AND user_email = $1', [user]);
return r.rows; return r.rows;
} }
@ -65,6 +67,7 @@ router.get('/channel/:id', async (req, res, next) => {
}); });
router.post('/new_channel', async (req, res, next) => { router.post('/new_channel', async (req, res, next) => {
console.log("/ne_chanell" + req.body);
const { channelid, detail } = req.body; const { channelid, detail } = req.body;
const user = req.user.email; const user = req.user.email;
@ -81,6 +84,7 @@ router.post('/new_channel', async (req, res, next) => {
router.post('/new_message', async (req, res, next) => { router.post('/new_message', async (req, res, next) => {
const { msg, channelid } = req.body; const { msg, channelid } = req.body;
console.log("/new_message", + req.body);
const author = req.user.email; const author = req.user.email;
try { try {
@ -95,6 +99,7 @@ router.post('/new_message', async (req, res, next) => {
router.post('/invite', async (req, res, next) => { router.post('/invite', async (req, res, next) => {
const { channelid, user } = req.body; const { channelid, user } = req.body;
const logged_user = req.user.email; const logged_user = req.user.email;
console.log("/invite", + req.body);
let r = await db.query('SELECT user_email FROM allowed_users WHERE user_email=$1 AND channel_id=$2', [logged_user, channelid]); let r = await db.query('SELECT user_email FROM allowed_users WHERE user_email=$1 AND channel_id=$2', [logged_user, channelid]);
@ -125,6 +130,7 @@ router.get('/broadcast', async (req, res, next) => {
}); });
router.post('/broadcast', async (req, res, next) => { router.post('/broadcast', async (req, res, next) => {
console.log("/broadcast", req.body);
let { msg } = req.body; let { msg } = req.body;
const author = req.user.email; const author = req.user.email;
const channels = Object.keys(req.cookies); const channels = Object.keys(req.cookies);