<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I have a question regarding the express.sid cookie:</p>
<p dir="auto">After login, the express.sid is generated and stored in the cookies.</p>
<p dir="auto">I tried using this session ID to fetch the CSRF token by calling /api/config, but it doesn’t seem to work for me.</p>
<p dir="auto">My goal is:</p>
<p dir="auto">Successfully retrieve a valid CSRF token.</p>
<p dir="auto">Use it to create topics or posts via the Write API.</p>
<p dir="auto">Ensure that once I have this token and session, I can access all the required NodeBB APIs.</p>
<p dir="auto">Could you please clarify the correct approach to:</p>
<p dir="auto">Retrieve and use the CSRF token with the express.sid?</p>
<p dir="auto">Authenticate API requests (like creating topics or posts) when using Keycloak for login instead of NodeBB’s built-in login?</p>
<p dir="auto">Any guidance or best practices for this integration would be greatly appreciated.</p>
<p dir="auto">app.post('/api/login', async (req, res) =&gt; {<br />
try {<br />
const { username, password } = req.body || {}<br />
if (!username || !password) {<br />
return res.status(400).json({ error: 'Missing username or password' })<br />
}<br />
// 1) Login against Keycloak (Direct Access Grant)<br />
const tokenResponse = await fetchKeycloakTokens({ username, password });</p>
<pre><code>// 2) Get user profile to build NodeBB payload
const userinfo = await fetchUserInfo({ accessToken: tokenResponse.access_token });

// 3) Build session-sharing JWT for NodeBB
// Minimal fields: id (unique), username, email
const nodebbPayload = {
    id: userinfo.sub || userinfo.preferred_username || username,
    username: userinfo.preferred_username || username,
    email: userinfo.email || undefined,
    fullname: userinfo.name || undefined,
};
if (!SESSION_SHARING_JWT_SECRET) {
    return res.status(500).json({ error: 'Server not configured: SESSION_SHARING_JWT_SECRET missing' });
}
const signed = await jwt.sign(nodebbPayload, SESSION_SHARING_JWT_SECRET, { expiresIn: '1h' });

 const loginRes = await axios.post('http://localhost:4567/api/v3/utilities/login', {
    username,
    password
}, {
    headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${signed}`, // pass your Bearer token here
    },
    withCredentials: true
});

console.log(loginRes, "LOGIN RESPONSE");
// 4) Set cookie for NodeBB domain so it can pick it up
const response = await axios.get('http://192.168.60.108:4567/api/config', { withCredentials: true });
const newToken = response?.data?.csrf_token;

const cookieOptions = {
    httpOnly: true,
    secure: false,
    sameSite: 'none',
    path: '/',
    maxAge: 8 * 60 * 60 * 10000,
    domain: '192.168.60.108'
}

res.cookie("token", signed, cookieOptions);
res.cookie('csrf_token', newToken, cookieOptions);

return res.json({ success: true, message: "login successfully", redirect: NODEBB_BASE_URL, keycloakAccessToken: tokenResponse.access_token });
</code></pre>
<p dir="auto">} catch (err) {<br />
// eslint-disable-next-line no-console<br />
console.error('Login error:', err?.response?.data || err?.message || err);<br />
const status = err?.response?.status || 500<br />
const message = err?.response?.data?.error_description || err?.response?.data?.error || err?.message || 'Unexpected error';<br />
return res.status(status).json({ success: false, error: message });<br />
}</p>
<p dir="auto">});<br />
In the /api/login API, I called the /api/config API, but when I check the /api/config API in Postman using the express.sid generated after login, it never returns the response for that specific user.</p>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://bbs.spacc.eu.org/new/topic/35219c15-275f-46ad-a5fc-adf7a47ae01c/unable-to-retrieve-csrf-token-via-api-config-using-express.sid-after-keycloak-login</link><generator>RSS for Node</generator><lastBuildDate>Mon, 09 Nov 2026 14:22:01 GMT</lastBuildDate><atom:link href="https://bbs.spacc.eu.org/new/topic/35219c15-275f-46ad-a5fc-adf7a47ae01c.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Sep 2025 04:52:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Tue, 16 Sep 2025 05:00:58 GMT]]></title><description><![CDATA[<p dir="auto"><a href="https://community.nodebb.org/user/balu" target="_blank" rel="noopener noreferrer nofollow ugc">balu</a> the cookie is generated by the express-cookie package.</p>
<p dir="auto">If you're receiving uid 0 it means the cookie isn't being passed in correctly.</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105833</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105833</guid><dc:creator><![CDATA[julian@community.nodebb.org]]></dc:creator><pubDate>Tue, 16 Sep 2025 05:00:58 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Tue, 16 Sep 2025 04:58:22 GMT]]></title><description><![CDATA[<p dir="auto">Thanks <a href="https://community.nodebb.org/user/julian" target="_blank" rel="noopener noreferrer nofollow ugc">julian</a>  for your reply, and sorry for my poor communication earlier. My main issue is that I’m using SSO with Cognito. After a successful login, I call the /api/config API using the generated express.sid, but the response always returns uid: 0. My question is: how is the express.sid generated? I’m using React.js for the frontend, not the default NodeBB UI.</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105831</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105831</guid><dc:creator><![CDATA[balu@community.nodebb.org]]></dc:creator><pubDate>Tue, 16 Sep 2025 04:58:22 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Tue, 16 Sep 2025 04:52:43 GMT]]></title><description><![CDATA[<p dir="auto"><a href="https://community.nodebb.org/user/balu" target="_blank" rel="noopener noreferrer nofollow ugc">balu</a> please continue the conversation here so others may contribute</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105830</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105830</guid><dc:creator><![CDATA[julian@community.nodebb.org]]></dc:creator><pubDate>Tue, 16 Sep 2025 04:52:43 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Tue, 16 Sep 2025 04:32:50 GMT]]></title><description><![CDATA[<p dir="auto"><a href="https://community.nodebb.org/user/julian" target="_blank" rel="noopener noreferrer nofollow ugc">julian</a> Thank you for your reply, I am DM my question.</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105829</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105829</guid><dc:creator><![CDATA[balu@community.nodebb.org]]></dc:creator><pubDate>Tue, 16 Sep 2025 04:32:50 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Mon, 15 Sep 2025 15:38:17 GMT]]></title><description><![CDATA[<p dir="auto"><a href="https://community.nodebb.org/user/balu" target="_blank" rel="noopener noreferrer nofollow ugc">balu</a> hey, sorry for the delay, can you post a cURL call to the <code>/api/config</code> endpoint with the cookie?</p>
<p dir="auto">I know you posted a code sample (via DM) but the reason I ask for a cURL call is that it is the most straightforward way to test the call and often eliminates errors in header values, etc.</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105825</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105825</guid><dc:creator><![CDATA[julian@community.nodebb.org]]></dc:creator><pubDate>Mon, 15 Sep 2025 15:38:17 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Mon, 15 Sep 2025 07:09:34 GMT]]></title><description><![CDATA[<p dir="auto"><a href="https://community.nodebb.org/user/henrycharles" target="_blank" rel="noopener noreferrer nofollow ugc">HenryCharles</a> Thank you. I’m using the session-sharing plugin, but I’m facing an issue. After logging in with SSO, I call NodeBB APIs directly (not the UI). The express.sid is generated, and I use that express.sid to get the CSRF token. However, when I call the /api/config API, the response always gives uid: 0 instead of the logged-in user’s UID.</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105822</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105822</guid><dc:creator><![CDATA[balu@community.nodebb.org]]></dc:creator><pubDate>Mon, 15 Sep 2025 07:09:34 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to Retrieve CSRF Token via &#x2F;api&#x2F;config Using express.sid After Keycloak Login on Fri, 12 Sep 2025 10:34:01 GMT]]></title><description><![CDATA[<p dir="auto">You’ll need both the session cookie and its matching CSRF token. Log in so NodeBB sets the cookie, then call /api/config with it to get the token. With Keycloak, it’s best to use the session-sharing plugin and JWT so NodeBB handles sessions and CSRF for you.</p>
]]></description><link>https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105804</link><guid isPermaLink="true">https://bbs.spacc.eu.org/new/post/https://community.nodebb.org/post/105804</guid><dc:creator><![CDATA[henrycharles@community.nodebb.org]]></dc:creator><pubDate>Fri, 12 Sep 2025 10:34:01 GMT</pubDate></item></channel></rss>