TUTORIAL

Sync users between Weavy and Confluence

Syncing users between Weavy and Confluence is done to have users show up when creating a new chat, mention, etc., in the Weavy building blocks.

Syncing users should always be done from your server side. Forge uses a bridge that allows you to define backend functions that can be called from the frontend.

1. Modify index.js

Update the index.js file located in the src folder in your forge project and add the syncUser endpoint.
Add the syncUser endpoint to index.js
import Resolver from '@forge/resolver';
import api, { fetch, route } from '@forge/api';

const apiKey = "********************";
const backend = "WEAVY_URL";

resolver.define("syncUser", async (req, res) => {
    // get confluence user data
    const userResponse = await api.asApp().requestConfluence(route`/wiki/rest/api/user?accountId=${req.context.accountId}`, {
        headers: {
            'Accept': 'application/json'
        }
    });

    var user = await userResponse.json();
  
    // user's account id - we will use this as the 'uid' when syncing the user to Weavy
    let uid = user.accountId;

    // user's name
    let name = user.displayName;

    // sync the user to Weavy
    let response = await fetch(`${backend}/api/users/${uid}`, {
        method: 'PUT',
        headers: {
            'content-type': 'application/json',
            'Authorization': `Bearer ${apiKey}`
        },
        body: JSON.stringify({ name: name })
    });
     
    return uid;

});

export const handler = resolver.getDefinitions();

2. Modify App.js

Update the app.js file and add the request to the syncUser endpoint.
Add to App.js
const init = async () => {
    // create weavy context 
    const weavy = new Weavy();
    weavy.url = "WEAVY_URL";
    weavy.tokenFactory = async (refresh) => {
        return await invoke('token', { refresh: refresh });
    };

    // sync user
    await invoke('syncUser', {});    
}
Unlock the tutorial with your Weavy API key.