Add js file before loading/rendering header.tpl in middleware
-
Is this possible? Because I have a problem, in my header.tpl, I included script that gets/sets token and request for logged in user from different server, the problem is that I have to refresh the page twice so that the isLoggedIn validation in menu.tpl will be TRUE, because in actual, the first time I load the page Header.tpl, menu.tpl is already loaded then ---> will read get/set token --> refresh page twice (must be refreshed only once):
Is there a way that script file for get/set token will be loaded first before the renderHeader in middleware.js?
This is my script:
**<script>
//START TOKEN
var _session = {};
var domain = config_ws.cookieUrl? 'domain='+config_ws.cookieUrl+';': '';
var dmName = document.createElement("input");
dmName.setAttribute('type', 'hidden');
dmName.setAttribute('value', domain);
dmName.setAttribute('id', 'domainValue');
_session.setToken = function(t) {
// we won't be using the $cookie service when setting the token
// because it doesn't allow setting the domainif (!t || t == '') { //$cookies.accessToken = ''; document.cookie = 'accessToken=; expires=Thu, 01 Jan 1970 00:00:00 GMT; '+domain+' path=/'; } else { //$cookies.accessToken = t; document.cookie = 'accessToken='+t+'; expires=0; '+domain+' path=/'; } }; _session.getToken = function() { /*if ($cookies.accessToken && $cookies.accessToken != '') { return $cookies.accessToken; } else { return ''; }*/ var match = document.cookie.match(new RegExp('accessToken=([^;]+)')); return match && match[1]? match[1]: ''; }; _session.hasToken = function() { return _session.getToken().length > 0? true: false; }; var csrf = $('#csrf_token').val(); var userLoggedIn = $('#user_loggedin').val(); if(userLoggedIn == "true") { var userWsid = $('#user_wsid').val(); if(userWsid == 0) { // means user logged in is a NodeBB User // level of priority NodeBB User } else { // means user logged in is from WS setUserToken(); } } else { setUserToken(); } function setUserToken() { // means user logged in is from WS if(_session.hasToken() == true) { $.ajax({ type: "GET", beforeSend: function (request) { request.setRequestHeader("Authorization", "Bearer " + _session.getToken()); }, url: config_ws.wsBaseUrl + "/api/user/get", success: function(wsuser) { // console.dir(wsuser); //get billing info $.ajax({ type: "GET", beforeSend: function (request) { request.setRequestHeader("Authorization", "Bearer " + _session.getToken()); _session.setToken(_session.getToken()); }, url: config_ws.wsBaseUrl + "/api/user/billing-info?userId=" + wsuser.userId, success: function(wslocation) { // create or update ws user $.ajax({ type: "POST", data:{user: wsuser, _csrf: csrf, wsBaseUrl: config_ws.wsBaseUrl, wsuserlocation: wslocation}, url: RELATIVE_PATH + '/api/wsuser', cache: false, success: function(res) { // if (previousUrl) { // app.previousUrl = previousUrl; // } else if (!app.previousUrl) { // app.previousUrl = '/'; // } //window.location.replace(window.location.pathname); app.loadConfig(); }, complete: function() { setTimeout(setUserToken,1000); //After completion of request, time to redo it after a second }, error: function() { console.log("error-post"); }, dataType: 'json', async: true }); } }); }, error: function(request,error) { console.log(error); }, dataType: 'json', async: true }); } else { if(userLoggedIn == "true") { $.post(RELATIVE_PATH + '/logout', { _csrf: $('#csrf_token').val() }, function() { if(window.location.pathname != RELATIVE_PATH + '/') { window.location.href = RELATIVE_PATH + '/'; } }); } } } // END TOKEN </script>
**
Thank You!
-
Create a plugin.json file in your theme and add a link to the file that contains your JS. See link below.
Then create a folder called lib inside a folder called static, then a new file called main.js. Paste your script in there, and it will be minified along with the core code on startup.
See plugin.json
and main.js
be sure to include use strict.
"use strict"; your code goes here without the script tags }());
-
You're missing the
(function() {
(My turn to troll you)
-
@psychobunny good point.