Project

General

Profile

AJAX connection with API key resets session

Added by Faun Woeci over 10 years ago

Hello everyone,

My company choosed redmine to manage bug tracking. We use the spent hour field of issues to report activities both from users and lifecycle issues. As a team manager, I spend a lot of time filling up spent time fields as I have to overview the global activity and sometimes I give a hand to others issues. I decided to write down a js bookmarklet so when my current page is a issue's one, I just click my bookmark to add 0.05 hour to the current issue.

The code is functionnal and the time displayed on the issue page is updated at the end of the process. But I have an authentication issue here. If I use the bookmarklet, I have to reconnect next time I display (or reload) a redmine page.

I use the following header to authenticate : X-Redmine-API-Key and I can see the cookie changing as I have the spent time information updated. I guess the redmine session is reset.

Do you know any workaround to this problem? Do I need to change the authentication method, for example using the _redmine_session cookie (if possible)?

here is the bookmarklet sample code:

var hours=0.05;
var activity=8;
var api_key="fff5f-----------------------------9f2d15b";
var params = '{"time_entry":{"issue_id":'+window.location.pathname.split('/')[2]+',"hours":'+hours+',"activity_id":'+activity+'}}';

function xhr(path,method){
  wl=window.location;
  x=new XMLHttpRequest();
  x.open(method,wl.protocol+'//'+wl.host+path,true);
  x.setRequestHeader("Content-type","application/json");
  x.setRequestHeader("X-Redmine-API-Key",api_key);
  return x;
}
function timeUpdate(id){
  x=xhr('/issues/'+id+'.json','GET');
  x.onreadystatechange=function()
    {
    if (x.readyState==4 && x.status==200)
      {
          r = JSON.parse(x.responseText);
          document.querySelector('.spent-time a').innerHTML=parseFloat(r.issue.spent_hours).toFixed(2);
      }
    };
  x.send();
};

x=xhr('/time_entries.json','POST');
x.onreadystatechange=function()
  {
  if (x.readyState==4 && x.status==201)
    {
        r = JSON.parse(x.responseText);
        if(r.time_entry.issue.id != undefined){
          timeUpdate(r.time_entry.issue.id);
        }
    }
  };

x.send(params);
void(0);

Thank you.