https://supertokens.com/ logo
user context in golang
a

aV

05/12/2023, 7:23 AM
@rp i am trying to inject an id into userctx as soon as user signs up and trying to access the id in session.init to add it to access token payload. But i am getting value of id null
12:43PM DBG supertokens.go:174 > accessTokenPayload={"iss":"http://localhost:8080/auth"}
12:43PM DBG supertokens.go:179 > aid=null
12:43PM DBG supertokens.go:180 > accessTokenPayload={"aid":null,"iss":"http://localhost:8080/auth"}
session.init function
go
session.Init(&sessmodels.TypeInput{

                Override: &sessmodels.OverrideStruct{
                    Functions: func(originalImplementation sessmodels.RecipeInterface) sessmodels.RecipeInterface {

                        orig~~in~~alCreateNewSession := *originalImplementation.CreateNewSession

                        (*originalImplementation.CreateNewSession) = func(userID string, accessTokenPayload, sessionDataInDatabase map[string]interface{}, disableAntiCsrf *bool, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) {


                            if accessTokenPayload == nil {
                                accessTokenPayload = map[string]interface{}{}
                            }
                            log.Debug().Any("accessTokenPayload", accessTokenPayload).Msg("")

                            aid := (*userContext)[globals.SessionAccountIDKey]

                            accessTokenPayload[globals.SessionAccountIDKey] = aid
                            log.Debug().Any("aid", aid).Msg("")
                            log.Debug().Any("accessTokenPayload", accessTokenPayload).Msg("")

                            return originalCreateNewSession(userID, accessTokenPayload, sessionDataInDatabase, disableAntiCsrf, userContext)
                        }

                        return originalImplementation
                    },
                },
            }),
go
if response.OK != nil {
                                log.Debug().Msg("response.Ok inside")
                                        isNewUser := response.OK.CreatedNewUser
                                userID := response.OK.User.ID

                                if isNewUser {
                                                account, err := s.Service.Account.Create(context.Background(), ent.CreateAccountInput{}, response.OK.User.ID)
                                    if err != nil {
                    
                                    log.Error().Err(err).Msg("unable to create account")

                                        err := supertokens.DeleteUser(userID)
                                        if err != nil {
                                            log.Error().Err(err).Msg("unable to delete user")
                                        }
                                        log.Info().Msg("supertokens user deleted")
                                    }

                                    (*userContext)[globals.SessionAccountIDKey] = strconv.Itoa(account.ID)

                    }
r

rp

05/12/2023, 7:25 AM
@sattvikc can help here
s

sattvikc

05/12/2023, 7:30 AM
@aV are you looking for the place where you want to add the property?
or have you done it already?
a

aV

05/12/2023, 7:33 AM
@User what property? I am trying to create an account on server as soon as a new user signs up. I am trying to inject the account id into access token payload. For now I am passing the account id into the userctx and then accessing it in session.init function to change the access token payload
s

sattvikc

05/12/2023, 7:35 AM
I see you are trying to access the account id in the create session override. Can you share the code where you are passing the account id.
a

aV

05/12/2023, 7:35 AM
This
s

sattvikc

05/12/2023, 7:37 AM
ok so this code is executed after create session, that's why you don't see it in the user context
since you are trying to inject it into the access token payload, would it be possible to move this code to the create session override itself ?
s

sattvikc

05/12/2023, 7:42 AM
for the sake of clarity, can you share the entire init code ?
a

aV

05/12/2023, 7:43 AM
Isn't passwordless.Init called first and then session.Init
i set account id in userctx in passwordless.init and trying to access ot in session.init
s

sattvikc

05/12/2023, 7:46 AM
it is not related to the passwordless init
could you share the entire supertokens.Init code for the clarity ?
s

sattvikc

05/12/2023, 7:54 AM
yea got it, so you have overridden consumeCodePOST API, so the place you are adding account id to the user context is not the right place to do
reason being, that code executes later on
a

aV

05/12/2023, 7:54 AM
then where?
s

sattvikc

05/12/2023, 7:55 AM
you should instead override the function
one moment, I'll tell you which one
ConsumeCode
https://supertokens.com/docs/passwordless/advanced-customizations/user-context - in fact this page contains the ConsumeCode override only. You have overridden API instead of the Recipe function
a

aV

05/12/2023, 8:02 AM
let me check
Yes now working fine thanks
s

sattvikc

05/12/2023, 8:09 AM
awesome