Getting user details

Before loading page in connect add-on I need to get user details using rest api.

Case 1:
When I have two pages I can get details from first page and pass to second page as arguments successfully.

In script file
// Get user timezone
AP.user.getTimeZone(function(timezone){
	outerObj.userTimezone = timezone;
});
		
// Get user locale
AP.user.getLocale(function(locale){
	outerObj.userLocale =locale;
});
		
$("#btnRedirect").on('click', function(e){ 
    AP.navigator.go('addonModule', { addonKey: addon_key, moduleKey: module_key, customData: {tz: outerObj.userTimezone, loc: outerObj.userLocale}});
});

In router file
app.get('/secondpage', addon.authenticate(), (req, res) => {
    var timezone = res.locals.timezone, locale = res.locals.locale,
    res.setHeader('Access-Control-Allow-Origin','*');
    res.render('secondpage', { title: 'Atlassian Connect', Timezone: timezone, Locale: locale });
});

Case2:
But, with single page I couldn’t get the user details(timezone and locale).

How to access AP.request in router file(index.js) before the page loads. So, I can pass the user timzezone and locale as arguments when redirecting to the hbs template file?

Here is the sample code that is used for redirecting in router file(index.js)

app.get('/hello-world', addon.authenticate(), (req, res) => {
    res.setHeader('Access-Control-Allow-Origin','*');
    res.render('hello-world', { title: 'Atlassian Connect' });
});

Hi, @hariprasath! Did you try GET /rest/api/3/myself? I believe this should be enough to achieve what you want.

We are using template file(hbs) which is processed on server side. As rest api cannot be used on server side what is the recommended way to solve this? Also, to note the user’s locale and timezone in context parameters are deprecated.

Is this a duplicate from Jira add-on page Internationalization?

In addition to the answer by @remie you can achieve this with the request:

httpClient.get(`/rest/api/3/user?accountId=${userId}`, (err, response, body) => {

})

3 Likes