From 78e3082139891be02280b84ef10bdd638d911f8a Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 20 Oct 2020 10:12:25 -0500 Subject: [PATCH] Add logic for saving session preferences (#17) --- app/controllers/api/v1/Session.controller.js | 17 +++++++++++++++++ app/models/auth/User.model.js | 4 ++++ app/routing/routers/api/v1/session.routes.js | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/Session.controller.js b/app/controllers/api/v1/Session.controller.js index 77a1954..107ace3 100644 --- a/app/controllers/api/v1/Session.controller.js +++ b/app/controllers/api/v1/Session.controller.js @@ -9,11 +9,28 @@ class SessionController extends Controller { return res.api(await this.session_data(req.user)) } + async save_session(req, res, next) { + if ( req?.body?.user?.preferences ) { + const save_prefs = req.body.user.preferences; + req.user.preferences.dark_mode = !!save_prefs.dark_mode; + + if ( save_prefs.default_page ) { + req.user.preferences.default_page = save_prefs.default_page; + } else { + req.user.preferences.default_page = ''; + } + } + + await req.user.save() + return res.api() + } + async session_data(user) { return { user: { id: user.id, username: user.uid, + preferences: user.preferences || {}, }, app: { name: this.configs.get('app.name'), diff --git a/app/models/auth/User.model.js b/app/models/auth/User.model.js index e8b8add..6d75cb1 100644 --- a/app/models/auth/User.model.js +++ b/app/models/auth/User.model.js @@ -13,6 +13,10 @@ class User extends AuthUser { static get schema() { return {...super.schema, ...{ // other schema fields here + preferences: { + dark_mode: { type: Boolean, default: false }, + default_page: String, + }, }} } diff --git a/app/routing/routers/api/v1/session.routes.js b/app/routing/routers/api/v1/session.routes.js index 1932511..46cd820 100644 --- a/app/routing/routers/api/v1/session.routes.js +++ b/app/routing/routers/api/v1/session.routes.js @@ -11,7 +11,7 @@ const index = { }, post: { - + '/': [ 'controller::api:v1:Session.save_session' ], }, }