Hey everyone, I'm facing an issue with my Express backend setup along with Supertokens for session ...
s
Hey everyone, I'm facing an issue with my Express backend setup along with Supertokens for session management. Here's a snippet of my backend code:
Copy code
javascript
import express from "express";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from "supertokens-node/framework/express";

let app = express();

app.post("/like-comment", verifySession(), (req: SessionRequest, res) => {
    let userId = req.session!.getUserId();
    //....
});
I've added
verifySession()
to ensure that only authenticated users can access the
/like-comment
route. However, I'm having trouble accessing this route from the front-end using Axios. I've wrapped my component inside
SessionAuth
so that logged-in users have access, but I keep receiving a 401 error. Could anyone guide me on how to properly access this route from the front-end using Axios and ensure that the user is authenticated? Thanks in advance!
r
Hey @sammon2412
Can you enable backend debug logs and show the output when you call this API?
s
Hi @rp_st These are the logs after successfully logging into the system
I also changed my backend to python, still the issue is there
https://supertokens.com/docs/thirdparty/custom-ui/handling-session-tokens
From this document I understood that , If I called `supertokens.init`then I don't need to do anything while calling APIs
logs are saying like, Token is not passing when an API call happens!
That's why backend is sending
401 Unauthorized
Copy code
logs
[DEBUG]    2024-02-09T15:47:14.092Z    6db5db23-3a68-446f-856d-c8859a687f1b    
{
    "t": "2024-02-09T15:47:14.092Z",
    "sdkVer": "0.18.7",
    "message": "errorHandler: Error is from SuperTokens recipe. Message: Session does not exist. Are you sending the session tokens in the request with the appropriate token transfer method?",
    "file": "supertokens.py:662"
}
r
seems like the request doesn't have the access token in it. Can i see the request headers?
as seen on chrome network tab
right yeaa.. access token is missing
whats the sign in API response headers?
s
See the tokens are coming and setting them in the cookies?
What is going wrong here?
r
whats the orange triangle on the right?
Can you hover on it?
s
r
are you using incognito?
s
No
This is my config.tsx
r
well, the issue is that the browser is not accepting cookies cause of samesite=none
you should switch to header based auth
instead of cookie based
and then it should work
s
Ok, let me try that
it's a simple config change on the frontend
s
Ok
Still 401 Unauthorized
Copy code
js
export const SuperTokensConfig = {
  appInfo: {
    appName: "croohm",
    apiDomain: getApiDomain(),
    websiteDomain: getWebsiteDomain(),
    apiBasePath: "/auth", // Set the api_base_path here
    // websiteBasePath: "/auth", // Set the website_base_path here
    apiGatewayPath: "/prod", // Set the api_gateway_path here
  },
  // recipeList contains all the modules that you want to
  // use from SuperTokens. See the full list here: https://supertokens.com/docs/guides
  recipeList: [
    EmailPassword.init(),
    Session.init({
      tokenTransferMethod: "header", // or "cookie"
    }),
  ],
};
do I need change anything in backend?
r
did you sign out and sign in?
i mean you need to clear all the storage and relogin and then see
no need for backend config change. Just frontend
s
Ok
Yes It's working now
Thank you so much @rp_st
7 Views