Correct way to redirect to another addon-page from the express router

Let us say we have these to pages defined

"generalPages": [
    {
        "location": "system.top.navigation.bar",
        "name": {
            "value": "Page A"
        },
        "key": "key-a",
        "url": "/url-a",
        "conditions": [
            {
                "condition": "user_is_admin"
            }
        ]
    },
    {
        "location": "system.top.navigation.bar",
        "name": {
            "value": "Page B"
        },
        "key": "key-b",
        "url": "/url-b",
        "conditions": [
            {
                "condition": "user_is_admin"
            }
        ]
    }
]

Now in my router I want to redirect from page B to page A. First I tried this:

app.get('/url-b', addon.authenticate(), async function(req: any, res: any) {
    res.redirect('url-a') //DOES NOT WORK AS no JWT token is passed
})

Then this:

app.get('/url-b', addon.authenticate(), async function(req: any, res: any) {
    res.redirect('url-a?jwt=' + req.query['jwt']) // DOES NOT WORK as every page has a unique JWT
})

What is the correct way to achieve this?

Have you checked the Connect JS API, specifically AP.go()? (the routing will then happen in the F/E)
https://developer.atlassian.com/cloud/jira/platform/jsapi/navigator/

Hi @SebastianK81,

Can I first ask why do you want to redirect the handling? If it is to singularise the bulk of the implementation of multiple end points, then I think the initial handling should authenticate the JWT and pass some context to a common internal implementation. If you are redirecting for another reason, then maybe there’s a different solution.

Regards,
Dugald

@dmorrow Page B is managing a meta configuration between jira and our system. Before the content is rendered the addon-configuration should be checked (e.g. API connection to our system is working). If the configuration is not okay, the user should be redirected to the add-on configuration page (Page A) with a flash message, asking to fix the configuration.
Possible solutions that I can come up with, but are not perfect:

  • Using the solution of @dciupureanu. So I render an HTML what does execute AP.go() right after loading the page.
  • Showing an error message on page B and asking the user to click a link to page A

Hi @SebastianK81,

Did you check the response headers from Express? And how exactly the request after the redirect differs from the first one?

The Express default behavior for res.redirect is sending an http 302 status code along with the new URL. Technically, this should prompt the browser to try an identical request to new URL. I vaguely remember that this is not quite what happened when we initially started out with ACE. Apparently browser interpreted the ‘identical’ quite freely and sometimes chose to change the http verb or make other changes. HTTP/1.1 then introduced the 307 Temporary Redirect status code to solve this issue.
See here:

So, instead of using res.redirect(’/new-url’) we went with res.redirect(307, ‘/new-url’) which seemed to do the trick. It might be worth a try in your case, too.

Hope that helps,
Oliver

You should redirect to JIRABASEURL/plugins/servlet/ac/your-app-key/url-a

Thank you, I will have a look into hat. Can you please fix your “See here” link?

1 Like

Done. Apparently, the markdown parser does not like links with an ‘#’ anchor.

1 Like