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
support-questions
  • r

    rp

    11/23/2021, 9:49 AM
    Cool. Give me a few mins. Will give you a sample impl
  • r

    rp

    11/23/2021, 9:50 AM
    And which version of the SDK?
  • j

    Jim Gambit

    11/23/2021, 9:50 AM
    supertokens-python = "^0.2.0"
  • j

    Jim Gambit

    11/23/2021, 9:53 AM
    Thanks a lot ๐Ÿ™‚ Also, is there a way to get the client IP? I believe in normal starlette Request object you can get it via print(request.client.host) but in SuperTokens' FastApiRequest object there is no client attribute
  • k

    kakashi_44

    11/23/2021, 10:16 AM
    hey @User , try the following sample code snippet
  • k

    kakashi_44

    11/23/2021, 10:17 AM
    python
    from supertokens_python.recipe.session.asyncio import revoke_all_sessions_for_user
    from supertokens_python import init
    from supertokens_python.recipe import session
    from typing import TYPE_CHECKING
    if TYPE_CHECKING:
        from supertokens_python.framework.fastapi.fastapi_request import FastApiRequest
        from typing import Union
    
    
    def override_session_functions(original_implementation):
        original_create_new_session = original_implementation.create_new_session
    
        async def create_new_session(request: FastApiRequest, user_id: str, jwt_payload: Union[dict, None] = None, session_data: Union[dict, None] = None):
            await revoke_all_sessions_for_user(user_id)
            # to access original request object, just do:
            original_request = request.original
            return await original_create_new_session(request, user_id, jwt_payload, session_data)
        original_implementation.create_new_session = create_new_session
        return original_implementation
    
    init({
        'app_info': {...},
        'supertokens': {...},
        'framework': '...',
        'recipe_list': [
            session.init({
                'override': {
                    'functions': override_session_functions
                }
            })
        ]
    })
  • k

    kakashi_44

    11/23/2021, 10:18 AM
    You can get the original fastapi request doing
    original_request = request.original
    as shown in the above code snippet
  • j

    Jim Gambit

    11/23/2021, 10:18 AM
    Thanks! ๐Ÿ™‚ I will try this out
  • k

    kakashi_44

    11/23/2021, 10:19 AM
    Cool ๐Ÿ‘ ๐Ÿ™‚
  • r

    rp

    11/23/2021, 10:52 AM
    @User , maybe you missed this: > One point to be aware is that unless you enable access token blacklisting, the existing logged in device will not get logged out until it refreshes its session - which will happen depending on the configured access token's lifetime (which is 1 hour by default) Would this be OK?
  • j

    Jim Gambit

    11/23/2021, 11:32 AM
    Yes, I did this as well and ran a couple of scenarios in our code base. The code snippet is working for us and it seems like the easiest and simplest solution for our use case. Thanks a ton for the help ๐Ÿ™‚
  • j

    Jim Gambit

    11/23/2021, 11:38 AM
    Just confirming, access token blacklisting can be enabled through docker environment variable, right?
  • r

    rp

    11/23/2021, 12:46 PM
    @User yes. It can be enabled that way. However, the downside to this is that it will cause a db lookup for every session verificaiton request which might affect performance.
  • r

    rp

    11/23/2021, 12:46 PM
    You may want to add your own solution of blacklisting by using a cache instead.
  • r

    rp

    11/23/2021, 12:47 PM
    but, try and it out and see how it goes - maybe it would be fine for you ๐Ÿ™‚
  • j

    jj_

    11/24/2021, 3:49 AM
    Is there a way to customize errors on sign in or signup page.
  • r

    rp

    11/24/2021, 4:45 AM
    Hey @User , which recipe are you using?
  • j

    jj_

    11/24/2021, 4:52 AM
    email-password
  • r

    rp

    11/24/2021, 4:53 AM
    And the error is a validation error for a specific field in the form? Or a more generic error?
  • j

    jj_

    11/24/2021, 4:56 AM
    specify as in if the user session already exists on another device then , we need a custom error to be displayed .
  • r

    rp

    11/24/2021, 5:30 AM
    @User , give us sometime, I will reply to you ASAP.
  • r

    rp

    11/24/2021, 5:37 AM
    @User you need to do the following: - In your override function, you can throw a custom error which get's propogated to your app's error handler. - In your app's error handler, you should detect that custom error and send a JSON, 200 reply to the frontend with the following structure:
    {status: "GENERAL_ERROR", message: "some custom error message"}
    Then in the UI, you will see "some custom error message"
  • r

    rp

    11/24/2021, 5:37 AM
    Are you using node?
  • r

    rp

    11/24/2021, 5:40 AM
    If you are, here is an example code snippet: First we throw a custom error in the overrided function:
    js
    Session.init({
            override: {
                functions: (oI) => {
                    return {
                        ...oI,
                        createNewSession: async function (_) {
                            // for example purposes, we are always throwing an error...
                            throw new Error("create session failed");
                        }
                    }
                }
            }
        })
    Then we can catch that in the app's error handler like this:
    js
    app.use((err, req, res, next) => {
        if (err.message === "create session failed") {
            res.setHeader('Content-Type', 'application/json');
            res.end(JSON.stringify({
                status: "GENERAL_ERROR",
                message: "Unable to make new session" // this message will get displayed to the user.
            }));
            return;
        }
        res.status(500).send("Internal error: " + err.message);
    });
    n
    • 2
    • 30
  • z

    ZeferiniX

    11/24/2021, 6:25 PM
    I'm trying to migrate
    supertokens-node
    from v7 to v8 and just found out
    getSessionData
    has been deprecated. Would probably be helpful if that's logged somehow. That aside, I'm trying to migrate to the recommended function which is
    getSessionInformation
    and the type reference from the docs doesn't match what's usable in the code.
  • z

    ZeferiniX

    11/24/2021, 6:25 PM
    from the docs
  • z

    ZeferiniX

    11/24/2021, 6:26 PM
    inspected the codebase and seems like as the code says
  • r

    rp

    11/24/2021, 6:27 PM
    these are the contents of SessionInformation
  • z

    ZeferiniX

    11/24/2021, 6:27 PM
    now my question is, where does
    sessionData
    actually go?
  • r

    rp

    11/24/2021, 6:27 PM
    So sessionData is stored in the db against the sessionhandle
Powered by Linen
Title
r

rp

11/24/2021, 6:27 PM
So sessionData is stored in the db against the sessionhandle
View count: 2