Hi, I’m using the new library solid-client-authn-node
to authenticate against my local community solid server (v1.0.0).
Based on the example code for multiple sessions.
Is it correct that console.log(session.info.isLoggedIn);
in /fetch prints false
?
const express = require("express");
const cookieSession = require("cookie-session");
const {
getSessionFromStorage,
getSessionIdFromStorageAll,
Session
} = require("@inrupt/solid-client-authn-node");
const app = express();
const port = 8081;
// The following snippet ensures that the server identifies each user's session
// with a cookie using an express-specific mechanism
app.use(
cookieSession({
name: "session",
// These keys are required by cookie-session to sign the cookies.
keys: [
"Required, but value not relevant for this demo - key1",
"Required, but value not relevant for this demo - key2",
],
maxAge: 24 * 60 * 60 * 1000, // 24 hours
})
);
app.get("/login", async (req, res, next) => {
const session = new Session();
req.session.sessionId = session.info.sessionId;
const redirectToSolidIdentityProvider = (url) => {
res.redirect(url);
};
await session.login({
redirectUrl: `http://localhost:${port}/redirect-from-solid-idp`,
oidcIssuer: "http://localhost:3000",
clientName: "Demo app",
handleRedirect: redirectToSolidIdentityProvider,
});
});
app.get("/redirect-from-solid-idp", async (req, res) => {
const session = await getSessionFromStorage(req.session.sessionId);
await session.handleIncomingRedirect(`http://localhost:${port}${req.url}`);
if (session.info.isLoggedIn) {
return res.send(`<p>Logged in with the WebID ${session.info.webId}.</p>`)
}
});
app.get("/fetch", async (req, res, next) => {
if(typeof req.query["resource"] === "undefined") {
res.send(
"<p>Please pass the (encoded) URL of the Resource you want to fetch using `?resource=<resource URL>`.</p>"
);
}
const session = await getSessionFromStorage(req.session.sessionId);
console.log(session.info.isLoggedIn);
console.log(await (await session.fetch(req.query["resource"])).text());
res.send("<p>Performed authenticated fetch.</p>");
});
// 7. To log out a session, just retrieve the session from storage, and
// call the .logout method.
app.get("/logout", async (req, res, next) => {
const session = await getSessionFromStorage(req.session.sessionId);
session.logout();
res.send(`<p>Logged out.</p>`);
});
// 8. On the server side, you can also list all registered sessions using the
// getSessionIdFromStorageAll function.
app.get("/", async (req, res, next) => {
const sessionIds = await getSessionIdFromStorageAll();
for(const sessionId in sessionIds) {
// Do something with the session ID...
}
res.send(
`<p>There are currently [${sessionIds.length}] visitors.</p>`
);
});
app.listen(port, () => {
console.log(
`Server running on port [${port}]. ` +
`Visit [http://localhost:${port}/login] to log in to [broker.pod.inrupt.com].`
);
});