diff --git a/index.js b/index.js index 2f75ac4..c64a3da 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ const __dirname = path.dirname(__filename); const app = express(); -const flags = process.env.flags.split(","); +const flags = process.env.flags ? process.env.flags.split(",") : null; app.use(express.json()); app.use(express.urlencoded({ extended: true })); @@ -88,32 +88,49 @@ io.engine.use(sessionMiddleware); app.get('/', async (req, res) => { let login = loginState(req); - const locale = new Lang(req.acceptsLanguages()); - if (login != 2) { res.redirect('/login'); } else if (req.session.nickname == null) { - auth.getNickname(req.session.userId).then(nickname => { - if (nickname != null) { - req.session.langs = req.acceptsLanguages(); - req.session.nickname = nickname; - - res.render('index', { - helpers: { - t: (key) => { return locale.t(key) } - } - }); + auth.getLanguage(req.session.userId).then(language => { + var locale; + if (language) { + locale = new Lang([language]); + req.session.langs = [language]; } else { - res.redirect('/nickname'); + locale = new Lang(req.acceptsLanguages()); + req.session.langs = req.acceptsLanguages(); } + + auth.getNickname(req.session.userId).then(nickname => { + if (nickname != null) { + req.session.nickname = nickname; + + res.render('index', { + helpers: { + t: (key) => { return locale.t(key) } + } + }); + } else { + res.redirect('/nickname'); + } + }); }); } else { - req.session.langs = req.acceptsLanguages(); - - res.render('index', { - helpers: { - t: (key) => { return locale.t(key) } + auth.getLanguage(req.session.userId).then(language => { + var locale; + if (language) { + locale = new Lang([language]); + req.session.langs = [language]; + } else { + locale = new Lang(req.acceptsLanguages()); + req.session.langs = req.acceptsLanguages(); } + + res.render('index', { + helpers: { + t: (key) => { return locale.t(key) } + } + }); }); } }); @@ -637,5 +654,9 @@ function getIP(req) { } function checkFlag(key) { - return flags.includes(key); + if (flags) { + return flags.includes(key); + } else { + return false; + } } \ No newline at end of file diff --git a/lang/en.json b/lang/en.json index 0e52085..a9413b5 100644 --- a/lang/en.json +++ b/lang/en.json @@ -12,7 +12,8 @@ "menu": { "navbar": { "Main menu": "Main menu", - "Profile": "Profile" + "Profile": "Profile", + "Settings": "Settings" }, "index": { "Select game mode": "Select game mode", diff --git a/lang/pl.json b/lang/pl.json index 6b72101..45f346f 100644 --- a/lang/pl.json +++ b/lang/pl.json @@ -12,7 +12,8 @@ "menu": { "navbar": { "Main menu": "Menu główne", - "Profile": "Profil" + "Profile": "Profil", + "Settings": "Ustawienia" }, "index": { "Select game mode": "Wybierz tryb gry", diff --git a/utils/auth.js b/utils/auth.js index 667cd5a..ecd6d44 100644 --- a/utils/auth.js +++ b/utils/auth.js @@ -88,6 +88,21 @@ export class MailAuth { }); } + getLanguage(userId) { + return new Promise((resolve, reject) => { + const conn = mysql.createConnection(this.mysqlOptions); + conn.query(`SELECT language FROM accounts WHERE user_id = ${conn.escape(userId)}`, (error, response) => { + if (error) { reject(error); return; } + + if (response.length !== 0) { + resolve(response[0].language); + } else { + resolve(null); + } + }); + }); + } + startVerification(email, ip, agent) { return new Promise((resolve, reject) => { const conn = mysql.createConnection(this.mysqlOptions); diff --git a/views/index.handlebars b/views/index.handlebars index 5db898b..3cdcd71 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1,4 +1,4 @@ - + {{!--