Skip to content

Commit 57ed0a1

Browse files
committed
fix: ev postinit callback
1 parent 944aa1b commit 57ed0a1

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

recipe/emailverification/recipe.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121

2222
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
2323
"github.com/supertokens/supertokens-golang/recipe/emailverification/api"
24+
"github.com/supertokens/supertokens-golang/recipe/emailverification/claims"
2425
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
26+
"github.com/supertokens/supertokens-golang/recipe/session"
2527
"github.com/supertokens/supertokens-golang/supertokens"
2628
)
2729

@@ -120,6 +122,21 @@ func recipeInit(config evmodels.TypeInput) supertokens.Recipe {
120122
return nil, err
121123
}
122124
singletonInstance = &recipe
125+
126+
supertokens.AddPostInitCallback(func() {
127+
sessionRecipe, err := session.GetRecipeInstanceOrThrowError()
128+
if err != nil {
129+
return
130+
}
131+
132+
sessionRecipe.AddClaimFromOtherRecipe(claims.EmailVerificationClaim.TypeSessionClaim)
133+
134+
if config.Mode == "REQUIRED" {
135+
sessionRecipe.AddClaimValidatorFromOtherRecipe(
136+
*claims.EmailVerificationClaim.Validators.IsVerified(nil),
137+
)
138+
}
139+
})
123140
return &singletonInstance.RecipeModule, nil
124141
}
125142
return nil, errors.New("Emailverification recipe has already been initialised. Please check your code for bugs.")

recipe/session/recipe.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func getRecipeInstanceOrThrowError() (*Recipe, error) {
102102
return nil, defaultErrors.New("Initialisation not done. Did you forget to call the init function?")
103103
}
104104

105+
func GetRecipeInstanceOrThrowError() (*Recipe, error) {
106+
return getRecipeInstanceOrThrowError()
107+
}
108+
105109
func recipeInit(config *sessmodels.TypeInput) supertokens.Recipe {
106110
return func(appInfo supertokens.NormalisedAppinfo, onSuperTokensAPIError func(err error, req *http.Request, res http.ResponseWriter)) (*supertokens.RecipeModule, error) {
107111
if singletonInstance == nil {
@@ -197,7 +201,7 @@ func (r *Recipe) handleError(err error, req *http.Request, res http.ResponseWrit
197201
}
198202

199203
// Claim functions
200-
func (r *Recipe) addClaimFromOtherRecipe(claim *claims.TypeSessionClaim) error {
204+
func (r *Recipe) AddClaimFromOtherRecipe(claim *claims.TypeSessionClaim) error {
201205
for _, existingClaim := range r.claimsAddedByOtherRecipes {
202206
if claim.Key == existingClaim.Key {
203207
return defaultErrors.New("claim already added by other recipe")
@@ -211,7 +215,7 @@ func (r *Recipe) getClaimsAddedByOtherRecipes() []*claims.TypeSessionClaim {
211215
return r.claimsAddedByOtherRecipes
212216
}
213217

214-
func (r *Recipe) addClaimValidatorFromOtherRecipe(validator claims.SessionClaimValidator) error {
218+
func (r *Recipe) AddClaimValidatorFromOtherRecipe(validator claims.SessionClaimValidator) error {
215219
r.claimValidatorsAddedByOtherRecipes = append(r.claimValidatorsAddedByOtherRecipes, validator)
216220
return nil
217221
}

0 commit comments

Comments
 (0)