https://supertokens.com/ logo
Title
c

chris_st

11/26/2022, 2:00 PM
I apologize if this is documented somewhere -- I didn't find it. I'd like to be able to turn "Sign Up" on and off -- for example, to not allow sign up during a limited beta period. Is this possible, specifically with passwordless link/OTP? The default behavior creates a new user if you "Sign In" with a email address that hasn't been seen before. Obviously, this gate has to be on the server side -- can't trust client-side 🙂 Thanks!
r

rp

11/26/2022, 2:21 PM
hey @chris_st
you can do this. Override the generateCodePOST API on the backend, and from the input, get the info about what email was used, and then check if the email already exists in supertokens. If it does, then call the original implement, else return a GENERAL_ERROR status to the frontend saying something like sign up is disabled.
If you tell me which backend SDK is being used, i can show you a code snippet
c

chris_st

11/26/2022, 2:34 PM
@rp Thanks! I'm using Go. Probably should have mentioned that 🙂
Is it maybe "createCodePOST" instead of "generateCodePOST"?
r

rp

11/26/2022, 2:41 PM
yea
you can do something like this in passwordless.init:
go
import (
    "github.com/supertokens/supertokens-golang/recipe/passwordless"
    "github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
    "github.com/supertokens/supertokens-golang/supertokens"
)

func main() {
    passwordless.Init(plessmodels.TypeInput{
        Override: &plessmodels.OverrideStruct{
            APIs: func(originalImplementation plessmodels.APIInterface) plessmodels.APIInterface {
                ogCreateCodePOST := *originalImplementation.CreateCodePOST
                (*originalImplementation.CreateCodePOST) = func(email, phoneNumber *string, options plessmodels.APIOptions, userContext supertokens.UserContext) (plessmodels.CreateCodePOSTResponse, error) {
                    user, err := passwordless.GetUserByEmail(*email)
                    if err != nil {
                        return plessmodels.CreateCodePOSTResponse{}, err
                    }
                    if user != nil {
                        // this means that the user with this email already exist, so it's a sign in
                        // so we allow it
                        return ogCreateCodePOST(email, phoneNumber, options, userContext)
                    }
                    // this means that the user does not exist and is trying to sign up, which we
                    // disallow
                    return plessmodels.CreateCodePOSTResponse{
                        GeneralError: &supertokens.GeneralErrorResponse{
                            Message: "Sign up is disabled",
                        },
                    }, nil
                }
                return originalImplementation
            },
        },
    })
}
c

chris_st

11/26/2022, 2:49 PM
THANK YOU! That works perfectly.
For the record: I don't think I could have figured out that piece of code on my own... this is definitely going to be a documentation challenge on your side.
r

rp

11/26/2022, 2:57 PM
Yeaaa I agree! We do need to write more docs for how to do customisations
i

ITEnthusiasm

11/26/2022, 3:27 PM
This is a really great question. I was wondering something like this. Lol. Super helpful.
Are there contributors for the docs?
r

rp

11/26/2022, 6:00 PM
@ITEnthusiasm we welcome docs contributions. But we are very particular about how our docs are to be written, so expect there to be a lot of back and forth on if you make a PR 🙂