Add support for fetching session data; add /start url
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

(Noded/frontend#15)
This commit is contained in:
Garrett Mills 2020-10-12 20:31:57 -05:00
parent 6ee03ec0f8
commit 273460b126
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
4 changed files with 82 additions and 24 deletions

View File

@ -7,33 +7,44 @@ const Controller = require('libflitter/controller/Controller');
* are used as handlers for routes specified in the route files. * are used as handlers for routes specified in the route files.
*/ */
class Home extends Controller { class Home extends Controller {
static get services() { static get services() {
return [...super.services, 'configs']; 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')
} }
/* /*
* Return the welcome view. * Serve the main welcome page.
* 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 }); welcome(req, res) {
} if (req.user) {
async get_login(req, res) { // If we have a user, redirect them to the main app
const AppName = this.configs.get('app.name'); return res.redirect('/i')
return res.page('login', { AppName }); }
}
toApp(req, res) { /*
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; module.exports = Home;

View File

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

View File

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

View File

@ -41,9 +41,12 @@ const index = {
// e.g. controller::Home.welcome // e.g. controller::Home.welcome
'/': ['controller::Home.welcome'], '/': ['controller::Home.welcome'],
'/stat': ['controller::Home.get_stat'],
// Placeholder for auth dashboard. You'd replace this with // Placeholder for auth dashboard. You'd replace this with
// your own route protected by 'middleware::auth:UserOnly' // 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'], '/login': ['middleware::auth:GuestOnly', 'controller::Home.get_login'],
'/test-json': ['controller::Export.json_export'], '/test-json': ['controller::Export.json_export'],
'/test-markdown': ['controller::Export.markdown_export'], '/test-markdown': ['controller::Export.markdown_export'],