How to get the token for verifying the user Answered

hello everyone how I can get the token for the user connected and sent it to the my backend to verify if the user valid

Comments

6 comments

  • Comment author
    Tyler Bell

    Does this help?

    https://docs.memberstack.com/hc/en-us/articles/7253255689755-Using-Permissions-and-Token-Verification-to-Secure-Your-Site#heading-3

    You can ignore the permissions and just focus on the parts that mention the token.

    0
  • Comment author
    khaoula EZ-Zaoui

    yes it help but I always getting null Token values ? this is my code

    if (parsedUrl.pathname === "/get-token") {
    try {
    console.log("req.headers:---->", req.headers);
    console.log("req.headers.cookie:---->", req.headers.cookie);
    // console.log("req.headers.get(cookie)----> ", req.headers.get("cookie"));
    const cookieString =req.headers.cookie; // req.headers.get("cookie") ; //
    console.log("cookieString:", cookieString);
    const cookieName = "_ms-mid";
    const token = getCookie(cookieString, cookieName);

    console.log("Access Token:", token); res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify({ token })); } catch (error) { console.error(error); res.statusCode = 401; res.end("Unauthorized"); }

    }

    exemple of returned data :

    0
  • Comment author
    Jayess

    Hello Khaoula 👋

    Thanks for providing the code 😀 I’m not a developer and I asked ChatGPT to see if they can help give us some clues.

    “Based on the code you provided, it seems that you are trying to extract a cookie value from the request headers and send it back as a JSON response. However, you are always getting a null value for the token.
    There could be a few reasons why this is happening. Here are some things you can try to debug the issue:

    1. Check the cookie name: Make sure that the name of the cookie you are trying to extract matches the name of the cookie that is being sent in the request. In this case, you are trying to extract a cookie named “_ms-mid”. You can verify this by checking the “cookieString” variable in your console logs.
    2. Check the format of the cookie string: The “cookieString” variable should contain a string in the format “cookieName=cookieValue”. Make sure that the cookie string is being parsed correctly by the “getCookie” function.
    3. Check if the cookie is being sent in the request headers: Make sure that the cookie is being sent in the request headers by checking the “req.headers.cookie” value in your console logs.
    4. Check if the cookie is being sent with the correct path and domain: Cookies can be set with specific paths and domains. Make sure that the cookie is being sent with the correct path and domain that matches the current request.

    You can try these steps to debug the issue and find out why you are always getting a null value for the token.”

    Josh Lopez Nicolas Scott If there’s anything you guys notice 😀

    0
  • Comment author
    Nicolas Scott

    Yes, I wrote this code. Token is probably null because of this line:

    const token = getCookie(cookieString, cookieName);

    It looks like the getCookie function that was meant to go with this code is missing.

    You'll need to replace getCookie() with the actual token verification method from the admin SDK and pass in the token.

    Like below:

    import memberstackAdmin from "@memberstack/admin";
    const memberstack = memberstackAdmin.init("")
    const cookieName = "_ms-mid";
    const token = <insert framework specific code here to retrieve cookies from headers"
    const tokenVerified = await memberstack.verifyToken({ token: token });

    I'll update the docs. Apologies for the confusion!

    0
  • Comment author
    Jayess

    Thanks Nicolas 🙏 We appreciate all the work you do!

    0
  • Comment author
    khaoula EZ-Zaoui

    Jayess Nicolas Scott thank you very much for your answer 🙏

    0

Please sign in to leave a comment.