Hey! I have written an api to return an session us...
# support-questions-legacy
r
Hey! I have written an api to return an session using the received cookie. Took reference from here: https://supertokens.com/docs/passwordless/nextjs/app-directory/session-helpers
Copy code
app.get("/auth/internal/getSSRSession", authenticate, async (req: Request, res) => {
    let cookie: string = req.headers.cookie as string;
    console.log(parseCookieString(cookie));
    let baseRequest = new PreParsedRequest({
        method:  "get",
        url: "",
        query: {},
        headers: new NodeHeaders(req.headers),
        cookies: parseCookieString(cookie),
        getFormBody: req.body,
        getJSONBody: req.body,
    });

    let baseResponse = new CollectingResponse();

    try {
        let session = await Session.getSession(baseRequest, baseResponse);
        return {
            session,
            hasInvalidClaims: false,
            hasToken: session !== undefined,
            baseResponse,
        };
    } catch (error) {
        log.error("An error occurred while getting SSR session" + error);
        res.status(500).json({ success: false, error: "Internal server error" });
    }
});
But even for valid cookies, it is saying "Failed to verify access token".
r
How are you sending the cookies? What is setting the cookies? How do you know the access token you are sending is valid?
r
So, I am logging the cookies in my custom validator. I am picking the cookies value from the header. Its format was colon separated so created a custom parser to convert it into map of which was the required type in base request. Custom parser: export const parseCookieString = (cookieString: string): Record => { const cookies: Record = {}; cookieString.split(';').forEach(cookiePair => { const [key, value] = cookiePair.trim().split('='); if (key && value) { cookies[key] = value; } }); return cookies; };
Output after this parsing:
Copy code
{
  sAccessToken: 'eyJraWQiOiJkLTE3MDQzMjY5MzQzNDkiLCJ0eXAiOiJKV1QiLCJ2ZXJzaW9uIjoiNSIsImFsZyI6IlJTMjU2In0.eyJpYXQiOjE3MDQzMzExMjUsImV4cCI6MTcwNDMzMTI0NSwic3ViIjoiMTlkZDk5ZTktNWQzZC00YTM0LWEwMmEtZmM0MTg4YTE4MzYyIiwidElkIjoicHVibGljIiwicnN1YiI6IjE5ZGQ5OWU5LTVkM2QtNGEzNC1hMDJhLWZjNDE4OGExODM2MiIsInNlc3Npb25IYW5kbGUiOiIxOTJiZGNmZC0wOTYzLTQ0Y2MtOTg5OC1iM2JmNTlhOGQ5NDEiLCJyZWZy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5MWVkNWE4MWIzOWQzOTkxYmFjZTIwYiIsInBhcmVudFJlZnJlc2hUb2tlbkhhc2gxIjpudWxsLCJhbnRpQ3NyZlRva2VuIjpudWxsLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjMwMDEvYXV0aCJ9fQ'
}
r
im not sure if you need to create the parseCookieString function yourself.
in our docs, we have:
Copy code
const parsedCookies: Record<string, string> = Object.fromEntries(
        (req !== undefined ? req.cookies : cookies()).getAll().map((cookie) => [cookie.name, cookie.value])
    );
Here: https://supertokens.com/docs/thirdpartyemailpassword/nextjs/app-directory/session-helpers Have you tried this?
r
Yes, I am following this only. But I am using node js and the headers which are used in next were not present. import { cookies, headers } from "next/headers";
r
If you have the request object, you need to use
req.cookies
as shown in the docs
r
Issue is the input ccokie in an string. So, I wrote an custom logic which is doing the same mentioned in the above link. We ultimately wanted a parsed cookie in the form of Record.
r
right
@at_supertokens can you have a look at this issue?
@roushan_46400 i wonder why req.cookies is a string for you, but in our testing, it wasnt a string
also, could you enable backend debug logs and show the output of it when you call the getSesssion function?
r
Basically it is how it is received for all the other apis, I checked the request for '/sessioninfo' api and there it was passed as a string in header.
I have enabled the backend debug logs but still not getting any debug logs on the console: I did this: export const SuperTokensConfig: any = { debug: true,
r
which version of the node sdk are you using?
r
^16.11.38
r
huh? thats not a version we even have
the latest supertokens-node sdk version is 16.6.8
r
Oh! you meant supertokens-node, that is "supertokens-node": "latest"
r
right. Can you see in package-lock?
canwe get on a debugging call?
right now
r
16.3.4
r
the debug boolean was addded in 16.4.0
are you open to discussing this on a call rigth now?
r
Yeah sure
r
ok. Ill send over a link
Join Zoom Meeting https://us06web.zoom.us/j/5466985678?pwd=bU1UVVJiRnRCMDBrNTBLNlhXWCtkUT09 Meeting ID: 546 698 5678 Passcode: e8Jyam --- One tap mobile +13052241968,,5466985678#,,,,*189222# US +13092053325,,5466985678#,,,,*189222# US --- Dial by your location • +1 305 224 1968 US • +1 309 205 3325 US • +1 312 626 6799 US (Chicago) • +1 346 248 7799 US (Houston) • +1 360 209 5623 US • +1 386 347 5053 US • +1 507 473 4847 US • +1 564 217 2000 US • +1 646 931 3860 US • +1 669 444 9171 US • +1 669 900 6833 US (San Jose) • +1 689 278 1000 US • +1 719 359 4580 US • +1 929 205 6099 US (New York) • +1 253 205 0468 US • +1 253 215 8782 US (Tacoma) • +1 301 715 8592 US (Washington DC) Meeting ID: 546 698 5678 Passcode: 189222 Find your local number: https://us06web.zoom.us/u/kcoq4YKhGU
r
Getting the userId. Thanks.
4 Views