mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-29 18:16:17 +01:00
refactor: async/await controllers/index.js
This commit is contained in:
@@ -40,7 +40,7 @@ Controllers.composer = require('./composer');
|
|||||||
|
|
||||||
Controllers.write = require('./write');
|
Controllers.write = require('./write');
|
||||||
|
|
||||||
Controllers.reset = function (req, res, next) {
|
Controllers.reset = async function (req, res) {
|
||||||
if (meta.config['password:disableEdit']) {
|
if (meta.config['password:disableEdit']) {
|
||||||
return helpers.notAllowed(req, res);
|
return helpers.notAllowed(req, res);
|
||||||
}
|
}
|
||||||
@@ -78,14 +78,9 @@ Controllers.reset = function (req, res, next) {
|
|||||||
|
|
||||||
if (req.session.reset_code) {
|
if (req.session.reset_code) {
|
||||||
// Validate and save to local variable before removing from session
|
// Validate and save to local variable before removing from session
|
||||||
user.reset.validate(req.session.reset_code, function (err, valid) {
|
const valid = await user.reset.validate(req.session.reset_code);
|
||||||
if (err) {
|
renderReset(req.session.reset_code, valid);
|
||||||
return next(err);
|
delete req.session.reset_code;
|
||||||
}
|
|
||||||
|
|
||||||
renderReset(req.session.reset_code, valid);
|
|
||||||
delete req.session.reset_code;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
res.render('reset', {
|
res.render('reset', {
|
||||||
code: null,
|
code: null,
|
||||||
@@ -97,14 +92,14 @@ Controllers.reset = function (req, res, next) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Controllers.login = function (req, res, next) {
|
Controllers.login = async function (req, res) {
|
||||||
var data = { loginFormEntry: [] };
|
const data = { loginFormEntry: [] };
|
||||||
var loginStrategies = require('../routes/authentication').getLoginStrategies();
|
const loginStrategies = require('../routes/authentication').getLoginStrategies();
|
||||||
var registrationType = meta.config.registrationType || 'normal';
|
const registrationType = meta.config.registrationType || 'normal';
|
||||||
var allowLoginWith = (meta.config.allowLoginWith || 'username-email');
|
const allowLoginWith = (meta.config.allowLoginWith || 'username-email');
|
||||||
var returnTo = (req.headers['x-return-to'] || '').replace(nconf.get('base_url') + nconf.get('relative_path'), '');
|
const returnTo = (req.headers['x-return-to'] || '').replace(nconf.get('base_url') + nconf.get('relative_path'), '');
|
||||||
|
|
||||||
var errorText;
|
let errorText;
|
||||||
if (req.query.error === 'csrf-invalid') {
|
if (req.query.error === 'csrf-invalid') {
|
||||||
errorText = '[[error:csrf-invalid]]';
|
errorText = '[[error:csrf-invalid]]';
|
||||||
} else if (req.query.error) {
|
} else if (req.query.error) {
|
||||||
@@ -126,33 +121,24 @@ Controllers.login = function (req, res, next) {
|
|||||||
data.title = '[[pages:login]]';
|
data.title = '[[pages:login]]';
|
||||||
data.allowPasswordReset = !meta.config['password:disableEdit'];
|
data.allowPasswordReset = !meta.config['password:disableEdit'];
|
||||||
|
|
||||||
privileges.global.canGroup('local:login', 'registered-users', function (err, hasLoginPrivilege) {
|
const hasLoginPrivilege = await privileges.global.canGroup('local:login', 'registered-users');
|
||||||
if (err) {
|
data.allowLocalLogin = hasLoginPrivilege || parseInt(req.query.local, 10) === 1;
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
data.allowLocalLogin = hasLoginPrivilege || parseInt(req.query.local, 10) === 1;
|
if (!data.allowLocalLogin && !data.allowRegistration && data.alternate_logins && data.authentication.length === 1) {
|
||||||
if (!data.allowLocalLogin && !data.allowRegistration && data.alternate_logins && data.authentication.length === 1) {
|
if (res.locals.isAPI) {
|
||||||
if (res.locals.isAPI) {
|
return helpers.redirect(res, {
|
||||||
return helpers.redirect(res, {
|
external: nconf.get('relative_path') + data.authentication[0].url,
|
||||||
external: nconf.get('relative_path') + data.authentication[0].url,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return res.redirect(nconf.get('relative_path') + data.authentication[0].url);
|
|
||||||
}
|
|
||||||
if (req.loggedIn) {
|
|
||||||
user.getUserFields(req.uid, ['username', 'email'], function (err, user) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
data.username = allowLoginWith === 'email' ? user.email : user.username;
|
|
||||||
data.alternate_logins = false;
|
|
||||||
res.render('login', data);
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
res.render('login', data);
|
|
||||||
}
|
}
|
||||||
});
|
return res.redirect(nconf.get('relative_path') + data.authentication[0].url);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.loggedIn) {
|
||||||
|
const userData = await user.getUserFields(req.uid, ['username', 'email']);
|
||||||
|
data.username = allowLoginWith === 'email' ? userData.email : userData.username;
|
||||||
|
data.alternate_logins = false;
|
||||||
|
}
|
||||||
|
res.render('login', data);
|
||||||
};
|
};
|
||||||
|
|
||||||
Controllers.register = async function (req, res, next) {
|
Controllers.register = async function (req, res, next) {
|
||||||
@@ -246,8 +232,8 @@ Controllers.robots = function (req, res) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Controllers.manifest = function (req, res, next) {
|
Controllers.manifest = async function (req, res) {
|
||||||
var manifest = {
|
const manifest = {
|
||||||
name: meta.config.title || 'NodeBB',
|
name: meta.config.title || 'NodeBB',
|
||||||
short_name: meta.config['title:short'] || meta.config.title || 'NodeBB',
|
short_name: meta.config['title:short'] || meta.config.title || 'NodeBB',
|
||||||
start_url: nconf.get('url'),
|
start_url: nconf.get('url'),
|
||||||
@@ -312,18 +298,21 @@ Controllers.manifest = function (req, res, next) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins.fireHook('filter:manifest.build', { req: req, res: res, manifest: manifest }, function (err, data) {
|
const data = await plugins.fireHook('filter:manifest.build', {
|
||||||
if (err) {
|
req: req,
|
||||||
return next(err);
|
res: res,
|
||||||
}
|
manifest: manifest,
|
||||||
res.status(200).json(data.manifest);
|
|
||||||
});
|
});
|
||||||
|
res.status(200).json(data.manifest);
|
||||||
};
|
};
|
||||||
|
|
||||||
Controllers.outgoing = function (req, res, next) {
|
Controllers.outgoing = function (req, res, next) {
|
||||||
var url = req.query.url || '';
|
const url = req.query.url || '';
|
||||||
var allowedProtocols = ['http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal'];
|
const allowedProtocols = [
|
||||||
var parsed = require('url').parse(url);
|
'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher',
|
||||||
|
'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal',
|
||||||
|
];
|
||||||
|
const parsed = require('url').parse(url);
|
||||||
|
|
||||||
if (!url || !parsed.protocol || !allowedProtocols.includes(parsed.protocol.slice(0, -1))) {
|
if (!url || !parsed.protocol || !allowedProtocols.includes(parsed.protocol.slice(0, -1))) {
|
||||||
return next();
|
return next();
|
||||||
|
|||||||
Reference in New Issue
Block a user