Add logic for saving session preferences (#17)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Garrett Mills 2020-10-20 10:12:25 -05:00
parent c86b82bd0c
commit 78e3082139
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
3 changed files with 22 additions and 1 deletions

View File

@ -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'),

View File

@ -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,
},
}}
}

View File

@ -11,7 +11,7 @@ const index = {
},
post: {
'/': [ 'controller::api:v1:Session.save_session' ],
},
}