https://supertokens.com/ logo
Docs
Join the conversationJoin Discord
Channels
community
contributing
general
github-activity
info
introductions
new-releases
random
security
support-questions
welcome-0xdelusion
welcome-aj-ya
welcome-aleksandrc
welcome-alpinjs
welcome-amberlamps1
welcome-andrew-rodriguez
welcome-ankit-choudhary
welcome-anthony-stod-custodio
welcome-call-in
welcome-chwalbox
welcome-claybiokiller
welcome-co7e
welcome-cosmoecwsa
welcome-devdag
welcome-dinso
welcome-drebotelho
welcome-elio
welcome-ernest
welcome-foxbarrington
welcome-fromscratch
welcome-galto4ir
welcome-goetzum
welcome-hay-kot
welcome-himanshu-kukreja
welcome-hossambarakat
welcome-ichikawakazuto
welcome-jahir9991
welcome-jamesl
welcome-jerry123424
welcome-john-oliver
welcome-jonas-alexanderson
welcome-jxyz
welcome-kelvinwop
welcome-kraz
welcome-lancekey
welcome-leoo
welcome-lukeacollins
welcome-m-j-mon
welcome-malik-khoja
welcome-marco
welcome-mardadi
welcome-meshguy
welcome-metamorph
welcome-mike-tectu
welcome-mirzok
welcome-mozomig
welcome-naberyou66_
welcome-nacer
welcome-namratha
welcome-naveenkumar
welcome-nightlight
welcome-nischith
welcome-notankit
welcome-olawumi
welcome-pavan-kumar-reddy-n
welcome-pineappaul
welcome-poothebear
welcome-rick
welcome-samuel-qosenergy
welcome-samuelstroschein
welcome-shubhamgoel23
welcome-shubhamkaushal
welcome-sidebar
welcome-surajsli
welcome-suyash_
welcome-syntaxerror
welcome-tauno
welcome-tauno
welcome-tawnoz
welcome-teclali
welcome-tls
welcome-turbosepp
welcome-vikram_shadow
welcome-yann
Powered by Linen
general
  • r

    rp

    03/27/2020, 8:55 PM
    that's how the browser works.
  • s

    Sun Walker

    03/27/2020, 8:55 PM
    Oh,
  • r

    rp

    03/27/2020, 8:55 PM
    it doesn't send expired cookies to the API.
  • r

    rp

    03/27/2020, 8:55 PM
    now we could set the cookie expiry to be larger than the access token
  • s

    Sun Walker

    03/27/2020, 8:56 PM
    oh so browser can see expiry of cookie
  • r

    rp

    03/27/2020, 8:56 PM
    access token's expiry, but this would not provide much benefit cause we would still want to enforce the fact that we get a new refresh token for each /refreshSession call.
  • r

    rp

    03/27/2020, 8:56 PM
    yes. browser has complete access to the cookie (not the JS running on your page), but the browser itself.
  • r

    rp

    03/27/2020, 8:57 PM
    > at one point will we be able to replace all of this with redis, as it's faster or is that less secure? We will build redis support soon. But if you are using JWTs, then it would add no performance benefit cause even now, most getSession calls don't even need a network call to anything (not even supertokens core).
  • s

    Sun Walker

    03/27/2020, 8:58 PM
    How comes they dont always call db?
  • r

    rp

    03/27/2020, 8:58 PM
    cause that's how JWT verification works
  • r

    rp

    03/27/2020, 8:59 PM
    you have the public key in the node process (in memory). And when you get a JWT, you verify the signature using that public key. If it checks out, and the JWT hasn't expired, and the anti-csrf verification is done, then your good to go!
  • r

    rp

    03/27/2020, 8:59 PM
    if any of those fail, then you query the ST core and do whatever that says.
  • s

    Sun Walker

    03/27/2020, 9:00 PM
    this is what I've got
    export const secureRoutesMiddleware = async (req: Request, res: Response, next: NextFunction) => {
      try {
        const session: any = await getSession(req, res, true);
        req.session = session;
        return next();
      } catch (err) {
        const authError = Error.isErrorFromAuth(err);
    
        if (authError && err.errType === Error.UNAUTHORISED) {
          return res.status(440).send({ mustLogin: true, message: 'Unauthorised. Please Login.' });
        }
        if (authError && err.errType !== Error.GENERAL_ERROR) {
          return res
            .status(440)
            .send({ mustRefresh: true, message: 'Session Expired. Please Refresh Session.' });
        }
        throw createHttpError(500, { err });
      }
    };
  • s

    Sun Walker

    03/27/2020, 9:00 PM
    Doesn't get session take in the req and res and call db, to get the session and check if jwt in session is valid?
  • r

    rp

    03/27/2020, 9:00 PM
    Yea. This is what we use for our website too
  • r

    rp

    03/27/2020, 9:00 PM
    Somthing very similar
  • r

    rp

    03/27/2020, 9:01 PM
    however, you may not want to hard code the anti-csrf 'true' value there. As some APIs (when you have a website), would not require CSRF
  • r

    rp

    03/27/2020, 9:02 PM
    so getSession takes the req, gets the JWT from it from cookies Then it takes the JWT and verifies the signature using the public key it already has (which is gets from the core when u start the node process). The JWT contains the userId - so no db call there eigher
  • r

    rp

    03/27/2020, 9:02 PM
    either*
  • s

    Sun Walker

    03/27/2020, 9:03 PM
    Ahhhhh
  • s

    Sun Walker

    03/27/2020, 9:03 PM
    from the core? what's that
  • r

    rp

    03/27/2020, 9:03 PM
    the core is the ST service you are running.
  • s

    Sun Walker

    03/27/2020, 9:03 PM
    Got ya
  • r

    rp

    03/27/2020, 9:03 PM
    I call it the ST core. haha
  • s

    Sun Walker

    03/27/2020, 9:04 PM
    I like that name lol
  • r

    rp

    03/27/2020, 9:04 PM
    haha
  • r

    rp

    03/27/2020, 9:04 PM
    thanks
  • s

    Sun Walker

    03/27/2020, 9:04 PM
    also another important one, I've been getting a response of 'Session Revoked, Please Login' sometimes when I try to /refreshSession with correct details which isnt supposed to happen. This is my function, is there anything wrong with it?
    export const attemptRefreshSession = async (req: Request, res: Response) => {
      try {
        const session: Session = await refreshSession(req, res);
    
        if (!session) {
          throw new Unauthorized();
        }
        return env !== 'development' ? true : developmentCookieResponse(res);
      } catch (err) {
        const authError = Error.isErrorFromAuth(err);
    
        if (authError && err.errType === Error.UNAUTHORISED) {
          return res.status(440).send({ mustLogin: true, message: 'Unauthorized. Please Login.' });
        }
        if (authError && err.errType !== Error.GENERAL_ERROR) {
          console.log(err, err.errType); // TODO remove
          const sessionHandle = err?.err?.sessionHandle;
          const successfulRevoke = await revokeSessionUsingSessionHandle(sessionHandle);
          return res
            .status(440)
            .send({ mustLogin: true, message: 'Session Revoked. Please Login.', successfulRevoke });
        }
        throw createHttpError(500, { err });
      }
    };
  • s

    Sun Walker

    03/27/2020, 9:05 PM
    was console logging to try to debug
  • r

    rp

    03/27/2020, 9:06 PM
    So maybe that's cause you have been using the old refresh token to make an API call after using a new access token?
Powered by Linen
Title
r

rp

03/27/2020, 9:06 PM
So maybe that's cause you have been using the old refresh token to make an API call after using a new access token?
View count: 2