using this ajax request from client side I want to get the data on server side by authenticating the jwt token sent from client side. Server side(router code) is below,
So, how can I authenticate or validate the url using jwt token by sending through request.SetHeaderRequest? I referred this url, but I couldn’t figure out any solution for it.
Also, I referred another link. It was mentioned that Get method was working with that code, but unfortunately it didn’t work for me. So, can anyone give me a solution for this?
In your server side code (router), to have your calls authenticated with jwt, if you’re using ACE, what I do is have the addon.authenticate() as part of the dependency as such:
If you’re using your add-on user to invoke REST APIs:
app.get('/hello-world', addon.authenticate(), function (req, res) {
let httpClient = addon.httpClient(req);
httpClient.get({
url: `rest/api/<version>/<endpoint>`,
contentType: 'application/json',
json: true
}, (error, res, body) => {
//do something about the body
});
});
If you’re acting as a user:
httpClient.asUser('admin').get({
url: `rest/api/<version>/<endpoint>`,
contentType: 'application/json',
json: true
}, (error, res, body) => {
//do something about the body
});
app.get('/hello-world', addon.authenticate(), function (req, res) {
...
});
Still I get the issue - ‘Authentication verification error: 401 Could not find authentication data on req
uest’, when the above code is used in server side(add-on user to invoke REST APIs).
I have another question over here. When I load the page, I can get jwt token within meta tag. But, when redirecting to another page, there I do not find jwt token, the content within tag is empty when inspected.
Add the token in html head tag: <meta name="token" content="{{token}}">
Extract the token in script: var token = document.querySelector('meta[name="token"]').content
Append Authorization in request headers before sending request back to addon (server) from iframe client (Jira in my case) RequestHeaders["Authorization"] = "JWT ${token}"
Lastly, use addon.checkValidToken() in your route instead of addon.authenticate()