Add logic for saving session preferences (#17)
This commit is contained in:
parent
c86b82bd0c
commit
78e3082139
@ -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'),
|
||||
|
@ -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,
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ const index = {
|
||||
},
|
||||
|
||||
post: {
|
||||
|
||||
'/': [ 'controller::api:v1:Session.save_session' ],
|
||||
},
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user