custom fields for passwordless
# support-questions
s
Hi , I'm using self-hosted version and using react with typescript for frontend. I implemented custom fields to get some additional informations from user while using passwordless recipe. Is there a way to pass custom fields via supertokens-web-js
createCode
function? Currently it doesn't allow me to pass custom fields.
r
hey @syntaxerror yes there is - you can use the pre API hook to add custom properties to the request body.
s
const body = JSON.parse(requestInit.body) body["myField"] = "myFieldValue" requestInit.body = JSON.stringify(body) Is that the only valid case for that purpose?
Is there any helper or utils class for that purpose ?
r
there is no helper for this.. but the way you are doing it is fine
s
@rp thanks, btw I want to know something on backend side, so I can send custom request like that but should I handle custom request fields via userContext on backend side? In golang function interface as following; CreateCode *func(email *string, phoneNumber *string, userInputCode *string, userContext supertokens.UserContext) (CreateCodeResponse, error) So I can't add any custom field for that purpose. I am trying to decode request from user context again and getting field via defaultContext := (*userContext)["_default"] and iterating over on it to find request context etc... Is there any valid and safe way to handle that?
r
Using
getting field via defaultContext := (*userContext)["_default"]
is the right way of doing it. We plan on adding helper functions to make sense of the userContext soon.
s
ok, great. currently while implementing it it looks a little weird. thanks for confirming my approaches.
r
Yea. I agree that it's weird. I would suggest that you make a helper function somewhere which returns a typed HttpRequest and takes in the userContext type
and use that helper function in your actual overrides.
s
yeah, I did it in that way . thanks for your great support @rp
n
hii def override_passwordless_apis(original_implementation: APIInterface): original_consume_code_post = original_implementation.consume_code_post async def consume_code_post(pre_auth_session_id: str, user_input_code: Union[str, None], device_id: Union[str, None], link_code: Union[str, None], api_options: APIOptions, user_context: Dict[str, Any]): # First we call the original implementation of consume_code_post. response = await original_consume_code_post(pre_auth_session_id, user_input_code, device_id, link_code, api_options, user_context) # Post sign up response, we check if it was successful if isinstance(response, ConsumeCodePostOkResult): _ = response.user.user_id __ = response.user.email _ = response.user.phone_number print(response.user.user_id) print(user_context) # print(user_context['_default']['request'].body.get('shweta')) request = user_context['_default']['request'] print(request) we want custom field for password less how to read custom field using python
Our request body for consume code from the front-end application (react js) is as below: { deviceId: deviceDetail.deviceId, preAuthSessionId: deviceDetail.preAuthSessionId, userInputCode: otp, custom_field: "success", }; "custon_field" is essencial for us to decide the role of that user
r
can you please reformat the question so it's easier to read? I suggest using code snippet formatting
n
Hi , I'm using self-hosted version and using react with javascript for frontend and python django for backend. I implemented custom fields to get some additional informations from user while using passwordless recipe. Is there a way to pass custom fields via supertokens-web-js ConsumeCode function? Currently it doesn't allow me to pass custom fields. i want to know for which parameter should i assign the customvalue so that i can read in the backend consumecode override function
r
You can use the preAPIHook function in supertokens-web-js to modify the request object to add the custom field to it
n
Below is the request body we are sending for consumecode: { deviceId: "dfdsfkdshgj98658735843857348", preAuthSessionId: "fdsfsdfdsfds435367836878", userInputCode: "123456", custom_field: "success" }
r
right. So it you are passing in a custom field then. So whats the issue?
n
how can i read this reqestbody or custom_field in the backend override_passwordless_apis---- consume_code_post
r
the input has api_options in which there should be a request object
using which you can read the json body and get the custom prop
n
def override_passwordless_apis(original_implementation: APIInterface): original_consume_code_post = original_implementation.consume_code_post async def consume_code_post(pre_auth_session_id: str, user_input_code: Union[str, None], device_id: Union[str, None], link_code: Union[str, None], api_options: APIOptions, user_context: Dict[str, Any]): # First we call the original implementation of consume_code_post. response = await original_consume_code_post(pre_auth_session_id, user_input_code, device_id, link_code, api_options, usercontext) # Post sign up response, we check if it was successful if isinstance(response, ConsumeCodePostOkResult): = response.user.userid = response.user.email = response.user.phone_number print(response.user.user_id) print(user_context)
how to access that request object. can u pls give syntax for it
r
please look up our SDK reference
@Namratha try this
Copy code
python
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import passwordless
from supertokens_python.recipe.passwordless.interfaces import APIInterface, APIOptions, ConsumeCodePostOkResult
from typing import Union, Dict, Any

def override_passwordless_apis(original_implementation: APIInterface):
    original_consume_code_post = original_implementation.consume_code_post

    async def consume_code_post(pre_auth_session_id: str,
                                user_input_code: Union[str, None],
                                device_id: Union[str, None],
                                link_code: Union[str, None],
                                api_options: APIOptions,
                                user_context: Dict[str, Any]):
        # Access the custom_field parameter from the user_context dictionary
        custom_field = await api_options.request.json().get("custom_field")

        # First we call the original implementation of consume_code_post.
        response = await original_consume_code_post(pre_auth_session_id, user_input_code, device_id, link_code, api_options, user_context)

        # Post sign up response, we check if it was successful
        if isinstance(response, ConsumeCodePostOkResult):
            user = response.user
            if response.created_new_user:
                # TODO: post sign up logic
            else:
                # TODO: post sign in logic

        return response

    # Update the original implementation with the new consume_code_post function
    original_implementation.consume_code_post = consume_code_post
    return original_implementation

init(InputAppInfo(...))
passwordless.init({
    "override": {
        "apis": override_passwordless_apis
    }
})
essentially we use:
Copy code
custom_field = await api_options.request.json().get("custom_field")
To get the custom_field. Im not 100% sure if the above is full accurate, but you get the idea..
n
Thanks a lot @rp
4 Views