Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow referencing of overrided function by original implementation #199

Closed
10 tasks done
rishabhpoddar opened this issue Oct 26, 2021 · 1 comment
Closed
10 tasks done
Assignees
Labels
bug Something isn't working

Comments

@rishabhpoddar
Copy link
Member

rishabhpoddar commented Oct 26, 2021

Right now, if the original implementation has a function A, which calls another function B, and then if the user overrides function B and then if A is called, it does not call user's B, but instead calls the original B.

This would break logic in many cases. At the moment, this affects the JS SDKs and the golang SDK.

JS fix (node, auth-react, website, react-native SDKs):

The trick here is to not use arrow functions for any of the functions in the recipe / api interface implementaiton. This allows the this variable to point to functions in the new object returned by the user solving this problem.

Then when the user calls the original function, they must bind it to this object as shown below.

Small example:

class Aa {
    m: string = "m"

    someFunc = function () {
        console.log("original someFunc", this.m)
        this.someOtherFunc()
    }

    someOtherFunc = function () {
        console.log("original someOtherFunc")
    }
}

let oI = new Aa();

let a = {
    ...oI,
    someFunc: function () {
        oI.someFunc.bind(this)();
    },
    someOtherFunc: function () {
        console.log("overrided a someOtherFunc")
    }
}

let b = {
    ...a,
    someFunc: function () {
        a.someFunc.bind(this)();
    },
    someOtherFunc: function () {
        console.log("overrided b someOtherFunc")
    }
}

b.someFunc();

Golang fix:

The trick here is to make all the recipe / api interface functions pointers to functions, and then get the user to change the pointer value when overriding it like so:

package main

import (
	"fmt"

	"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
)

func main() {
	sI := func(email, password string) (epmodels.SignInResponse, error) {
		fmt.Println("signin")
		return epmodels.SignInResponse{}, nil
	}

	sU := func(email, password string) (epmodels.SignUpResponse, error) {
		fmt.Println("signup")
		sI("", "")
		return epmodels.SignUpResponse{}, nil
	}

	oI := epmodels.RecipeInterface{
		SignUp: &sU,
		SignIn: &sI,
	}

	originalSignIn := (*oI.SignIn)
	*(oI.SignIn) = func(email, password string) (epmodels.SignInResponse, error) {
		originalSignIn(email, password)
		fmt.Println("signin haha")
		return epmodels.SignInResponse{}, nil
	}

	(*oI.SignUp)("", "")
}
  • Here, SignUp is a pointer to the actual sign up function, and SignIn is a pointer to the actual sign in function.
  • This will be a breaking change and will require changes to the docs everywhere.

TODO:


Another important issue:

Here is the situation:

Recipe1 = {
   someFunc: () => {this.someFunc2()},
   someFunc2: () => {...}
}

recipe1 = new Recipe1()

Recipe2 = {
   someFunc: () => recipe1.someFunc(),
   someFunc3: () => recipe1.someFunc2(), // 
}

Now if a user creates an instance of Recipe2, and then overrides someFunc3 to do what they like (and semantically someFunc3 is similar to someFunc2), calling recipe2.someFunc() will not have the same expected behaviour.

@rishabhpoddar rishabhpoddar self-assigned this Oct 26, 2021
@rishabhpoddar rishabhpoddar added the bug Something isn't working label Oct 26, 2021
@rishabhpoddar rishabhpoddar mentioned this issue Oct 27, 2021
8 tasks
rishabhpoddar pushed a commit to supertokens/supertokens-auth-react that referenced this issue Oct 28, 2021
rishabhpoddar pushed a commit to supertokens/supertokens-website that referenced this issue Oct 28, 2021
rishabhpoddar pushed a commit to supertokens/supertokens-react-native that referenced this issue Oct 28, 2021
@rishabhpoddar
Copy link
Member Author

Issue two fix for nodejs: 8a4afbb

rishabhpoddar pushed a commit to supertokens/supertokens-golang that referenced this issue Oct 30, 2021
rishabhpoddar pushed a commit to supertokens/supertokens-auth-react that referenced this issue Oct 30, 2021
This was referenced Oct 31, 2021
rishabhpoddar pushed a commit to supertokens/supertokens-auth-react that referenced this issue Nov 5, 2021
rishabhpoddar pushed a commit to supertokens/supertokens-react-native that referenced this issue Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant