t
Missing Cookies
Why are cookies not being included in my requests? I'm successfully creating a session. But when I then try to call a route with the
verifySession()
middleware, I get a 401 and these debug messages:
Copy code
Not handling because no recipe matched
...
requestRID is: undefined
...
UNAUTHORISED because idRefreshToken from cookies is undefined
...
Session does not exist. Are you sending the session tokens in the request as cookies?
My understanding was that the axios interceptors would handle storing & including cookies in subsequent requests. But I still see those errors after adding the
addAxiosInterceptors
middleware (via both
axios.create()
and manually in each axios import Any idea what I'm doing wrong? ---- More info: - I went through the Supertokesn Postman tutorial without issues. Since Postman handles the cookie stuff, it makes me think that I might need to do something on my end to ensure supertokens cookies are always sent in requests, in addition to adding the axios interceptors(?) ---- Stack: Node, React, Axios, SuperTokens Managed Hosting
j
Hey @tylerdane , are you using axios on your frontend? If so have you added the supertokens axios interceptors
Could you send a screenshot of the set cookie headers from the login request
t
Hey @jscyo - Yes, I've tried adding the inteceptors via these methods:
Copy code
import axios from "axios";
import Session from "supertokens-auth-react/recipe/session";

Session.addAxiosInterceptors(axios);

... my api calls
Copy code
import axios from "axios";
import Session from "supertokens-auth-react/recipe/session";

export const MyApi = axios.create({
  baseURL: http://localhost:3000/api,
 });

Session.addAxiosInterceptors(MyApi);
... my api calls
More code snippets:
Copy code
//my.api.ts
import axios from "axios";
Session.addAxiosInterceptors(axios);

export const createSession = async () => {
  const response = await axios.post(`http://localhost:3000/api/session`);
};

export const verifySession = async () => {
  const response = await axios.post(`http://localhost:3000/api/verify-demo`);
};

----

//MyComponent.tsx
import {createSession, verifySession} from "./my.api"

return (
<button onClick={async () => await createSession()}>Create Session</button>
<button onClick={async () => await verifySession()}>Verify Session</button>
)

----

// my.backend.ts

// this works
app.post("/api/session", async (req, res) => {
  const uid = Math.floor(Math.random() * 100);
  const user = `test-user-${uid}`;
  await Session.createNewSession(res, user, {}, {});
  res.send({
    message: `user session created: ${user}`,
  });
});

// this fails
app.post(
  "/api/verify-demo",
  verifySession(),
  async (req: SessionRequest, res) => {
    const userId = req.session!.getUserId();
    res.send({
      userId,
    });
  }
);
Ahhhhh, silly silly me. I mistakingly set the wrong
apiDomain
port (by using the frontend port, not backend port) For reference, here's what I mean:
Copy code
//app.ts

SuperTokens.init({
  enableDebugLogs: true,
  appInfo: {
    appName: "My App",
    apiDomain: "http://localhost:<backendPort>", // <-- I was trying to use frontend port fhere
    websiteDomain: "http://localhost:<frontendPort>",
    apiBasePath: "/api",
    websiteBasePath: "/super",
  },
  recipeList: [Session.init()],
});
We're all good now
r
Ok great!
68 Views