supertokens / supertokens-golang

GoLang SDK for SuperTokens
https://supertokens.com
Other
115 stars 34 forks source link

superTokensInstance should not be package-level #420

Open Leskodamus opened 2 months ago

Leskodamus commented 2 months ago

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.

rishabhpoddar commented 2 months ago

Agreed. Until this is done though, you can call the supertokens.ResetForTest() function during tests so that you can call init multiple times.