@Nicolas Thanks a lot. It works nicely
R
ricoud
@ricoud
Posts
-
[nodebb-plugin-ns-awards] NS Awards, Give Medals! -
angular app pluginHere is the angular app main file with routes, I only include the two first one
"use strict"; var fbapp = angular.module("francoBowlApp", ["ngRoute", "angularFileUpload", "ngDragDrop", "ui.bootstrap", "angularUtils.directives.dirPagination", "services"]); // get global variable fbapp.run(["$rootScope", function($rootScope) { $rootScope.fb_webservice = document.getElementById("fbws").textContent;}]); // configure routes fbapp.config(["$routeProvider'","$httpProvider", function($routeProvider, $httpProvider) { var rootFolder = "templates/angular", token = document.getElementById("token").textContent; $routeProvider // route de la page index .when("/", { templateUrl : rootFolder+"/index.tpl", controller : "fbCtrl" }) // route de la page mon franco bowl .when('/monfranco', { templateUrl : rootFolder+"/francobowl/index.tpl", controller : "fbMonFrancoIndexCtrl" }); // include bearer token in each web service call $httpProvider.defaults.headers.common.Authorization = "Bearer"+ token; }]);
Then the main template
<div class="row" ng-app="francoBowlApp" ng-controller="fbCtrl"> <span hidden="hidden" id="token">{token}</span> <span hidden="hidden" id="fbws">{fbws}</span> <span hidden="hidden" id="username">{username}</span> <div class="col-sm-2"> <div class="sidebar-nav"> <div class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <span class="visible-xs navbar-brand">Site menu</span> </div> <div class="navbar-collapse collapse sidebar-navbar-collapse"> <ul class="nav navbar-nav"> <!-- IF uid --> <li><a href="#/monfranco">Mon Franco Bowl</a></li> <!-- ELSE --> <li><a href="#">Accueil</a></li> <!-- ENDIF uid --> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Administrer <b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#/admin/competition">Compétitions</a></li> <li><a href="#/admin/mot_clef">Mots clefs</a></li> <li><a href="#/admin/image">Images</a></li> <li><a href="#/admin/jeu">Jeux</a></li> </ul> </li> <li><a href="#/equipe">Equipes</a></li> </ul> </div><!--/.nav-collapse --> </div> </div> </div> <div class="col-sm-10 col-xs-12"> <div ng-view> </div> </div> </div>
And my library.js
"use strict"; var plugin = {}, user = module.parent.require('./user'), async = module.parent.require('async'), meta = module.parent.require('./meta'), http = require('http'), jwt = require('jsonwebtoken'); function renderAdminPage(req, res, next) { res.render('admin/plugins/francobowl', {}); } function renderCustomPage(req, res, next) { var jwt = require('jsonwebtoken'), err, userInfo = {}, url = req.body.url, config; // recuperer les informations sur l utilisateur async.parallel({ // definition des donnĂ©es utilisateur userData : function(next) { if (req.user && req.user.uid) { // si l utilisateur est authentifie appel a la base de donnees user.getUserData(req.user.uid, next); } else { // si l utilisateur n est pas authentifie definir comme anonyme next(err, {uid : 0, username : 'anonymous'}); } }, // verifier si l utilisateur est un administrateur isAdministrator : function(next) { if (req.user && req.user.uid) { // si l utilisateur est authentifieappel a la base de donnees user.isAdministrator(req.user.uid, next); } else { // si l utilisateur n est pas authentifie definir comme // non administrateur next(err, false); } }, // recuperer les donnees de configuration du franco bowl config : function(next) { meta.settings.get('francobowl', function(err, settings){ if (err) { next(err); } config = settings; next(); }); } }, function(err, data) { // en cas d erreur redirection vers la page d erreur if(err) { return res.redirect(url + '?error=' + err.message); }; // definir les donnees de l utilisateur en fonction des donnees gerees userInfo = { uid : data.userData.uid, username : data.userData.username, admin : data.isAdministrator }; // creer le token pour les appels aux webservice userInfo.token = jwt.sign(userInfo, config.secret_key, { expiresInMinutes: 60 * 5 }); // charger l url des webservices francobowl userInfo.fbws = 'http://' + config.fb_ws + config.fb_root; // charger la page res.render('francobowl-page', userInfo); }); } plugin.addNavigation = function(header, callback) { // ajouter le lien dans le menu de navigation principal header.navigation.push({ class: '', route: '/francobowl', iconClass: 'fa fa-fw fa-shield', title: 'FrancoBowl', text: 'Franco Bowl' }); callback(null, header); }; plugin.addAdminNavigation = function(custom_header, callback) { // ajouter le lien dans le menu de navigation de la page d admin custom_header.plugins.push({ route: '/plugins/francobowl', icon: 'fa-file-archive-o', name: 'Franco Bowl' }); callback(null, custom_header); }; plugin.init = function(params, callback) { // ajouter les routes var app = params.router, middleware = params.middleware; app.get('/FrancoBowl', middleware.buildHeader, renderCustomPage); app.get('/templates/FrancoBowl.tpl', renderCustomPage); app.get('/api/FrancoBowl', renderCustomPage); app.get('/admin/plugins/francobowl', middleware.admin.buildHeader, renderAdminPage); app.get('/api/admin/plugins/francobowl', renderAdminPage); callback(); }; // creer le coach dans la base du francobowl plugin.userData = function(newUser) { // recuperer les donnees d acces au web service du francobowl meta.settings.get('francobowl', function(err, settings){ if (err) { next(err); } // recuperer les donnees de l adminitsrateur user.getUserData(1, function(err, user){ if (err) { next(err); } // definir les donnees Ă transferer var data = JSON.stringify({uid: newUser.uid}), // donnees administrateur pour le JWT userInfo = { uid : user.uid, username : user.username, admin : true }, // les donnees pour la requĂȘte options = { host: settings.fb_ws, path: settings.fb_root + '/coach', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length, 'Authorization': 'Bearer ' + jwt.sign(userInfo, settings.secret_key, { expiresInMinutes: 60 * 5 }) } }, // definir la requete post req = http.request(options, function(res) { var msg = ''; res.on('data', function(chunk){ msg += chunk; }); res.on('end', function(){ if (res.statusCode !== 200) { console.log('Error : ' + res.msg); } }); }); // lancer la requete post req.write(data); req.end(); }); }); }; module.exports = plugin;
Hope you can help and let me know if you want something else.
Thanks in advance -
angular app pluginhello,
here you will find the code https://github.com/ricoud/nodebb-plugin-angtest
Thanks in advance -
angular app plugin@psychobunny did you have time to test the plugin?
Thanks