From 273460b1267e18cd2a0ee7692f35d9c39d9614b5 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Mon, 12 Oct 2020 20:31:57 -0500 Subject: [PATCH] Add support for fetching session data; add /start url (Noded/frontend#15) --- app/controllers/Home.controller.js | 57 ++++++++++++-------- app/controllers/api/v1/Session.controller.js | 26 +++++++++ app/routing/routers/api/v1/session.routes.js | 18 +++++++ app/routing/routers/index.routes.js | 5 +- 4 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 app/controllers/api/v1/Session.controller.js create mode 100644 app/routing/routers/api/v1/session.routes.js diff --git a/app/controllers/Home.controller.js b/app/controllers/Home.controller.js index 6c3ab35..951e1b7 100644 --- a/app/controllers/Home.controller.js +++ b/app/controllers/Home.controller.js @@ -7,33 +7,44 @@ const Controller = require('libflitter/controller/Controller'); * are used as handlers for routes specified in the route files. */ class Home extends Controller { - static get services() { - return [...super.services, 'configs']; - } - - /* - * Serve the main welcome page. - */ - welcome(req, res) { - if (req.user) { - // If we have a user, redirect them to the main app - return res.redirect('/i') + static get services() { + return [...super.services, 'configs']; } /* - * Return the welcome view. - * The page() method is added by Flitter and passes some - * helpful contextual data to the view as well. + * Serve the main welcome page. */ - return res.page('welcome', { user: req.user }); - } - async get_login(req, res) { - const AppName = this.configs.get('app.name'); - return res.page('login', { AppName }); - } - toApp(req, res) { - return res.redirect('/i'); - } + welcome(req, res) { + if (req.user) { + // If we have a user, redirect them to the main app + return res.redirect('/i') + } + + /* + * Return the welcome view. + * The page() method is added by Flitter and passes some + * helpful contextual data to the view as well. + */ + return res.page('welcome', { user: req.user }); + } + + async get_login(req, res) { + const AppName = this.configs.get('app.name'); + return res.page('login', { AppName }); + } + + toApp(req, res) { + return res.redirect('/i'); + } + + async get_stat(req, res, next) { + return res.api({ + noded: true, + app_name: this.configs.get('app.name'), + system_base: this.configs.get('app.url'), + authenticated_user: !!req.user, + }) + } } module.exports = Home; diff --git a/app/controllers/api/v1/Session.controller.js b/app/controllers/api/v1/Session.controller.js new file mode 100644 index 0000000..77a1954 --- /dev/null +++ b/app/controllers/api/v1/Session.controller.js @@ -0,0 +1,26 @@ +const { Controller } = require('libflitter') + +class SessionController extends Controller { + static get services() { + return [...super.services, 'configs'] + } + + async get_session(req, res, next) { + return res.api(await this.session_data(req.user)) + } + + async session_data(user) { + return { + user: { + id: user.id, + username: user.uid, + }, + app: { + name: this.configs.get('app.name'), + url: this.configs.get('app.url'), + }, + } + } +} + +module.exports = exports = SessionController diff --git a/app/routing/routers/api/v1/session.routes.js b/app/routing/routers/api/v1/session.routes.js new file mode 100644 index 0000000..1932511 --- /dev/null +++ b/app/routing/routers/api/v1/session.routes.js @@ -0,0 +1,18 @@ +const index = { + + prefix: '/api/v1/session', + + middleware: [ + 'auth:UserOnly', + ], + + get: { + '/': [ 'controller::api:v1:Session.get_session' ], + }, + + post: { + + }, +} + +module.exports = exports = index diff --git a/app/routing/routers/index.routes.js b/app/routing/routers/index.routes.js index 51161dc..184d65e 100644 --- a/app/routing/routers/index.routes.js +++ b/app/routing/routers/index.routes.js @@ -41,9 +41,12 @@ const index = { // e.g. controller::Home.welcome '/': ['controller::Home.welcome'], + '/stat': ['controller::Home.get_stat'], + // Placeholder for auth dashboard. You'd replace this with // your own route protected by 'middleware::auth:UserOnly' - '/dash': ['controller::Home.toApp'], + '/dash': ['middleware::auth:UserOnly', 'controller::Home.toApp'], + '/start': ['middleware::auth:UserOnly', 'controller::Home.toApp'], '/login': ['middleware::auth:GuestOnly', 'controller::Home.get_login'], '/test-json': ['controller::Export.json_export'], '/test-markdown': ['controller::Export.markdown_export'],