Unfinished settings + improvements

This commit is contained in:
MaciejkaG 2024-03-28 16:47:48 +01:00
parent 9d0c60419e
commit e7b0470653
5 changed files with 67 additions and 23 deletions

View File

@ -22,7 +22,7 @@ const __dirname = path.dirname(__filename);
const app = express(); 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.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
@ -88,14 +88,21 @@ io.engine.use(sessionMiddleware);
app.get('/', async (req, res) => { app.get('/', async (req, res) => {
let login = loginState(req); let login = loginState(req);
const locale = new Lang(req.acceptsLanguages());
if (login != 2) { if (login != 2) {
res.redirect('/login'); res.redirect('/login');
} else if (req.session.nickname == null) { } else if (req.session.nickname == null) {
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();
}
auth.getNickname(req.session.userId).then(nickname => { auth.getNickname(req.session.userId).then(nickname => {
if (nickname != null) { if (nickname != null) {
req.session.langs = req.acceptsLanguages();
req.session.nickname = nickname; req.session.nickname = nickname;
res.render('index', { res.render('index', {
@ -107,14 +114,24 @@ app.get('/', async (req, res) => {
res.redirect('/nickname'); res.redirect('/nickname');
} }
}); });
});
} else { } else {
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(); req.session.langs = req.acceptsLanguages();
}
res.render('index', { res.render('index', {
helpers: { helpers: {
t: (key) => { return locale.t(key) } t: (key) => { return locale.t(key) }
} }
}); });
});
} }
}); });
@ -637,5 +654,9 @@ function getIP(req) {
} }
function checkFlag(key) { function checkFlag(key) {
if (flags) {
return flags.includes(key); return flags.includes(key);
} else {
return false;
}
} }

View File

@ -12,7 +12,8 @@
"menu": { "menu": {
"navbar": { "navbar": {
"Main menu": "Main menu", "Main menu": "Main menu",
"Profile": "Profile" "Profile": "Profile",
"Settings": "Settings"
}, },
"index": { "index": {
"Select game mode": "Select game mode", "Select game mode": "Select game mode",

View File

@ -12,7 +12,8 @@
"menu": { "menu": {
"navbar": { "navbar": {
"Main menu": "Menu główne", "Main menu": "Menu główne",
"Profile": "Profil" "Profile": "Profil",
"Settings": "Ustawienia"
}, },
"index": { "index": {
"Select game mode": "Wybierz tryb gry", "Select game mode": "Wybierz tryb gry",

View File

@ -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) { startVerification(email, ip, agent) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const conn = mysql.createConnection(this.mysqlOptions); const conn = mysql.createConnection(this.mysqlOptions);

View File

@ -1,4 +1,4 @@
<nav><span onclick="switchView('mainMenuView')">{{ t 'menu.navbar.Main menu' }}</span> <span id="profileButton" onclick="switchView('profileView')">{{ t 'menu.navbar.Profile' }}</span></nav> <nav><span onclick="switchView('settingsView')">{{ t 'menu.navbar.Settings' }}</span><span onclick="switchView('mainMenuView')">{{ t 'menu.navbar.Main menu' }}</span> <span id="profileButton" onclick="switchView('profileView')">{{ t 'menu.navbar.Profile' }}</span></nav>
{{!-- <h1 class="header">Statki</h1> --}} {{!-- <h1 class="header">Statki</h1> --}}
<div class="container" id="mainMenuView" data-path="/"> <div class="container" id="mainMenuView" data-path="/">
<div> <div>
@ -93,6 +93,12 @@
</div> </div>
</div> </div>
<div class="container" id="settingsView" data-path="/match">
<div>
</div>
</div>
<script> <script>
window.locale = { window.locale = {
"Unknown error occured": "{{ t 'menu.General.Unknown error occured' }}", "Unknown error occured": "{{ t 'menu.General.Unknown error occured' }}",