user preferences

This commit is contained in:
STEINNI
2025-10-07 19:57:19 +00:00
parent 28170c1358
commit 2e85b18ed4
2 changed files with 42 additions and 7 deletions
+14
View File
@@ -0,0 +1,14 @@
export function authGuard(req, res, next) {
const { userInfos, authenticated } = req.session || {}
if (!userInfos || !authenticated) {
let jsonResp = {'success':false,
'payload': null,
'error': {
'displayMessage' : 'Please login first !',
'debugMessage' : 'No session or unauthorized one.'
}
}
return res.status(401).json(jsonResp)
}
next()
}
+28 -7
View File
@@ -1,6 +1,8 @@
import { Utils } from './helpers/utils.js' import { Utils } from './helpers/utils.js'
import { verifyPassword } from './helpers/pwd.js' import { verifyPassword } from './helpers/pwd.js'
import { MySQLClient } from './helpers/mysqlClient.js' import { MySQLClient } from './helpers/mysqlClient.js'
import { authGuard } from './authGuard.js'
export class P42ApiEndpoints{ export class P42ApiEndpoints{
constructor(app, db) { constructor(app, db) {
this.db = new MySQLClient(db, 60) this.db = new MySQLClient(db, 60)
@@ -11,11 +13,11 @@ export class P42ApiEndpoints{
} }
registerPaths(){ registerPaths(){
this.app.get('/hw', this.hw.bind(this))
this.app.get('/checkauth', this.checkauth.bind(this)) this.app.get('/checkauth', this.checkauth.bind(this))
this.app.post('/login', this.login.bind(this)) this.app.post('/login', this.login.bind(this))
this.app.get('/logout', this.logout.bind(this)) this.app.get('/logout', this.logout.bind(this))
this.app.get('/preferences', authGuard, this.getPrefs.bind(this))
this.app.put('/preferences', authGuard, this.setPrefs.bind(this))
} }
err(req, res, msg, debug, status=500) { err(req, res, msg, debug, status=500) {
@@ -26,7 +28,7 @@ export class P42ApiEndpoints{
'displayMessage' : msg, 'displayMessage' : msg,
'debugMessage' : debug 'debugMessage' : debug
} }
}; }
res.set('Content-Type', 'application/json'); res.set('Content-Type', 'application/json');
res.status(status) res.status(status)
res.send(JSON.stringify(jsonResp)); res.send(JSON.stringify(jsonResp));
@@ -59,10 +61,6 @@ export class P42ApiEndpoints{
} }
///////////////////////////API starts here...///////////////////////////// ///////////////////////////API starts here.../////////////////////////////
async hw(req, res) {
this.ok(req, res, {hello:'world'})
}
async checkauth(req, res) { async checkauth(req, res) {
if(req.session.userInfos && req.session.authenticated && req.session.userInfos && req.session.userInfos.identity && req.session.userInfos.identity.username) { if(req.session.userInfos && req.session.authenticated && req.session.userInfos && req.session.userInfos.identity && req.session.userInfos.identity.username) {
this.ok(req, res, { this.ok(req, res, {
@@ -183,4 +181,27 @@ export class P42ApiEndpoints{
} }
} }
async getPrefs(req, res) {
const results = await this.db.execute(`
SELECT * FROM preferences
LEFT JOIN users ON prf_usr_id=usr_id
WHERE usr_uuid = ?
`, [req.session.userInfos.identity.uuid])
let prefs = {}
if(results.length){ prefs = results[0].prf_value }
this.ok(req, res, prefs)
}
async setPrefs(req, res) {
const results = await this.db.execute(`
INSERT INTO preferences (prf_usr_id, prf_value)
SELECT usr_id, ?
FROM users
WHERE usr_uuid = ?
ON DUPLICATE KEY UPDATE prf_value = ?
`, [req.body, req.session.userInfos.identity.uuid, req.body])
this.ok(req, res, {})
}
} }