supertokens-golang icon indicating copy to clipboard operation
supertokens-golang copied to clipboard

superTokensInstance should not be package-level

Open Leskodamus opened this issue 1 year ago • 1 comments

The current implementation defines and uses a package-level variable for the *superTokens instance which is not a great if anyone would like to have multiple instances or for testing (in parallel).

Instead it should be handled in encapsulation, for example by changing the func supertokensInit(config TypeInput) error {} and func Init(config TypeInput) error {} functions to return a *superTokens object and by making type superTokens struct {} a public struct:

type SuperTokens struct {}

func supertokensInit(config TypeInput) (error, *SuperTokens) {}

func Init(config TypeInput) (error, *SuperTokens) {}

Maybe Init(config TypeInput) should even be renamed to NewSuperTokens or NewSuperTokensInstnace.

To me this implementation is not very idiomatic Go and looks more like JavaScript. This would probably a bigger change affecting various parts of the SDK.

Edit: After using the SDK a bit more, I've realized that almost every type has a singleton instance. This is not very flexible and definitely not a good design pattern. Basically all singletons should be removed and refactored with more structure in mind.

Leskodamus avatar Jul 11 '24 07:07 Leskodamus