},
// Find one user by id, name, email, or token
- getOne: function(by, value, cb) {
+ getOne: function(by, value, fields, cb) {
const delimiter = (typeof value === "string" ? "'" : "");
db.serialize(function() {
const query =
- "SELECT * " +
+ "SELECT " + fields + " " +
"FROM Users " +
"WHERE " + by + " = " + delimiter + value + delimiter;
db.get(query, cb);
},
tryNotify: function(id, message) {
- UserModel.getOne("id", id, (err,user) => {
+ UserModel.getOne("id", id, "name, email", (err, user) => {
if (!err && user.notify) UserModel.notify(user, message);
});
},
});
};
if (req.body.chall.to) {
- UserModel.getOne("name", challenge.to, (err,user) => {
- if (err || !user)
- res.json(err || {errmsg: "Typo in player name"});
- else {
- challenge.to = user.id; //ready now to insert challenge
- insertChallenge();
- if (user.notify)
- UserModel.notify(
- user,
- "New challenge : " + params.siteURL + "/#/?disp=corr");
+ UserModel.getOne(
+ "name", challenge.to, "id, name, email, notify",
+ (err, user) => {
+ if (err || !user) res.json(err || {errmsg: "Typo in player name"});
+ else {
+ challenge.to = user.id; //ready now to insert challenge
+ insertChallenge();
+ if (user.notify) {
+ UserModel.notify(
+ user,
+ "New challenge : " + params.siteURL + "/#/?disp=corr");
+ }
+ }
}
- });
+ );
} else insertChallenge();
}
});
};
if (!req.cookies.token) callback(anonymous);
else if (req.cookies.token.match(/^[a-z0-9]+$/)) {
- UserModel.getOne("sessionToken", req.cookies.token, (err, user) => {
- callback(user || anonymous);
- });
+ UserModel.getOne(
+ "sessionToken", req.cookies.token, "name, email, id, notify",
+ (err, user) => callback(user || anonymous)
+ );
}
});
const nameOrEmail = decodeURIComponent(req.query.nameOrEmail);
const type = (nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
if (UserModel.checkNameEmail({ [type]: nameOrEmail })) {
- UserModel.getOne(type, nameOrEmail, (err,user) => {
+ UserModel.getOne(type, nameOrEmail, "id, name, email", (err, user) => {
access.checkRequest(res, err, user, "Unknown user", () => {
setAndSendLoginToken("Token for " + params.siteURL, user);
res.json({});
router.get('/authenticate', access.unlogged, access.ajax, (req,res) => {
if (!req.query.token.match(/^[a-z0-9]+$/))
return res.json({ errmsg: "Bad token" });
- UserModel.getOne("loginToken", req.query.token, (err,user) => {
- access.checkRequest(res, err, user, "Invalid token", () => {
- // If token older than params.tokenExpire, do nothing
- if (Date.now() > user.loginTime + params.token.expire)
- res.json({ errmsg: "Token expired" });
- else {
- // Generate session token (if not exists) + destroy login token
- UserModel.trySetSessionToken(user.id, (token) => {
- res.cookie("token", token, {
- httpOnly: true,
- secure: !!params.siteURL.match(/^https/),
- maxAge: params.cookieExpire,
- });
- res.json({
- id: user.id,
- name: user.name,
- email: user.email,
- notify: user.notify
+ UserModel.getOne(
+ "loginToken", req.query.token, "id, name, email, notify",
+ (err,user) => {
+ access.checkRequest(res, err, user, "Invalid token", () => {
+ // If token older than params.tokenExpire, do nothing
+ if (Date.now() > user.loginTime + params.token.expire)
+ res.json({ errmsg: "Token expired" });
+ else {
+ // Generate session token (if not exists) + destroy login token
+ UserModel.trySetSessionToken(user.id, (token) => {
+ res.cookie("token", token, {
+ httpOnly: true,
+ secure: !!params.siteURL.match(/^https/),
+ maxAge: params.cookieExpire,
+ });
+ res.json(user);
});
- });
- }
- });
- });
+ }
+ });
+ }
+ );
});
router.get('/logout', access.logged, access.ajax, (req,res) => {
var UserModel = require("../models/User");
-module.exports =
-{
+module.exports = {
// Prevent access to "users pages"
logged: function(req, res, next) {
const callback = () => {
loggedIn = false;
callback();
} else {
- UserModel.getOne("sessionToken", req.cookies.token, (err, user) => {
- if (!!user) {
- req.userId = user.id;
- loggedIn = true;
- } else {
- // Token in cookies presumably wrong: erase it
- res.clearCookie("token");
- loggedIn = false;
+ UserModel.getOne(
+ "sessionToken", req.cookies.token, "id",
+ (err, user) => {
+ if (!!user) {
+ req.userId = user.id;
+ loggedIn = true;
+ } else {
+ // Token in cookies presumably wrong: erase it
+ res.clearCookie("token");
+ loggedIn = false;
+ }
+ callback();
}
- callback();
- });
+ );
}
},
res.json({ errmsg: msg });
} else cb();
}
-}
+};